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

Default interop missing when importing a CommonJS module #7991

Merged
merged 21 commits into from Jul 2, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
6f3403b
Add test case for missing default interop for commonjs import
thebriando Apr 21, 2022
86ca04e
Add interopDefault helper if the dep kind is require
thebriando Apr 21, 2022
e01a71f
Add test case for 'static' hybrid interop
lettertwo Apr 22, 2022
b24c60a
Update test description
thebriando Apr 22, 2022
1234fa0
Update test dirname
thebriando Apr 22, 2022
7f61d93
Fix dirname
lettertwo Apr 22, 2022
6ab2b2e
Update packages/core/integration-tests/test/scope-hoisting.js
thebriando Apr 25, 2022
7bf63b3
Store import kinds in imported symbols and only set symbols when kind…
thebriando May 9, 2022
ac10384
Remove unnecessary conditions
thebriando May 12, 2022
30eea7b
Only set symbols for esm deps when iterating through hoist_result.re_…
thebriando May 13, 2022
0f586a7
Add comments for new dep Map structure
thebriando May 23, 2022
3cc8069
Key by specifier + specifierType instead of keeping an array of deps
thebriando May 27, 2022
49136f0
Make import specifiers unique in packager
thebriando Jun 5, 2022
daee349
Track specifier types in wrapped_requires
thebriando Jun 7, 2022
48e40ad
Don't append specifier types to placeholders, since they are already …
thebriando Jun 16, 2022
949d94f
Merge remote-tracking branch 'origin/v2' into bdo/default-interop
thebriando Jun 16, 2022
4c5e7fa
Fix hoist unit tests
thebriando Jun 16, 2022
1498254
Merge branch 'v2' into bdo/default-interop
thebriando Jun 16, 2022
1e5896d
Merge branch 'v2' into bdo/default-interop
thebriando Jun 22, 2022
18888ba
Merge branch 'v2' into bdo/default-interop
thebriando Jun 28, 2022
22a1c9b
Merge branch 'v2' into bdo/default-interop
thebriando Jul 1, 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
@@ -0,0 +1,5 @@
import b from './b';
export const foo = b.foo; // <-- missing default interop
export const bar = (() => require('./b').foo)();

output = foo + bar;
@@ -0,0 +1 @@
module.exports = {foo: 1};
@@ -0,0 +1,5 @@
import b from './b';
const foo = b.foo;
const bar = require('./b').foo;

output = foo + bar;
@@ -0,0 +1 @@
module.exports = {foo: 1};
24 changes: 24 additions & 0 deletions packages/core/integration-tests/test/scope-hoisting.js
Expand Up @@ -4359,6 +4359,30 @@ describe('scope hoisting', function () {
assert.deepEqual(output.default, obj);
});

it('should add a default interop for a CJS module used in a hybrid module', async function () {
let b = await bundle(
path.join(
__dirname,
'/integration/scope-hoisting/commonjs/interop-commonjs-hybrid/a.js',
),
);

let output = await run(b);
assert.equal(output, 2);
});

it('should add a default interop for a CJS module used dynamically in a hybrid module', async function () {
thebriando marked this conversation as resolved.
Show resolved Hide resolved
let b = await bundle(
path.join(
__dirname,
'/integration/scope-hoisting/commonjs/interop-commonjs-hybrid-dynamic/a.js',
),
);

let output = await run(b);
assert.equal(output, 2);
});

it('should not insert default interop for wrapped CJS modules', async function () {
let b = await bundle(
path.join(
Expand Down
5 changes: 4 additions & 1 deletion packages/packagers/js/src/ScopeHoistingPackager.js
Expand Up @@ -834,7 +834,10 @@ ${code}
// use a helper to check the __esModule flag at runtime.
let kind = dep?.meta.kind;
if (
(!dep || kind === 'Import' || kind === 'Export') &&
(!dep ||
kind === 'Import' ||
kind === 'Require' ||
thebriando marked this conversation as resolved.
Show resolved Hide resolved
kind === 'Export') &&
exportSymbol === 'default' &&
resolvedAsset.symbols.hasExportSymbol('*') &&
this.needsDefaultInterop(resolvedAsset)
Expand Down