Skip to content

Commit e3b249f

Browse files
authored
fix: preserve lazy barrel forwarding state (#22334)
1 parent e95be18 commit e3b249f

8 files changed

Lines changed: 73 additions & 45 deletions

File tree

‎lib/dependencies/esm/ESMImportDependency.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ class HarmonyImportDependency extends ModuleDependency {
152152
this.phase = phase;
153153
/** @type {ImportAttributes | undefined} */
154154
this.attributes = attributes;
155-
/** @type {boolean} */
156-
this._lazyMake = false;
155+
/** @type {boolean | undefined} */
156+
this._lazyMake = undefined;
157157
}
158158

159159
get category() {
@@ -162,7 +162,7 @@ class HarmonyImportDependency extends ModuleDependency {
162162

163163
/**
164164
* Whether the lazy barrel currently defers creating this dependency's target module (lazy barrel optimization).
165-
* @returns {boolean} true while deferred, so it must not be processed or rendered
165+
* @returns {boolean | undefined} true while deferred, false when activated, undefined before classification
166166
*/
167167
isLazy() {
168168
return this._lazyMake;

‎lib/graph/Dependency.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ class Dependency {
342342

343343
/**
344344
* Whether the lazy barrel currently defers creating this dependency's target module (lazy barrel optimization).
345-
* @returns {boolean} true while deferred, so it must not be processed or rendered
345+
* @returns {boolean | undefined} true while deferred, false when activated, undefined before classification
346346
*/
347347
isLazy() {
348348
return false;

‎lib/optimize/LazyBarrel.js‎

Lines changed: 55 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,17 @@ function getForwardedIds(dependencies) {
5656
}
5757

5858
/**
59-
* Tracks the deferred (not yet factorized/built) dependencies of a
60-
* side-effect-free barrel module and which export names resolve to them.
59+
* Tracks the deferred dependencies of a side-effect-free barrel module and
60+
* keeps the export forwarding needed after those dependencies are resolved.
6161
*/
6262
class LazyBarrelInfo {
6363
constructor() {
6464
/** @type {Map<string, string>} forward id -> request key */
6565
this._forwardIdToRequest = new Map();
66-
/** @type {Map<string, DependencyGroup>} request key -> still-deferred group */
66+
/** @type {Map<string, DependencyGroup>} request key -> dependency group */
6767
this._requestToDepGroup = new Map();
68+
/** @type {Map<string, DependencyGroup>} request key -> activated group */
69+
this._resolvedRequestToDepGroup = new Map();
6870
/** @type {Set<string> | undefined} locally provided export names */
6971
this._terminalIds = undefined;
7072
/** @type {Set<string> | undefined} requests of star re-exports */
@@ -131,6 +133,16 @@ class LazyBarrelInfo {
131133
this._ordinaryRequests.add(requestKey);
132134
}
133135

136+
/**
137+
* Releases forwarding state when no ordinary import needs to be revisited.
138+
*/
139+
_release() {
140+
this._forwardIdToRequest.clear();
141+
this._terminalIds = undefined;
142+
this._fallbackRequests = undefined;
143+
this._ordinaryRequests = undefined;
144+
}
145+
134146
/**
135147
* Returns whether nothing is deferred.
136148
* @returns {boolean} true, when no dependency is deferred
@@ -148,37 +160,48 @@ class LazyBarrelInfo {
148160
}
149161

150162
/**
151-
* Drops the export-name lookups once no group stays deferred; they only serve
152-
* to find still-deferred groups, so keeping them per module wastes memory.
153-
*/
154-
_release() {
155-
this._forwardIdToRequest.clear();
156-
this._terminalIds = undefined;
157-
this._fallbackRequests = undefined;
158-
this._ordinaryRequests = undefined;
159-
}
160-
161-
/**
162-
* Removes and returns the groups needed to provide the requested export names.
163+
* Returns and activates the groups needed to provide the requested export names.
163164
* @param {Set<string> | true} forwardedIds requested export names, true for all
164165
* @returns {DependencyGroup[]} groups that must be processed now
165166
*/
166167
request(forwardedIds) {
167168
/** @type {DependencyGroup[]} */
168169
const result = [];
170+
const returned = new Set();
169171
/**
170-
* @param {DependencyGroup} group taken group
172+
* @param {string} requestKey request key to take or forward
171173
*/
172-
const unlazy = (group) => {
173-
for (const dependency of group.dependencies) {
174-
dependency.setLazy(false);
174+
const take = (requestKey) => {
175+
const group = this._requestToDepGroup.get(requestKey);
176+
if (group !== undefined) {
177+
if (returned.has(group)) return;
178+
returned.add(group);
179+
this._requestToDepGroup.delete(requestKey);
180+
for (const dependency of group.dependencies) {
181+
dependency.setLazy(false);
182+
}
183+
if (this._ordinaryRequests !== undefined) {
184+
this._resolvedRequestToDepGroup.set(requestKey, group);
185+
}
186+
result.push(group);
187+
return;
175188
}
176-
result.push(group);
189+
const resolvedGroup = this._resolvedRequestToDepGroup.get(requestKey);
190+
if (resolvedGroup === undefined || returned.has(resolvedGroup)) return;
191+
returned.add(resolvedGroup);
192+
result.push(resolvedGroup);
177193
};
178194
if (forwardedIds === true) {
179-
for (const group of this._requestToDepGroup.values()) unlazy(group);
180-
this._requestToDepGroup.clear();
181-
this._release();
195+
for (const requestKey of this._requestToDepGroup.keys()) take(requestKey);
196+
for (const requestKey of this._resolvedRequestToDepGroup.keys()) {
197+
take(requestKey);
198+
}
199+
if (
200+
this._ordinaryRequests === undefined &&
201+
this._requestToDepGroup.size === 0
202+
) {
203+
this._release();
204+
}
182205
return result;
183206
}
184207
let requestOrdinary = false;
@@ -190,15 +213,6 @@ class LazyBarrelInfo {
190213
}
191214
}
192215
}
193-
/**
194-
* @param {string} requestKey request key to take
195-
*/
196-
const take = (requestKey) => {
197-
const group = this._requestToDepGroup.get(requestKey);
198-
if (group === undefined) return;
199-
this._requestToDepGroup.delete(requestKey);
200-
unlazy(group);
201-
};
202216
for (const id of forwardedIds) {
203217
if (this._terminalIds !== undefined && this._terminalIds.has(id)) {
204218
continue;
@@ -214,7 +228,12 @@ class LazyBarrelInfo {
214228
if (requestOrdinary && this._ordinaryRequests !== undefined) {
215229
for (const requestKey of this._ordinaryRequests) take(requestKey);
216230
}
217-
if (this._requestToDepGroup.size === 0) this._release();
231+
if (
232+
this._ordinaryRequests === undefined &&
233+
this._requestToDepGroup.size === 0
234+
) {
235+
this._release();
236+
}
218237
return result;
219238
}
220239
}
@@ -359,6 +378,7 @@ class LazyBarrelController {
359378
if (factory !== undefined) {
360379
const resourceIdent = dep.getResourceIdentifier();
361380
const requestKey = /** @type {string} */ (resourceIdent);
381+
if (dep.isLazy() === false) continue;
362382
if (info === undefined) info = new LazyBarrelInfo();
363383
dep.setLazy(true);
364384
info.addLazy(requestKey, dep, factory, dep.getContext());
@@ -389,10 +409,9 @@ class LazyBarrelController {
389409
// barrel classified after its targets were already requested: replay the
390410
// recorded ids so processDependency un-lazies and builds those targets now
391411
info.request(state.forwardedIds);
392-
// stay lazy only while some targets remain deferred
393-
if (info.isEmpty()) return false;
394412
state.lazyBarrelInfo = info;
395-
return true;
413+
// stay lazy only while some targets remain deferred
414+
return !info.isEmpty();
396415
}
397416

398417
modules.set(module, {
@@ -459,8 +478,6 @@ class LazyBarrelController {
459478
// Pending requests will be replayed during `classify` later
460479
if (info === undefined) return;
461480
const groups = info.request(forwardedIds);
462-
// drop the per-module state once every deferred target has been built
463-
if (info.isEmpty()) state.lazyBarrelInfo = undefined;
464481
if (groups.length === 0) return;
465482
return groups.map((group) => ({
466483
factory: group.factory,
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
import { used } from "./barrel.js";
22

3+
it("should build the ordinary imports of reexport requested after the barrel was built", () =>
4+
import("./later.js").then(({ unused }) => {
5+
expect(unused()).toBe("unused");
6+
}));
7+
38
it("should keep the requested export and its ordinary import working", () => {
49
expect(used()).toBe("used");
510
});
611

7-
it("should only expand the ordinary imports of requested star exports when sideEffects is enabled", () => {
12+
it("should activate ordinary imports reached through nested star re-exports", () => {
813
const expected = [
914
"index.js",
1015
"barrel.js",
16+
"later.js",
17+
"later-1.js",
18+
"later-2.js",
1119
"used.js",
1220
"unused.js",
1321
"used-helper.js"
1422
];
15-
if (!BUILD_REPORT.sideEffects) expected.push("large-subtree.js", "deep.js");
23+
expected.push("large-subtree.js", "deep.js");
1624
expect(BUILD_REPORT.modules).toEqual(expected.sort());
1725
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { unused } from "./later-2.js";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { unused } from "./barrel.js";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { unused } from "./later-1.js";

‎types.d.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6824,7 +6824,7 @@ declare class Dependency {
68246824
/**
68256825
* Whether the lazy barrel currently defers creating this dependency's target module (lazy barrel optimization).
68266826
*/
6827-
isLazy(): boolean;
6827+
isLazy(): undefined | boolean;
68286828

68296829
/**
68306830
* Sets whether the lazy barrel defers creating this dependency's target module (lazy barrel optimization).

0 commit comments

Comments
 (0)