Skip to content

Commit

Permalink
fix(commonjs): do not transform "typeof exports" for mixed modules
Browse files Browse the repository at this point in the history
resolves #1014
  • Loading branch information
lukastaegert committed Dec 14, 2021
1 parent e60a572 commit d121e16
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 19 deletions.
7 changes: 4 additions & 3 deletions packages/commonjs/src/transform-commonjs.js
Expand Up @@ -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
Expand Down
@@ -0,0 +1,4 @@
module.exports = {
description: 'replaces "typeof exports" with "undefined" in mixed modules',
pluginOptions: { transformMixedEsModules: true }
};
@@ -0,0 +1 @@
module.exports = 21;
@@ -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 };
30 changes: 16 additions & 14 deletions packages/commonjs/test/snapshots/function.js.md
Expand Up @@ -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';␊
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;␊
`,
}
Binary file modified packages/commonjs/test/snapshots/function.js.snap
Binary file not shown.
15 changes: 15 additions & 0 deletions packages/commonjs/test/snapshots/test.js.md
Expand Up @@ -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 };␊
`
Binary file modified packages/commonjs/test/snapshots/test.js.snap
Binary file not shown.
18 changes: 16 additions & 2 deletions packages/commonjs/test/test.js
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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);
});

0 comments on commit d121e16

Please sign in to comment.