Skip to content

Commit 770c78b

Browse files
fix: don't forward a lazy barrel's own export names to its star re-export targets
1 parent e04df2a commit 770c78b

9 files changed

Lines changed: 117 additions & 4 deletions

File tree

‎lib/optimize/LazyBarrel.js‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,17 @@ class LazyBarrelInfo {
212212
this._fallbackTargetModules.add(module);
213213
}
214214

215+
/**
216+
* Returns whether a local export or named re-export provides the name.
217+
* @param {string} id export name
218+
* @returns {boolean} true, when the name does not need a star re-export
219+
*/
220+
isKnownId(id) {
221+
return (
222+
this._forwardIdToRequest !== undefined && this._forwardIdToRequest.has(id)
223+
);
224+
}
225+
215226
/**
216227
* Returns whether nothing is deferred.
217228
* @returns {boolean} true, when no dependency is deferred
@@ -524,11 +535,14 @@ class LazyBarrelController {
524535
const parentState =
525536
parent === undefined ? undefined : this._modules.get(parent);
526537
if (parentState !== undefined) {
527-
if (parentState.lazyBarrelInfo !== undefined) {
528-
parentState.lazyBarrelInfo.addFallbackTarget(module);
529-
}
538+
const parentInfo = parentState.lazyBarrelInfo;
539+
if (parentInfo !== undefined) parentInfo.addFallbackTarget(module);
530540
if (parentState.forwardedIds === true) return true;
531-
for (const id of parentState.forwardedIds) ids.add(id);
541+
// a name the barrel provides itself never comes from `export *`
542+
for (const id of parentState.forwardedIds) {
543+
if (parentInfo !== undefined && parentInfo.isKnownId(id)) continue;
544+
ids.add(id);
545+
}
532546
continue;
533547
}
534548
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const foo = "local";
2+
export * from "./star.js";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const foo = "heavy";
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { foo } from "./barrel.js";
2+
3+
it("should keep the local export", () =>
4+
import("./later.js").then(({ bar }) => {
5+
expect(foo).toBe("local");
6+
expect(bar).toBe("bar");
7+
}));
8+
9+
it("should not forward a barrel's local export to its star re-export target", () => {
10+
expect(BUILD_REPORT.modules.includes("heavy.js")).toBe(
11+
!BUILD_REPORT.sideEffects
12+
);
13+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { bar } from "./barrel.js";
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"sideEffects": false
3+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { foo } from "./heavy.js";
2+
export const bar = "bar";
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"use strict";
2+
3+
const fs = require("fs");
4+
const path = require("path");
5+
6+
module.exports = {
7+
findBundle(i) {
8+
return `bundle${i}.js`;
9+
},
10+
moduleScope(scope, options) {
11+
scope.BUILD_REPORT = JSON.parse(
12+
fs.readFileSync(
13+
path.join(options.output.path, `${options.name}.modules.json`),
14+
"utf8"
15+
)
16+
);
17+
}
18+
};
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"use strict";
2+
3+
const path = require("path");
4+
5+
/** @import { NormalModule } from "../../../../" */
6+
7+
/**
8+
* @param {boolean} sideEffects whether to honor the package's sideEffects flag
9+
* @param {boolean} concatenateModules whether to concatenate modules
10+
* @returns {import("../../../../").Configuration} configuration
11+
*/
12+
const config = (sideEffects, concatenateModules) => ({
13+
name: `side-effects-${sideEffects}-concatenation-${concatenateModules}`,
14+
mode: "production",
15+
optimization: {
16+
sideEffects,
17+
providedExports: true,
18+
usedExports: true,
19+
concatenateModules,
20+
minimize: false
21+
},
22+
plugins: [
23+
(compiler) => {
24+
compiler.hooks.thisCompilation.tap("Test", (compilation) => {
25+
/** @type {string[]} */
26+
let modulesBeforeOptimization = [];
27+
compilation.hooks.finishModules.tap("Test", (modules) => {
28+
// Inspect the make graph, including restored modules, before tree shaking
29+
// or concatenation can conceal dependencies that were already built.
30+
modulesBeforeOptimization = [];
31+
for (const module of modules) {
32+
const resource = /** @type {NormalModule} */ (module).resource;
33+
if (resource) {
34+
modulesBeforeOptimization.push(path.basename(resource));
35+
}
36+
}
37+
});
38+
compilation.hooks.processAssets.tap("Test", () => {
39+
compilation.emitAsset(
40+
`${compiler.options.name}.modules.json`,
41+
new compiler.webpack.sources.RawSource(
42+
JSON.stringify({
43+
sideEffects,
44+
modules: modulesBeforeOptimization.sort()
45+
})
46+
)
47+
);
48+
});
49+
});
50+
}
51+
]
52+
});
53+
54+
/** @type {import("../../../../").Configuration[]} */
55+
module.exports = [
56+
config(true, false),
57+
config(true, true),
58+
config(false, false)
59+
];

0 commit comments

Comments
 (0)