-
-
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
Changes from 10 commits
6f3403b
86ca04e
e01a71f
b24c60a
1234fa0
7f61d93
6ab2b2e
7bf63b3
ac10384
30eea7b
0f586a7
3cc8069
49136f0
daee349
48e40ad
949d94f
4c5e7fa
1498254
1e5896d
18888ba
22a1c9b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = {foo: 1}; |
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; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = {foo: 1}; |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -706,51 +706,67 @@ 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.meta.kind === 'Require' || | ||||
dep.meta.kind === 'Import' || | ||||
dep.meta.kind === 'DynamicImport') && | ||||
dep.meta.kind !== kind | ||||
) { | ||||
continue; | ||||
} | ||||
dep.symbols.set(imported, local, convertLoc(loc)); | ||||
} | ||||
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. Could this be simplified to just let dep = nullthrows(deps.get(source)).find(d => 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. So there's a case where 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. So this would be the case where you have both reexports and imports of the same specifier in a single asset. But it looks like currently And in this loop, 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. Hmm in this test it looks like
|
||||
} | ||||
|
||||
for (let {source, local, imported, loc} of hoist_result.re_exports) { | ||||
let dep = 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); | ||||
for (let dep of nullthrows(deps.get(source))) { | ||||
if (dep.specifierType !== 'esm') { | ||||
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); | ||||
} | ||||
} | ||||
} | ||||
|
||||
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))) { | ||||
dep.meta.shouldWrap = true; | ||||
} | ||||
} | ||||
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.
|
||||
|
||||
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]), | ||||
)) { | ||||
dep.meta.promiseSymbol = name; | ||||
} | ||||
} | ||||
|
||||
if (hoist_result.self_references.length > 0) { | ||||
|
@@ -796,43 +812,50 @@ 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))) { | ||||
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))) { | ||||
dep.symbols.ensure(); | ||||
dep.symbols.set('*', '*', convertLoc(loc), true); | ||||
} | ||||
} | ||||
} | ||||
|
||||
|
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.