Skip to content

Commit 10efec5

Browse files
committed
fix: preserve lazy barrel forwarding state
1 parent e9e02fb commit 10efec5

5 files changed

Lines changed: 43 additions & 38 deletions

File tree

‎lib/optimize/LazyBarrel.js‎

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const Dependency = require("../graph/Dependency");
1717
* @property {ModuleFactory} factory factory for the request
1818
* @property {Dependency[]} dependencies deferred dependencies of the request
1919
* @property {string | undefined} context request context
20+
* @property {boolean} resolved whether the group was already activated
2021
*/
2122

2223
/**
@@ -56,14 +57,14 @@ function getForwardedIds(dependencies) {
5657
}
5758

5859
/**
59-
* Tracks the deferred (not yet factorized/built) dependencies of a
60-
* side-effect-free barrel module and which export names resolve to them.
60+
* Tracks the deferred dependencies of a side-effect-free barrel module and
61+
* keeps the export forwarding needed after those dependencies are resolved.
6162
*/
6263
class LazyBarrelInfo {
6364
constructor() {
6465
/** @type {Map<string, string>} forward id -> request key */
6566
this._forwardIdToRequest = new Map();
66-
/** @type {Map<string, DependencyGroup>} request key -> still-deferred group */
67+
/** @type {Map<string, DependencyGroup>} request key -> dependency group */
6768
this._requestToDepGroup = new Map();
6869
/** @type {Set<string> | undefined} locally provided export names */
6970
this._terminalIds = undefined;
@@ -83,7 +84,7 @@ class LazyBarrelInfo {
8384
addLazy(requestKey, dependency, factory, context) {
8485
let group = this._requestToDepGroup.get(requestKey);
8586
if (group === undefined) {
86-
group = { factory, dependencies: [], context };
87+
group = { factory, dependencies: [], context, resolved: false };
8788
this._requestToDepGroup.set(requestKey, group);
8889
}
8990
if (!group.dependencies.includes(dependency)) {
@@ -136,26 +137,20 @@ class LazyBarrelInfo {
136137
* @returns {boolean} true, when no dependency is deferred
137138
*/
138139
isEmpty() {
139-
return this._requestToDepGroup.size === 0;
140+
for (const group of this._requestToDepGroup.values()) {
141+
if (!group.resolved) return false;
142+
}
143+
return true;
140144
}
141145

142146
/**
143147
* Returns the requests of the still-lazy groups.
144148
* @returns {Iterable<string>} still-lazy requests
145149
*/
146-
getLazyRequests() {
147-
return this._requestToDepGroup.keys();
148-
}
149-
150-
/**
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;
150+
*getLazyRequests() {
151+
for (const [requestKey, group] of this._requestToDepGroup) {
152+
if (!group.resolved) yield requestKey;
153+
}
159154
}
160155

161156
/**
@@ -175,10 +170,23 @@ class LazyBarrelInfo {
175170
}
176171
result.push(group);
177172
};
173+
const returned = new Set();
174+
/**
175+
* @param {string} requestKey request key to take or forward
176+
*/
177+
const take = (requestKey) => {
178+
const group = this._requestToDepGroup.get(requestKey);
179+
if (group === undefined || returned.has(group)) return;
180+
returned.add(group);
181+
if (!group.resolved) {
182+
group.resolved = true;
183+
unlazy(group);
184+
} else {
185+
result.push(group);
186+
}
187+
};
178188
if (forwardedIds === true) {
179-
for (const group of this._requestToDepGroup.values()) unlazy(group);
180-
this._requestToDepGroup.clear();
181-
this._release();
189+
for (const requestKey of this._requestToDepGroup.keys()) take(requestKey);
182190
return result;
183191
}
184192
let requestOrdinary = false;
@@ -190,15 +198,6 @@ class LazyBarrelInfo {
190198
}
191199
}
192200
}
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-
};
202201
for (const id of forwardedIds) {
203202
if (this._terminalIds !== undefined && this._terminalIds.has(id)) {
204203
continue;
@@ -214,7 +213,6 @@ class LazyBarrelInfo {
214213
if (requestOrdinary && this._ordinaryRequests !== undefined) {
215214
for (const requestKey of this._ordinaryRequests) take(requestKey);
216215
}
217-
if (this._requestToDepGroup.size === 0) this._release();
218216
return result;
219217
}
220218
}
@@ -391,10 +389,9 @@ class LazyBarrelController {
391389
// barrel classified after its targets were already requested: replay the
392390
// recorded ids so processDependency un-lazies and builds those targets now
393391
info.request(state.forwardedIds);
394-
// stay lazy only while some targets remain deferred
395-
if (info.isEmpty()) return false;
396392
state.lazyBarrelInfo = info;
397-
return true;
393+
// stay lazy only while some targets remain deferred
394+
return !info.isEmpty();
398395
}
399396

400397
modules.set(module, {
@@ -461,8 +458,6 @@ class LazyBarrelController {
461458
// Pending requests will be replayed during `classify` later
462459
if (info === undefined) return;
463460
const groups = info.request(forwardedIds);
464-
// drop the per-module state once every deferred target has been built
465-
if (info.isEmpty()) state.lazyBarrelInfo = undefined;
466461
if (groups.length === 0) return;
467462
return groups.map((group) => ({
468463
factory: group.factory,
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
import { used } from "./barrel.js";
22

3+
import("./later.js").then(({ unused: unused2 }) => {
4+
console.log(used, unused2);
5+
});
6+
37
it("should keep the requested export and its ordinary import working", () => {
48
expect(used()).toBe("used");
59
});
610

7-
it("should only expand the ordinary imports of requested star exports when sideEffects is enabled", () => {
11+
it("should activate ordinary imports reached through nested star re-exports", () => {
812
const expected = [
913
"index.js",
1014
"barrel.js",
15+
"later.js",
16+
"later-1.js",
17+
"later-2.js",
1118
"used.js",
1219
"unused.js",
1320
"used-helper.js"
1421
];
15-
if (!BUILD_REPORT.sideEffects) expected.push("large-subtree.js", "deep.js");
22+
expected.push("large-subtree.js", "deep.js");
1623
expect(BUILD_REPORT.modules).toEqual(expected.sort());
1724
});
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";

0 commit comments

Comments
 (0)