Skip to content

Commit 55a53f5

Browse files
authored
Defer ordinary imports while inspecting lazy barrel exports (#22282)
* Optimize lazy barrel ordinary imports * Update stats snapshots for lazy barrel optimization
1 parent b9acdce commit 55a53f5

13 files changed

Lines changed: 225 additions & 13 deletions

File tree

‎lib/optimize/LazyBarrel.js‎

Lines changed: 105 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ class LazyBarrelInfo {
6969
this._terminalIds = undefined;
7070
/** @type {Set<string> | undefined} requests of star re-exports */
7171
this._fallbackRequests = undefined;
72+
/** @type {Set<string> | undefined} ordinary import request keys */
73+
this._ordinaryRequests = undefined;
7274
}
7375

7476
/**
@@ -84,7 +86,9 @@ class LazyBarrelInfo {
8486
group = { factory, dependencies: [], context };
8587
this._requestToDepGroup.set(requestKey, group);
8688
}
87-
group.dependencies.push(dependency);
89+
if (!group.dependencies.includes(dependency)) {
90+
group.dependencies.push(dependency);
91+
}
8892
}
8993

9094
/**
@@ -116,6 +120,17 @@ class LazyBarrelInfo {
116120
this._fallbackRequests.add(requestKey);
117121
}
118122

123+
/**
124+
* Records an ordinary import that is safe to defer while inspecting exports.
125+
* @param {string} requestKey request key
126+
*/
127+
addOrdinary(requestKey) {
128+
if (this._ordinaryRequests === undefined) {
129+
this._ordinaryRequests = new Set();
130+
}
131+
this._ordinaryRequests.add(requestKey);
132+
}
133+
119134
/**
120135
* Returns whether nothing is deferred.
121136
* @returns {boolean} true, when no dependency is deferred
@@ -140,6 +155,7 @@ class LazyBarrelInfo {
140155
this._forwardIdToRequest.clear();
141156
this._terminalIds = undefined;
142157
this._fallbackRequests = undefined;
158+
this._ordinaryRequests = undefined;
143159
}
144160

145161
/**
@@ -165,6 +181,15 @@ class LazyBarrelInfo {
165181
this._release();
166182
return result;
167183
}
184+
let requestOrdinary = false;
185+
if (this._terminalIds !== undefined) {
186+
for (const id of forwardedIds) {
187+
if (this._terminalIds.has(id)) {
188+
requestOrdinary = true;
189+
break;
190+
}
191+
}
192+
}
168193
/**
169194
* @param {string} requestKey request key to take
170195
*/
@@ -186,6 +211,9 @@ class LazyBarrelInfo {
186211
for (const fallbackKey of this._fallbackRequests) take(fallbackKey);
187212
}
188213
}
214+
if (requestOrdinary && this._ordinaryRequests !== undefined) {
215+
for (const requestKey of this._ordinaryRequests) take(requestKey);
216+
}
189217
if (this._requestToDepGroup.size === 0) this._release();
190218
return result;
191219
}
@@ -249,9 +277,35 @@ class LazyBarrelController {
249277
}
250278
const dependencies = module.dependencies;
251279
const dependencyFactories = this._compilation.dependencyFactories;
280+
const state = modules.get(module);
281+
const forwardedIds = state && state.forwardedIds;
282+
let inspectOnly =
283+
forwardedIds !== undefined &&
284+
forwardedIds !== true &&
285+
forwardedIds.size > 0;
286+
/** @type {Set<string>} */
287+
const terminalIds = new Set();
288+
for (const dep of dependencies) {
289+
if (
290+
"getLazyUntil" in dep &&
291+
dep.getLazyUntil() === Dependency.LAZY_UNTIL_LOCAL
292+
) {
293+
const name = dep.getLazyName();
294+
if (typeof name === "string") terminalIds.add(name);
295+
}
296+
}
297+
if (inspectOnly && forwardedIds !== undefined && forwardedIds !== true) {
298+
for (const id of forwardedIds) {
299+
if (terminalIds.has(id)) {
300+
inspectOnly = false;
301+
break;
302+
}
303+
}
304+
}
252305
/** @type {LazyBarrelInfo | undefined} */
253306
let info;
254307
let hasFallback = false;
308+
let hasOrdinary = false;
255309
for (const dep of dependencies) {
256310
// TODO in the next major release: remove
257311
// It may be missing on custom dependency types not extending the base Dependency
@@ -289,15 +343,41 @@ class LazyBarrelController {
289343
hasFallback = true;
290344
}
291345
}
292-
const state = modules.get(module);
346+
// Ordinary ESM imports are only deferred when this module is being built to
347+
// inspect a named star export and that name is not locally provided.
348+
if (inspectOnly) {
349+
for (const dep of dependencies) {
350+
if (
351+
"getLazyUntil" in dep &&
352+
(dep.getLazyUntil() === null ||
353+
dep.getLazyUntil() === Dependency.LAZY_UNTIL_REQUEST) &&
354+
dep.category === esmDependencyCategory &&
355+
"setLazy" in dep &&
356+
dep.getResourceIdentifier() !== null
357+
) {
358+
const factory = dependencyFactories.get(
359+
/** @type {DependencyConstructor} */ (dep.constructor)
360+
);
361+
if (factory !== undefined) {
362+
const resourceIdent = dep.getResourceIdentifier();
363+
const requestKey = /** @type {string} */ (resourceIdent);
364+
if (info === undefined) info = new LazyBarrelInfo();
365+
dep.setLazy(true);
366+
info.addLazy(requestKey, dep, factory, dep.getContext());
367+
info.addOrdinary(requestKey);
368+
hasOrdinary = true;
369+
}
370+
}
371+
}
372+
}
293373
// info is only created once a deferrable target exists, so it is never empty here
294374
if (info === undefined) {
295375
if (state !== undefined) modules.delete(module);
296376
return false;
297377
}
298378
// terminals only matter together with a star re-export (to avoid building it
299379
// for a locally-provided name); skip the extra pass otherwise
300-
if (hasFallback) {
380+
if (hasFallback || hasOrdinary) {
301381
for (const dep of dependencies) {
302382
if (
303383
"getLazyUntil" in dep &&
@@ -341,8 +421,28 @@ class LazyBarrelController {
341421
const modules = this._modules;
342422
const state = modules.get(module);
343423

424+
const getRequestedIds = () => {
425+
const directIds = getForwardedIds(dependencies);
426+
if (directIds !== true && directIds.size !== 0) return directIds;
427+
for (const dependency of dependencies) {
428+
if (
429+
"getLazyUntil" in dependency &&
430+
dependency.getLazyUntil() === Dependency.LAZY_UNTIL_FALLBACK
431+
) {
432+
const parent =
433+
this._compilation.moduleGraph.getParentModule(dependency);
434+
const parentState =
435+
parent === undefined ? undefined : modules.get(parent);
436+
if (parentState !== undefined) {
437+
return parentState.forwardedIds;
438+
}
439+
}
440+
}
441+
if (directIds === true || directIds.size !== 0) return directIds;
442+
return directIds;
443+
};
344444
if (state === undefined) {
345-
const forwardedIds = getForwardedIds(dependencies);
445+
const forwardedIds = getRequestedIds();
346446
if (forwardedIds !== true && forwardedIds.size === 0) return;
347447
modules.set(module, {
348448
forwardedIds,
@@ -351,7 +451,7 @@ class LazyBarrelController {
351451
return;
352452
}
353453

354-
const forwardedIds = getForwardedIds(dependencies);
454+
const forwardedIds = getRequestedIds();
355455
if (forwardedIds !== true && forwardedIds.size === 0) return;
356456
if (state.forwardedIds !== true) {
357457
if (forwardedIds === true) state.forwardedIds = true;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from "./used.js";
2+
export * from "./unused.js";
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function deep() {
2+
return "unused";
3+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { used } from "./barrel.js";
2+
3+
it("should keep the requested export and its ordinary import working", () => {
4+
expect(used()).toBe("used");
5+
});
6+
7+
it("should only expand the ordinary imports of requested star exports when sideEffects is enabled", () => {
8+
const expected = [
9+
"index.js",
10+
"barrel.js",
11+
"used.js",
12+
"unused.js",
13+
"used-helper.js"
14+
];
15+
if (!BUILD_REPORT.sideEffects) expected.push("large-subtree.js", "deep.js");
16+
expect(BUILD_REPORT.modules).toEqual(expected.sort());
17+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { deep } from "./deep.js";
2+
3+
export function expensiveHelper() {
4+
return deep();
5+
}
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: 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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { expensiveHelper } from "./large-subtree.js";
2+
3+
export function unused() {
4+
return expensiveHelper();
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function usedHelper() {
2+
return "used";
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { usedHelper } from "./used-helper.js";
2+
3+
export function used() {
4+
return usedHelper();
5+
}

0 commit comments

Comments
 (0)