Skip to content

Commit c72c336

Browse files
authored
Fix dependency retargeting with ambiguous reexports (#9380)
1 parent 7959891 commit c72c336

File tree

10 files changed

+78
-27
lines changed

10 files changed

+78
-27
lines changed

packages/core/core/src/BundleGraph.js

+38-26
Original file line numberDiff line numberDiff line change
@@ -295,32 +295,44 @@ export default class BundleGraph {
295295
symbols.set(from, existing);
296296
} else {
297297
invariant(isReexportAll);
298-
let local = `${node.value.id}$rewrite$${asset}$${from}`;
299-
symbols.set(from, {
300-
isWeak: true,
301-
local,
302-
loc: reexportAllLoc,
303-
});
304-
// It might already exist with multiple export-alls causing ambiguous resolution
305-
if (
306-
node.value.sourceAssetId != null &&
307-
assetGraph.hasContentKey(node.value.sourceAssetId)
308-
) {
309-
let sourceAssetId = nullthrows(
310-
assetGraphNodeIdToBundleGraphNodeId.get(
311-
assetGraph.getNodeIdByContentKey(
312-
nullthrows(node.value.sourceAssetId),
298+
if (as === from) {
299+
// Keep the export-all for non-renamed reexports, this still correctly models
300+
// ambiguous resolution with multiple export-alls.
301+
symbols.set('*', {
302+
isWeak: true,
303+
local: '*',
304+
loc: reexportAllLoc,
305+
});
306+
} else {
307+
let local = `${node.value.id}$rewrite$${asset}$${from}`;
308+
symbols.set(from, {
309+
isWeak: true,
310+
local,
311+
loc: reexportAllLoc,
312+
});
313+
if (node.value.sourceAssetId != null) {
314+
let sourceAssetId = nullthrows(
315+
assetGraphNodeIdToBundleGraphNodeId.get(
316+
assetGraph.getNodeIdByContentKey(
317+
node.value.sourceAssetId,
318+
),
313319
),
314-
),
315-
);
316-
let sourceAsset = nullthrows(graph.getNode(sourceAssetId));
317-
invariant(sourceAsset.type === 'asset');
318-
let sourceAssetSymbols = sourceAsset.value.symbols;
319-
if (sourceAssetSymbols && !sourceAssetSymbols.has(as)) {
320-
sourceAssetSymbols.set(as, {
321-
loc: reexportAllLoc,
322-
local: local,
323-
});
320+
);
321+
let sourceAsset = nullthrows(
322+
graph.getNode(sourceAssetId),
323+
);
324+
invariant(sourceAsset.type === 'asset');
325+
let sourceAssetSymbols = sourceAsset.value.symbols;
326+
if (sourceAssetSymbols) {
327+
// The `as == from` case above should handle multiple export-alls causing
328+
// ambiguous resolution. So the current symbol is unambiguous and shouldn't
329+
// already exist on the importer.
330+
invariant(!sourceAssetSymbols.has(as));
331+
sourceAssetSymbols.set(as, {
332+
loc: reexportAllLoc,
333+
local: local,
334+
});
335+
}
324336
}
325337
}
326338
}
@@ -342,7 +354,7 @@ export default class BundleGraph {
342354
},
343355
usedSymbolsUp,
344356
// This is only a temporary helper needed during symbol propagation and is never
345-
// read afterwards.
357+
// read afterwards (and also not exposed through the public API).
346358
usedSymbolsDown: new Set(),
347359
}),
348360
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
if (Date.now() > 0) {
2+
output = require("./index.js").default;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { b,c } from "./lib";
2+
3+
export default b + " " + c;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module.exports = {};
2+
// export const a = 89;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const b = 123;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { c2 as c } from "./lib-c2.js";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const c2 = 999;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from "./lib-a";
2+
export * from "./lib-b";
3+
export * from "./lib-c";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"sideEffects": [
3+
"entry.js",
4+
"index.js",
5+
"lib-a.js",
6+
"lib-b.js",
7+
"lib-c2.js",
8+
"lib.js"
9+
]
10+
}

packages/core/integration-tests/test/scope-hoisting.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ describe('scope hoisting', function () {
148148
assert.equal(output, 2);
149149
});
150150

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

406+
it('supports re-exporting all with ambiguous CJS and non-renaming and renaming dependency retargeting', async function () {
407+
let b = await bundle(
408+
path.join(
409+
__dirname,
410+
'/integration/scope-hoisting/es6/re-export-all-ambiguous/entry.js',
411+
),
412+
{
413+
mode: 'production',
414+
},
415+
);
416+
417+
let output = await run(b);
418+
assert.strictEqual(output, '123 999');
419+
});
420+
406421
it('supports re-exporting all exports from an external module', async function () {
407422
let b = await bundle(
408423
path.join(

0 commit comments

Comments
 (0)