Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(commonjs): support CJS modules re-exporting transpiled ESM modules #1165

Merged
merged 34 commits into from Apr 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
f829b36
fix(commonjs): attach correct plugin meta-data to commonjs modules
lukastaegert Sep 21, 2021
1393c7c
feat(commonjs): reimplement dynamic import handling
lukastaegert Oct 28, 2021
35770bd
feat(commonjs): add strictRequires option to wrap modules
lukastaegert Nov 6, 2021
81628db
feat(commonjs): automatically wrap cyclic modules
lukastaegert Nov 9, 2021
93cf5fb
feat(commonjs): Infer type for unidentified modules
lukastaegert Nov 12, 2021
8d31f25
feat(commonjs): make namespace callable when requiring ESM with funct…
lukastaegert Nov 12, 2021
fb490c0
fix(commonjs): handle relative external imports
lukastaegert Nov 17, 2021
e672b42
feat(commonjs): limit ignoreTryCatch to external requires
lukastaegert Nov 19, 2021
748f8ba
feat(commonjs): auto-detect conditional requires
lukastaegert Nov 20, 2021
c399755
feat(commonjs): add dynamicRequireRoot option
lukastaegert Nov 21, 2021
4a84215
feat(commonjs): throw for dynamic requires from outside the configure…
lukastaegert Nov 22, 2021
7e2691a
refactor(commonjs): deconflict helpers only once globals are known
lukastaegert Nov 22, 2021
a16a3cd
feat(commonjs): expose plugin version
lukastaegert Nov 24, 2021
46776c6
fix(commonjs): do not transform "typeof exports" for mixed modules
lukastaegert Dec 2, 2021
373684d
fix(commonjs): inject module name into dynamic require function
lukastaegert Dec 14, 2021
b7e0d7e
fix(commonjs): validate node-resolve peer version
lukastaegert Dec 14, 2021
45a3141
fix(commonjs): use correct version and add package exports
lukastaegert Dec 14, 2021
12dbaa3
fix(commonjs): proxy all entries to not break legacy polyfill plugins
lukastaegert Jan 14, 2022
0fb0303
fix(commonjs): add heuristic to deoptimize requires after calling imp…
lukastaegert Feb 7, 2022
ebc1cb1
fix(commonjs): make sure type changes of esm imports are tracked
lukastaegert Feb 12, 2022
b61845f
fix(commonjs): handle external dependencies when using the cache
lukastaegert Feb 24, 2022
b1110d2
fix(commonjs): Do not change semantics when removing requires in if s…
lukastaegert Apr 3, 2022
f509b1a
fix(commonjs): Allow to dynamically require an empty file
lukastaegert Apr 3, 2022
347c148
Add a test to illustrate problematic re-exported module
fwouts Apr 8, 2022
2749ea9
fix(commonjs): support CJS modules re-exporting ESM modules
fwouts Apr 16, 2022
d637611
fix(commonjs): Warn when plugins do not pass options to resolveId
lukastaegert Apr 18, 2022
095491b
Merge remote-tracking branch 'upstream/commonjs/strict-require-order'…
fwouts Apr 19, 2022
e3b4b0c
Update snapshots post-merge
fwouts Apr 19, 2022
c77baef
Update tests
fwouts Apr 19, 2022
4863298
Update snapshot
fwouts Apr 19, 2022
159336a
Remove unnecessary fixtures
fwouts Apr 19, 2022
981918d
Remove comment given other tests do exactly the same
fwouts Apr 19, 2022
205b187
Update snapshots
lukastaegert Apr 24, 2022
224f904
Merge remote-tracking branch 'upstream/commonjs/strict-require-order'…
lukastaegert Apr 24, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions packages/commonjs/src/transform-commonjs.js
Expand Up @@ -71,6 +71,7 @@ export default async function transformCommonjs(
let programDepth = 0;
let currentTryBlockEnd = null;
let shouldWrap = false;
let reexports = false;

const globals = new Set();
// A conditionalNode is a node for which execution is not guaranteed. If such a node is a require
Expand Down Expand Up @@ -151,8 +152,9 @@ export default async function transformCommonjs(
if (hasDefineEsmProperty(node.right)) {
shouldWrap = true;
}
} else if (defaultIsModuleExports === false) {
} else if (isRequireExpression(node.right, scope)) {
shouldWrap = true;
reexports = true;
}
}
} else if (exportName === KEY_COMPILED_ESM) {
Expand Down Expand Up @@ -444,7 +446,9 @@ export default async function transformCommonjs(
shouldWrap = !isEsModule && (shouldWrap || (uses.exports && moduleExportsAssignments.length > 0));
const detectWrappedDefault =
shouldWrap &&
(topLevelDefineCompiledEsmExpressions.length > 0 || code.indexOf('__esModule') >= 0);
(reexports ||
topLevelDefineCompiledEsmExpressions.length > 0 ||
code.indexOf('__esModule') >= 0);

if (
!(
Expand Down
@@ -0,0 +1,3 @@
module.exports = {
description: 'creates the correct exports from CJS module re-exporting a transpiled ES module',
};
@@ -0,0 +1,2 @@
Object.defineProperty(exports, '__esModule', { value: true });
exports.default = 'default';
@@ -0,0 +1,3 @@
import dep from './proxy';

t.is(dep, 'default');
@@ -0,0 +1 @@
module.exports = require('./dep');
@@ -0,0 +1,3 @@
module.exports = {
description: 'creates the correct exports from CJS module re-exporting a transpiled ES module',
};
@@ -0,0 +1,2 @@
Object.defineProperty(exports, '__esModule', { value: true });
exports.default = 'default';
@@ -0,0 +1,3 @@
import * as entry from './proxy';

t.deepEqual(entry, { default: 'default' });
@@ -0,0 +1 @@
module.exports = require('./entry');
@@ -0,0 +1,3 @@
module.exports = {
description: 'creates the correct exports from CJS module re-exporting a transpiled ES module',
};
@@ -0,0 +1,3 @@
Object.defineProperty(exports, '__esModule', { value: true });
exports.default = 'default';
exports.named = 'named';
@@ -0,0 +1,3 @@
import * as entry from './proxy';

t.deepEqual(entry, { default: 'default', named: 'named' });
@@ -0,0 +1 @@
module.exports = require('./entry');
@@ -0,0 +1,3 @@
module.exports = {
description: 'creates the correct exports from CJS module re-exporting a transpiled ES module',
};
@@ -0,0 +1,2 @@
Object.defineProperty(exports, '__esModule', { value: true });
exports.named = 'named';
@@ -0,0 +1,8 @@
import * as entry from './proxy';

t.deepEqual(entry, {
default: {
named: 'named',
},
named: 'named'
});
@@ -0,0 +1 @@
module.exports = require('./entry');
@@ -0,0 +1,3 @@
module.exports = {
description: 'creates the correct exports from CJS module re-exporting a transpiled ES module',
};
@@ -0,0 +1,3 @@
Object.defineProperty(exports, '__esModule', { value: true });
exports.named = 'named';
exports.default = 'default';
@@ -0,0 +1,4 @@
import dep, { named } from './proxy';

t.is(dep, 'default');
t.is(named, 'named');
@@ -0,0 +1 @@
module.exports = require('./dep');
@@ -0,0 +1,3 @@
module.exports = {
description: 'creates the correct exports from CJS module re-exporting a transpiled ES module',
};
@@ -0,0 +1,2 @@
Object.defineProperty(exports, '__esModule', { value: true });
exports.named = 'named';
@@ -0,0 +1,3 @@
import { named } from './proxy';

t.is(named, 'named');
@@ -0,0 +1 @@
module.exports = require('./dep');