From 503d2e3035fb69ceed440122443791655fa42ab4 Mon Sep 17 00:00:00 2001 From: Young Liang Date: Sun, 17 Mar 2024 22:56:35 +0800 Subject: [PATCH 1/2] fix: update regex to do not match new line in specifier group --- packages/esm-shim/src/constants.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/esm-shim/src/constants.ts b/packages/esm-shim/src/constants.ts index ef2953f5e..9f00e960c 100644 --- a/packages/esm-shim/src/constants.ts +++ b/packages/esm-shim/src/constants.ts @@ -11,4 +11,4 @@ const require = cjsModule.createRequire(import.meta.url); `; export const ESMStaticImportRegex = - /(?<=\s|^|;)import\s*([\s"']*(?[\w\t\n\r $*,/{}]+)from\s*)?["']\s*(?(?<="\s*)[^"]*[^\s"](?=\s*")|(?<='\s*)[^']*[^\s'](?=\s*'))\s*["'][\s;]*/gm; + /(?<=\s|^|;)import\s*([\s"']*(?[\w\t\n\r $*,/{}]+)from\s*)?["']\s*(?(?<="\s*)[^"\n]*[^\s"](?=\s*")|(?<='\s*)[^'\n]*[^\s'](?=\s*'))\s*["'][\s;]*/gm; From a6b2fed6b882e474e41ed304418d490baef4a0b7 Mon Sep 17 00:00:00 2001 From: Young Liang Date: Sun, 17 Mar 2024 23:01:55 +0800 Subject: [PATCH 2/2] test: add testcases and update snapshot --- .../test/fixtures/cjs-import-literal.js | 6 ++++++ packages/esm-shim/test/snapshots/test.js.md | 20 ++++++++++++++++++ packages/esm-shim/test/snapshots/test.js.snap | Bin 540 -> 624 bytes packages/esm-shim/test/test.js | 16 ++++++++++++++ 4 files changed, 42 insertions(+) create mode 100644 packages/esm-shim/test/fixtures/cjs-import-literal.js diff --git a/packages/esm-shim/test/fixtures/cjs-import-literal.js b/packages/esm-shim/test/fixtures/cjs-import-literal.js new file mode 100644 index 000000000..915b1ceaf --- /dev/null +++ b/packages/esm-shim/test/fixtures/cjs-import-literal.js @@ -0,0 +1,6 @@ +const dn = __dirname; + +module.exports = { + keyword: ' import', + dn +}; diff --git a/packages/esm-shim/test/snapshots/test.js.md b/packages/esm-shim/test/snapshots/test.js.md index 1447dcc3a..98c82442e 100644 --- a/packages/esm-shim/test/snapshots/test.js.md +++ b/packages/esm-shim/test/snapshots/test.js.md @@ -93,3 +93,23 @@ Generated by [AVA](https://avajs.dev). ␊ export { c, child, s };␊ ` + +## inject cjs shim should not break on valid js object with `import` literal value + +> Snapshot 1 + + `␊ + // -- Shims --␊ + import cjsUrl from 'node:url';␊ + import cjsPath from 'node:path';␊ + import cjsModule from 'node:module';␊ + const __filename = cjsUrl.fileURLToPath(import.meta.url);␊ + const __dirname = cjsPath.dirname(__filename);␊ + const require = cjsModule.createRequire(import.meta.url);␊ + const dn = __dirname;␊ + ␊ + module.exports = {␊ + keyword: ' import',␊ + dn␊ + };␊ + ` diff --git a/packages/esm-shim/test/snapshots/test.js.snap b/packages/esm-shim/test/snapshots/test.js.snap index 7c8a3ca3d7b213043a7090bfdce2b3e22540253f..56705e6526167abd66b98c8ee729fa301541e67e 100644 GIT binary patch literal 624 zcmV-$0+0PcRzVeb*j_lGLS_ zt2sbZKOc(-00000000B+Rm*PEKom_t2+2Es0A0btK}wu`tFS{XAfjrB1ZBVCv z&uMBQJvU%hOu#~EphkdlHg}d8NpiDN)_l3iGGfX5?uYxGhaLaxKIlBU_o^EWhcFnx z1cMFUL`>vL7YV1D!$KvsWm`$ADY5jl z^xRq~T``_vT*%Oi3qy?lh+Y(fUIoncakM36a5pKiVYoS?oKC4Ac?(CM8yu|<9Q`|v zhQp^}^<$FfOfGSd5mrp_LNaz7K9R~o8;}0smY3_@w9Z@iW$X+EV>L@H?sMO6O|pd) z|COvp`^bub!J&}Sz_GEoK|y8PQShT7vv1uHGA2uwPOxpc48joJGBAb7Pjb-XU+S?0 z7<5XysV}-0x9GKx$l9^;d>{UWeoa5btYlsY)gkJd~^NbPUi Kl@5Xy3IG7c63de9nQwl-&wDlhQW$ZLzplYcU%=%{ zsn0-oCeD!IQYtn#jsYrWN^eUqQFDt+R0Mr=4_ zQwm0U=}Tpes}jy!!RF2=mTHs3o6;5OLBoB-{j%w|^xXnXOkNhygs93wAYn9mc6vIO z1!%5-jbp(qSZ2o`KAMnzuuhgK@I0gXuj|NMx;ijmvuGLASs&mIzLwHO@Vc!`IN%s=AI_Pgm8UC3Rb|GnVXi6tQaofSs_ZCOzJC6Px zN2AeRT>XSwEA@f~8B@iKZX)A>!&lmP*2QD^c;x3gnKgM!4$~5Wp?6Yv+UK_2Vzz-) z&stXFZDhq6(ojTcdPbWz%;@EeY+nRCEs&6y?@C2mk==eg<*? diff --git a/packages/esm-shim/test/test.js b/packages/esm-shim/test/test.js index 3f9e5f8e4..24f5a3291 100644 --- a/packages/esm-shim/test/test.js +++ b/packages/esm-shim/test/test.js @@ -67,3 +67,19 @@ test.serial('inject cjs shim for esm output with multiple import statements', as t.snapshot(output.code); t.falsy(output.map); }); + +// see issue #1649 https://github.com/rollup/plugins/issues/1649 +test.serial( + 'inject cjs shim should not break on valid js object with `import` literal value', + async (t) => { + const bundle = await rollup({ + input: 'test/fixtures/cjs-import-literal.js', + plugins: [esmShim()] + }); + const result = await bundle.generate({ format: 'es' }); + t.is(result.output.length, 1); + const [output] = result.output; + t.snapshot(output.code); + t.falsy(output.map); + } +);