diff --git a/packages/commonjs/src/transform-commonjs.js b/packages/commonjs/src/transform-commonjs.js index b1635e954..3fb8602cd 100644 --- a/packages/commonjs/src/transform-commonjs.js +++ b/packages/commonjs/src/transform-commonjs.js @@ -369,9 +369,10 @@ export default async function transformCommonjs( if (scope.contains(flattened.name)) return; if ( - flattened.keypath === 'module.exports' || - flattened.keypath === 'module' || - flattened.keypath === 'exports' + !isEsModule && + (flattened.keypath === 'module.exports' || + flattened.keypath === 'module' || + flattened.keypath === 'exports') ) { magicString.overwrite(node.start, node.end, `'object'`, { storeName: false diff --git a/packages/commonjs/test/fixtures/samples/mixed-module-typeof-exports/_config.js b/packages/commonjs/test/fixtures/samples/mixed-module-typeof-exports/_config.js new file mode 100644 index 000000000..3d25d8d4d --- /dev/null +++ b/packages/commonjs/test/fixtures/samples/mixed-module-typeof-exports/_config.js @@ -0,0 +1,4 @@ +module.exports = { + description: 'replaces "typeof exports" with "undefined" in mixed modules', + pluginOptions: { transformMixedEsModules: true } +}; diff --git a/packages/commonjs/test/fixtures/samples/mixed-module-typeof-exports/foo.js b/packages/commonjs/test/fixtures/samples/mixed-module-typeof-exports/foo.js new file mode 100644 index 000000000..ce0fffb75 --- /dev/null +++ b/packages/commonjs/test/fixtures/samples/mixed-module-typeof-exports/foo.js @@ -0,0 +1 @@ +module.exports = 21; diff --git a/packages/commonjs/test/fixtures/samples/mixed-module-typeof-exports/main.js b/packages/commonjs/test/fixtures/samples/mixed-module-typeof-exports/main.js new file mode 100644 index 000000000..b1f34e2a2 --- /dev/null +++ b/packages/commonjs/test/fixtures/samples/mixed-module-typeof-exports/main.js @@ -0,0 +1,7 @@ +const foo = require('./foo'); + +if (typeof exports !== 'undefined') { + throw new Error('There should be no global exports in an ES module'); +} + +export { foo as default }; diff --git a/packages/commonjs/test/snapshots/function.js.md b/packages/commonjs/test/snapshots/function.js.md index 4377db9eb..70584b812 100644 --- a/packages/commonjs/test/snapshots/function.js.md +++ b/packages/commonjs/test/snapshots/function.js.md @@ -3050,8 +3050,10 @@ Generated by [AVA](https://avajs.dev). root = window;␊ } else if (typeof global !== 'undefined') {␊ root = global;␊ - } else {␊ + } else if (typeof module !== 'undefined') {␊ root = module;␊ + } else {␊ + root = Function('return this')(); // eslint-disable-line no-new-func␊ }␊ ␊ root.pollution = 'foo';␊ @@ -4725,6 +4727,19 @@ Generated by [AVA](https://avajs.dev). `, } +## load-cycle-parallel + +> Snapshot 1 + + { + 'main.js': `'use strict';␊ + ␊ + var main = {};␊ + ␊ + module.exports = main;␊ + `, + } + ## module-meta-properties > Snapshot 1 @@ -7251,16 +7266,3 @@ Generated by [AVA](https://avajs.dev). module.exports = main;␊ `, } - -## load-cycle-parallel - -> Snapshot 1 - - { - 'main.js': `'use strict';␊ - ␊ - var main = {};␊ - ␊ - module.exports = main;␊ - `, - } diff --git a/packages/commonjs/test/snapshots/function.js.snap b/packages/commonjs/test/snapshots/function.js.snap index a4b135629..9a637b0f7 100644 Binary files a/packages/commonjs/test/snapshots/function.js.snap and b/packages/commonjs/test/snapshots/function.js.snap differ diff --git a/packages/commonjs/test/snapshots/test.js.md b/packages/commonjs/test/snapshots/test.js.md index 86e1a2048..1b1ebe464 100644 --- a/packages/commonjs/test/snapshots/test.js.md +++ b/packages/commonjs/test/snapshots/test.js.md @@ -100,3 +100,18 @@ Generated by [AVA](https://avajs.dev). ␊ module.exports = main;␊ ` + +## does not transform typeof exports for mixed modules + +> Snapshot 1 + + `var foo$1 = 21;␊ + ␊ + const foo = foo$1;␊ + ␊ + if (typeof exports !== 'undefined') {␊ + throw new Error('There should be no global exports in an ES module');␊ + }␊ + ␊ + export { foo as default };␊ + ` diff --git a/packages/commonjs/test/snapshots/test.js.snap b/packages/commonjs/test/snapshots/test.js.snap index d685942af..685297264 100644 Binary files a/packages/commonjs/test/snapshots/test.js.snap and b/packages/commonjs/test/snapshots/test.js.snap differ diff --git a/packages/commonjs/test/test.js b/packages/commonjs/test/test.js index ea0afb695..097f1a6df 100644 --- a/packages/commonjs/test/test.js +++ b/packages/commonjs/test/test.js @@ -331,8 +331,8 @@ test('typeof transforms: sinon', async (t) => { } = await bundle.generate({ format: 'es' }); t.is(code.indexOf('typeof require'), -1, code); - // t.not( code.indexOf( 'typeof module' ), -1, code ); // #151 breaks this test - // t.not( code.indexOf( 'typeof define' ), -1, code ); // #144 breaks this test + t.is(code.indexOf('typeof module'), -1, code); + t.is(code.indexOf('typeof define'), -1, code); }); test('deconflicts helper name', async (t) => { @@ -717,3 +717,17 @@ test('throws when there is a dynamic require from outside dynamicRequireRoot', a dynamicRequireRoot }); }); + +test('does not transform typeof exports for mixed modules', async (t) => { + const bundle = await rollup({ + input: 'fixtures/samples/mixed-module-typeof-exports/main.js', + plugins: [commonjs({ transformMixedEsModules: true })] + }); + + const { + output: [{ code }] + } = await bundle.generate({ format: 'es' }); + + t.is(code.includes('typeof exports'), true, '"typeof exports" not found in the code'); + t.snapshot(code); +});