-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Changes from 8 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 86ca04e
Add interopDefault helper if the dep kind is require
thebriando e01a71f
Add test case for 'static' hybrid interop
lettertwo b24c60a
Update test description
thebriando 1234fa0
Update test dirname
thebriando 7f61d93
Fix dirname
lettertwo 6ab2b2e
Update packages/core/integration-tests/test/scope-hoisting.js
thebriando 7bf63b3
Store import kinds in imported symbols and only set symbols when kind…
thebriando ac10384
Remove unnecessary conditions
thebriando 30eea7b
Only set symbols for esm deps when iterating through hoist_result.re_…
thebriando 0f586a7
Add comments for new dep Map structure
thebriando 3cc8069
Key by specifier + specifierType instead of keeping an array of deps
thebriando 49136f0
Make import specifiers unique in packager
thebriando daee349
Track specifier types in wrapped_requires
thebriando 48e40ad
Don't append specifier types to placeholders, since they are already …
thebriando 949d94f
Merge remote-tracking branch 'origin/v2' into bdo/default-interop
thebriando 4c5e7fa
Fix hoist unit tests
thebriando 1498254
Merge branch 'v2' into bdo/default-interop
thebriando 1e5896d
Merge branch 'v2' into bdo/default-interop
thebriando 18888ba
Merge branch 'v2' into bdo/default-interop
thebriando 22a1c9b
Merge branch 'v2' into bdo/default-interop
thebriando File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
...ation-tests/test/integration/scope-hoisting/commonjs/interop-commonjs-hybrid-dynamic/a.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
1 change: 1 addition & 0 deletions
1
...ation-tests/test/integration/scope-hoisting/commonjs/interop-commonjs-hybrid-dynamic/b.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = {foo: 1}; |
5 changes: 5 additions & 0 deletions
5
...e/integration-tests/test/integration/scope-hoisting/commonjs/interop-commonjs-hybrid/a.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import b from './b'; | ||
const foo = b.foo; | ||
const bar = require('./b').foo; | ||
|
||
output = foo + bar; |
1 change: 1 addition & 0 deletions
1
...e/integration-tests/test/integration/scope-hoisting/commonjs/interop-commonjs-hybrid/b.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = {foo: 1}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -706,51 +706,69 @@ export default (new Transformer({ | |
asset.symbols.set(exported, local, convertLoc(loc)); | ||
} | ||
|
||
let deps = new Map( | ||
asset | ||
.getDependencies() | ||
.map(dep => [dep.meta.placeholder ?? dep.specifier, dep]), | ||
); | ||
for (let dep of deps.values()) { | ||
dep.symbols.ensure(); | ||
let deps = new Map(); | ||
for (let dep of asset.getDependencies()) { | ||
if (!deps.has(dep.meta.placeholder ?? dep.specifier)) { | ||
deps.set(dep.meta.placeholder ?? dep.specifier, []); | ||
} | ||
deps.get(dep.meta.placeholder ?? dep.specifier)?.push(dep); | ||
} | ||
for (let depArr of deps.values()) { | ||
for (let dep of depArr) { | ||
dep.symbols.ensure(); | ||
} | ||
} | ||
|
||
for (let { | ||
source, | ||
local, | ||
imported, | ||
loc, | ||
kind, | ||
} of hoist_result.imported_symbols) { | ||
let dep = deps.get(source); | ||
if (!dep) continue; | ||
dep.symbols.set(imported, local, convertLoc(loc)); | ||
for (let dep of nullthrows(deps.get(source))) { | ||
if ( | ||
!dep || | ||
((dep.meta.kind === 'Require' || | ||
dep.meta.kind === 'Import' || | ||
dep.meta.kind === 'DynamicImport') && | ||
dep.meta.kind !== kind) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
) { | ||
continue; | ||
} | ||
dep.symbols.set(imported, local, convertLoc(loc)); | ||
} | ||
} | ||
|
||
for (let {source, local, imported, loc} of hoist_result.re_exports) { | ||
let dep = deps.get(source); | ||
if (!dep) continue; | ||
for (let dep of nullthrows(deps.get(source))) { | ||
if (!dep) continue; | ||
|
||
if (local === '*' && imported === '*') { | ||
dep.symbols.set('*', '*', convertLoc(loc), true); | ||
} else { | ||
let reExportName = | ||
dep.symbols.get(imported)?.local ?? | ||
`$${asset.id}$re_export$${local}`; | ||
asset.symbols.set(local, reExportName); | ||
dep.symbols.set(imported, reExportName, convertLoc(loc), true); | ||
if (local === '*' && imported === '*') { | ||
dep.symbols.set('*', '*', convertLoc(loc), true); | ||
} else { | ||
let reExportName = | ||
dep.symbols.get(imported)?.local ?? | ||
`$${asset.id}$re_export$${local}`; | ||
asset.symbols.set(local, reExportName); | ||
dep.symbols.set(imported, reExportName, convertLoc(loc), true); | ||
} | ||
} | ||
} | ||
|
||
for (let specifier of hoist_result.wrapped_requires) { | ||
let dep = deps.get(specifier); | ||
if (!dep) continue; | ||
dep.meta.shouldWrap = true; | ||
for (let dep of nullthrows(deps.get(specifier))) { | ||
if (!dep) continue; | ||
dep.meta.shouldWrap = true; | ||
} | ||
} | ||
|
||
for (let name in hoist_result.dynamic_imports) { | ||
let dep = deps.get(hoist_result.dynamic_imports[name]); | ||
if (!dep) continue; | ||
dep.meta.promiseSymbol = name; | ||
for (let dep of nullthrows( | ||
deps.get(hoist_result.dynamic_imports[name]), | ||
)) { | ||
if (!dep) continue; | ||
dep.meta.promiseSymbol = name; | ||
} | ||
} | ||
|
||
if (hoist_result.self_references.length > 0) { | ||
|
@@ -796,43 +814,52 @@ export default (new Transformer({ | |
asset.meta.shouldWrap = hoist_result.should_wrap; | ||
} else { | ||
if (symbol_result) { | ||
let deps = new Map( | ||
asset | ||
.getDependencies() | ||
.map(dep => [dep.meta.placeholder ?? dep.specifier, dep]), | ||
); | ||
let deps = new Map(); | ||
for (let dep of asset.getDependencies()) { | ||
if (!deps.has(dep.meta.placeholder ?? dep.specifier)) { | ||
deps.set(dep.meta.placeholder ?? dep.specifier, []); | ||
} | ||
deps.get(dep.meta.placeholder ?? dep.specifier)?.push(dep); | ||
} | ||
asset.symbols.ensure(); | ||
|
||
for (let {exported, local, loc, source} of symbol_result.exports) { | ||
let dep = source ? deps.get(source) : undefined; | ||
asset.symbols.set( | ||
exported, | ||
`${dep?.id ?? ''}$${local}`, | ||
convertLoc(loc), | ||
); | ||
if (dep != null) { | ||
dep.symbols.ensure(); | ||
dep.symbols.set( | ||
local, | ||
`${dep?.id ?? ''}$${local}`, | ||
convertLoc(loc), | ||
true, | ||
); | ||
if (!deps.get(source)) { | ||
asset.symbols.set(exported, `${local}`, convertLoc(loc)); | ||
} else { | ||
for (let dep of nullthrows(deps.get(source))) { | ||
asset.symbols.set( | ||
exported, | ||
`${dep?.id ?? ''}$${local}`, | ||
convertLoc(loc), | ||
); | ||
if (dep != null) { | ||
dep.symbols.ensure(); | ||
dep.symbols.set( | ||
local, | ||
`${dep?.id ?? ''}$${local}`, | ||
convertLoc(loc), | ||
true, | ||
); | ||
} | ||
} | ||
} | ||
} | ||
|
||
for (let {source, local, imported, loc} of symbol_result.imports) { | ||
let dep = deps.get(source); | ||
if (!dep) continue; | ||
dep.symbols.ensure(); | ||
dep.symbols.set(imported, local, convertLoc(loc)); | ||
for (let dep of nullthrows(deps.get(source))) { | ||
if (!dep) continue; | ||
dep.symbols.ensure(); | ||
dep.symbols.set(imported, local, convertLoc(loc)); | ||
} | ||
} | ||
|
||
for (let {source, loc} of symbol_result.exports_all) { | ||
let dep = deps.get(source); | ||
if (!dep) continue; | ||
dep.symbols.ensure(); | ||
dep.symbols.set('*', '*', convertLoc(loc), true); | ||
for (let dep of nullthrows(deps.get(source))) { | ||
if (!dep) continue; | ||
dep.symbols.ensure(); | ||
dep.symbols.set('*', '*', convertLoc(loc), true); | ||
} | ||
} | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A possible alternative way of implementing this could be to make the map keyed by
(dep.meta.placeholder ?? dep.specifier) + dep.specifierType
, rather than an array of dependencies per specifier.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the main reason why I didn't add the kind to the key is because we only want to do the
ImportKind
specific stuff when we are looking athoist_result.imported_symbols
. Keying by specifier + specifierType would mean that we would have to change all the other things in thehoist_result
to track thekind
, but I'm not sure if we should do that.