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 dependency retargeting with ambiguous reexports #9380

Merged
merged 3 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
64 changes: 38 additions & 26 deletions packages/core/core/src/BundleGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,32 +295,44 @@ export default class BundleGraph {
symbols.set(from, existing);
} else {
invariant(isReexportAll);
let local = `${node.value.id}$rewrite$${asset}$${from}`;
symbols.set(from, {
isWeak: true,
local,
loc: reexportAllLoc,
});
// It might already exist with multiple export-alls causing ambiguous resolution
if (
node.value.sourceAssetId != null &&
assetGraph.hasContentKey(node.value.sourceAssetId)
) {
let sourceAssetId = nullthrows(
assetGraphNodeIdToBundleGraphNodeId.get(
assetGraph.getNodeIdByContentKey(
nullthrows(node.value.sourceAssetId),
if (as === from) {
// Keep the export-all for non-renamed reexports, this still correctly models
// ambiguous resolution with multiple export-alls.
symbols.set('*', {
isWeak: true,
local: '*',
loc: reexportAllLoc,
});
} else {
let local = `${node.value.id}$rewrite$${asset}$${from}`;
symbols.set(from, {
isWeak: true,
local,
loc: reexportAllLoc,
});
if (node.value.sourceAssetId != null) {
let sourceAssetId = nullthrows(
assetGraphNodeIdToBundleGraphNodeId.get(
assetGraph.getNodeIdByContentKey(
node.value.sourceAssetId,
),
),
),
);
let sourceAsset = nullthrows(graph.getNode(sourceAssetId));
invariant(sourceAsset.type === 'asset');
let sourceAssetSymbols = sourceAsset.value.symbols;
if (sourceAssetSymbols && !sourceAssetSymbols.has(as)) {
sourceAssetSymbols.set(as, {
loc: reexportAllLoc,
local: local,
});
);
let sourceAsset = nullthrows(
graph.getNode(sourceAssetId),
);
invariant(sourceAsset.type === 'asset');
let sourceAssetSymbols = sourceAsset.value.symbols;
if (sourceAssetSymbols) {
// The `as == from` case above should handle multiple export-alls causing
// ambiguous resolution. So the current symbol is unambiguous and shouldn't
// already exist on the importer.
invariant(!sourceAssetSymbols.has(as));
sourceAssetSymbols.set(as, {
loc: reexportAllLoc,
local: local,
});
}
}
}
}
Expand All @@ -342,7 +354,7 @@ export default class BundleGraph {
},
usedSymbolsUp,
// This is only a temporary helper needed during symbol propagation and is never
// read afterwards.
// read afterwards (and also not exposed through the public API).
usedSymbolsDown: new Set(),
}),
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
if (Date.now() > 0) {
output = require("./index.js").default;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { b,c } from "./lib";

export default b + " " + c;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module.exports = {};
// export const a = 89;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const b = 123;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { c2 as c } from "./lib-c2.js";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const c2 = 999;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./lib-a";
export * from "./lib-b";
export * from "./lib-c";
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"sideEffects": [
"entry.js",
"index.js",
"lib-a.js",
"lib-b.js",
"lib-c2.js",
"lib.js"
]
}
17 changes: 16 additions & 1 deletion packages/core/integration-tests/test/scope-hoisting.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ describe('scope hoisting', function () {
assert.equal(output, 2);
});

it('supports import * as from a library that has export *', async function () {
it('supports dependency rewriting for import * as from a library that has export *', async function () {
let b = await bundle(
path.join(
__dirname,
Expand Down Expand Up @@ -403,6 +403,21 @@ describe('scope hoisting', function () {
assert.match(contents, /output="foo bar"/);
});

it('supports re-exporting all with ambiguous CJS and non-renaming and renaming dependency retargeting', async function () {
let b = await bundle(
path.join(
__dirname,
'/integration/scope-hoisting/es6/re-export-all-ambiguous/entry.js',
),
{
mode: 'production',
},
);

let output = await run(b);
assert.strictEqual(output, '123 999');
});

it('supports re-exporting all exports from an external module', async function () {
let b = await bundle(
path.join(
Expand Down