Repository navigation
Expand file tree
/
Copy pathLazyBarrel.js
More file actions
388 lines (358 loc) · 12.8 KB
/
Copy pathLazyBarrel.js
File metadata and controls
388 lines (358 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Haijie Xie @hai-x
*/
"use strict";
const Dependency = require("./Dependency");
/** @import Compilation, { DependencyConstructor } from "./Compilation" */
/** @import Module from "./Module" */
/** @import ModuleFactory from "./ModuleFactory" */
/**
* Defines the deferred request group type used by this module.
* @typedef {object} DependencyGroup
* @property {ModuleFactory} factory factory for the request
* @property {Dependency[]} dependencies deferred dependencies of the request
* @property {string | undefined} context request context
*/
/**
* Defines the lazy barrel state type used by this module.
* @typedef {object} LazyBarrelState
* @property {Set<string> | true} forwardedIds export names requested so far, true for all
* @property {LazyBarrelInfo | undefined} lazyBarrelInfo deferred dependencies, undefined until classified
*/
/**
* Defines the lazy barrel unlazy item type used by this module.
* @typedef {object} UnlazyDependencyInfo
* @property {ModuleFactory} factory factory for the request
* @property {Dependency[]} dependencies deferred dependencies of the request
* @property {string | undefined} context request context
* @property {Module} originModule the lazy barrel module
*/
/**
* Collects the export names a dependency group requests from its target.
* @param {Dependency[]} dependencies dependency group resolving to one module
* @returns {Set<string> | true} requested export names, true for all
*/
function getForwardedIds(dependencies) {
/** @type {Set<string>} */
const ids = new Set();
for (const dependency of dependencies) {
// TODO remove in webpack 6
// It may be missing on custom dependency types not extending the base Dependency
if (!("getForwardId" in dependency)) continue;
const id = dependency.getForwardId();
if (id === true) return true;
if (id !== null) ids.add(id);
}
return ids;
}
/**
* Tracks the deferred (not yet factorized/built) dependencies of a
* side-effect-free barrel module and which export names resolve to them.
*/
class LazyBarrelInfo {
constructor() {
/** @type {Map<string, string>} forward id -> request key */
this._forwardIdToRequest = new Map();
/** @type {Map<string, DependencyGroup>} request key -> still-deferred group */
this._requestToDepGroup = new Map();
/** @type {Set<string> | undefined} locally provided export names */
this._terminalIds = undefined;
/** @type {Set<string> | undefined} requests of star re-exports */
this._fallbackRequests = undefined;
}
/**
* Records a deferred dependency under its request key.
* @param {string} requestKey request key
* @param {Dependency} dependency dependency to defer
* @param {ModuleFactory} factory factory for the request
* @param {string | undefined} context request context
*/
addLazy(requestKey, dependency, factory, context) {
let group = this._requestToDepGroup.get(requestKey);
if (group === undefined) {
group = { factory, dependencies: [], context };
this._requestToDepGroup.set(requestKey, group);
}
group.dependencies.push(dependency);
}
/**
* Maps an export name to the request providing it.
* @param {string} id export name
* @param {string} requestKey request key
*/
addForwardId(id, requestKey) {
this._forwardIdToRequest.set(id, requestKey);
}
/**
* Registers a locally provided export name.
* @param {string} id export name
*/
addTerminal(id) {
if (this._terminalIds === undefined) this._terminalIds = new Set();
this._terminalIds.add(id);
}
/**
* Registers a star re-export request key.
* @param {string} requestKey request key
*/
addFallback(requestKey) {
if (this._fallbackRequests === undefined) {
this._fallbackRequests = new Set();
}
this._fallbackRequests.add(requestKey);
}
/**
* Returns whether nothing is deferred.
* @returns {boolean} true, when no dependency is deferred
*/
isEmpty() {
return this._requestToDepGroup.size === 0;
}
/**
* Returns the requests of the still-lazy groups.
* @returns {Iterable<string>} still-lazy requests
*/
getLazyRequests() {
return this._requestToDepGroup.keys();
}
/**
* Drops the export-name lookups once no group stays deferred; they only serve
* to find still-deferred groups, so keeping them per module wastes memory.
*/
_release() {
this._forwardIdToRequest.clear();
this._terminalIds = undefined;
this._fallbackRequests = undefined;
}
/**
* Removes and returns the groups needed to provide the requested export names.
* @param {Set<string> | true} forwardedIds requested export names, true for all
* @returns {DependencyGroup[]} groups that must be processed now
*/
request(forwardedIds) {
/** @type {DependencyGroup[]} */
const result = [];
/**
* @param {DependencyGroup} group taken group
*/
const unlazy = (group) => {
for (const dependency of group.dependencies) {
dependency.setLazy(false);
}
result.push(group);
};
if (forwardedIds === true) {
for (const group of this._requestToDepGroup.values()) unlazy(group);
this._requestToDepGroup.clear();
this._release();
return result;
}
/**
* @param {string} requestKey request key to take
*/
const take = (requestKey) => {
const group = this._requestToDepGroup.get(requestKey);
if (group === undefined) return;
this._requestToDepGroup.delete(requestKey);
unlazy(group);
};
for (const id of forwardedIds) {
if (this._terminalIds !== undefined && this._terminalIds.has(id)) {
continue;
}
const requestKey = this._forwardIdToRequest.get(id);
if (requestKey !== undefined) {
take(requestKey);
} else if (this._fallbackRequests !== undefined) {
// unknown name: any star re-export may provide it
for (const fallbackKey of this._fallbackRequests) take(fallbackKey);
}
}
if (this._requestToDepGroup.size === 0) this._release();
return result;
}
}
const esmDependencyCategory = "esm";
/**
* Owns the per side-effect-free module lazy barrel state for a `Compilation`,
* keeping the deferral bookkeeping out of `Compilation` itself.
*/
class LazyBarrelController {
/**
* @param {Compilation} compilation the owning compilation
*/
constructor(compilation) {
/** @type {Compilation} */
this._compilation = compilation;
/** @type {WeakMap<Module, LazyBarrelState>} */
this._modules = new WeakMap();
}
/**
* Releases all per-module deferral state. The last consumer is
* `FlagDependencyExportsPlugin`'s cache key during `finishModules`, so the
* bookkeeping is dead weight once sealing starts.
*/
clear() {
this._modules = new WeakMap();
}
/**
* requests of the re-export targets `module` still keeps lazy.
* `FlagDependencyExportsPlugin` folds them into its provided-exports cache
* key, so a barrel whose built targets differ from a cached run does not
* reuse that run's stale exports.
* @param {Module} module the module
* @returns {Iterable<string> | undefined} still-lazy requests, if any
*/
getLazyRequests(module) {
const factoryMeta = module.factoryMeta;
if (factoryMeta === undefined || !factoryMeta.sideEffectFree) return;
const state = this._modules.get(module);
if (state === undefined) return;
const info = state.lazyBarrelInfo;
if (info === undefined || info.isEmpty()) return;
return info.getLazyRequests();
}
/**
* Classifies the re-export dependencies of a side-effect-free module (lazy barrel)
* into deferrable groups and marks the deferred ones.
* @param {Module} module the module whose dependencies are processed
* @returns {boolean} true, when some dependencies were deferred
*/
classify(module) {
const modules = this._modules;
const factoryMeta = module.factoryMeta;
if (factoryMeta === undefined || !factoryMeta.sideEffectFree) {
return false;
}
const dependencies = module.dependencies;
const dependencyFactories = this._compilation.dependencyFactories;
/** @type {LazyBarrelInfo | undefined} */
let info;
let hasFallback = false;
for (const dep of dependencies) {
// TODO remove in webpack 6
// It may be missing on custom dependency types not extending the base Dependency
if (!("getLazyUntil" in dep)) continue;
const until = dep.getLazyUntil();
// null (eager) and LAZY_UNTIL_LOCAL (terminal) defer nothing; terminals are
// only consulted alongside a star re-export, so they are recorded below
if (until === null || until === Dependency.LAZY_UNTIL_LOCAL) continue;
// deferrable: setLazy is needed to toggle its deferred state
if (!("setLazy" in dep)) continue;
const resourceIdent = dep.getResourceIdentifier();
if (resourceIdent === null) continue;
const factory = dependencyFactories.get(
/** @type {DependencyConstructor} */ (dep.constructor)
);
if (factory === undefined) continue;
const category = dep.category;
// Match the request grouping key of `processDependencyForResolving`
const requestKey =
category === esmDependencyCategory
? resourceIdent
: `${category}${resourceIdent}`;
if (info === undefined) info = new LazyBarrelInfo();
if (dep.isLazy()) {
info.addLazy(requestKey, dep, factory, dep.getContext());
}
if (until === Dependency.LAZY_UNTIL_ID) {
info.addForwardId(
/** @type {string} */ (dep.getLazyName()),
requestKey
);
} else if (until === Dependency.LAZY_UNTIL_FALLBACK) {
info.addFallback(requestKey);
hasFallback = true;
}
}
const state = modules.get(module);
// info is only created once a deferrable target exists, so it is never empty here
if (info === undefined) {
if (state !== undefined) modules.delete(module);
return false;
}
// terminals only matter together with a star re-export (to avoid building it
// for a locally-provided name); skip the extra pass otherwise
if (hasFallback) {
for (const dep of dependencies) {
if (
"getLazyUntil" in dep &&
dep.getLazyUntil() === Dependency.LAZY_UNTIL_LOCAL
) {
info.addTerminal(/** @type {string} */ (dep.getLazyName()));
}
}
}
if (state !== undefined) {
// barrel classified after its targets were already requested: replay the
// recorded ids so processDependency un-lazies and builds those targets now
info.request(state.forwardedIds);
// stay lazy only while some targets remain deferred
if (info.isEmpty()) return false;
state.lazyBarrelInfo = info;
return true;
}
modules.set(module, {
forwardedIds: new Set(),
lazyBarrelInfo: info
});
return true;
}
/**
* Requests the export names that `dependencies` need from a lazy barrel and
* returns the deferred re-export targets that must be built now. Records the
* request when the barrel's dependencies were not classified yet.
* @param {Module} module the resolved module
* @param {Dependency[]} dependencies the dependencies that resolved to the module
* @returns {UnlazyDependencyInfo[] | undefined} items to process, if any
*/
request(module, dependencies) {
// state only exists for side-effect-free modules, so a non-side-effect-free
// module never has state — skip the WeakMap lookup entirely for it
const factoryMeta = module.factoryMeta;
if (factoryMeta === undefined || !factoryMeta.sideEffectFree) return;
const modules = this._modules;
const state = modules.get(module);
if (state === undefined) {
const forwardedIds = getForwardedIds(dependencies);
if (forwardedIds !== true && forwardedIds.size === 0) return;
modules.set(module, {
forwardedIds,
lazyBarrelInfo: undefined
});
return;
}
const forwardedIds = getForwardedIds(dependencies);
if (forwardedIds !== true && forwardedIds.size === 0) return;
if (state.forwardedIds !== true) {
if (forwardedIds === true) state.forwardedIds = true;
else for (const id of forwardedIds) state.forwardedIds.add(id);
}
const info = state.lazyBarrelInfo;
// Pending requests will be replayed during `classify` later
if (info === undefined) return;
const groups = info.request(forwardedIds);
// drop the per-module state once every deferred target has been built
if (info.isEmpty()) state.lazyBarrelInfo = undefined;
if (groups.length === 0) return;
return groups.map((group) => ({
factory: group.factory,
dependencies: group.dependencies,
context: group.context,
originModule: module
}));
}
/**
* Queues the deferred dependency groups of a lazy barrel requested by a single dependency.
* @param {Module} module the resolved module of the dependency
* @param {Dependency} dependency the requesting dependency
* @param {{ factory: ModuleFactory, dependencies: Dependency[], context: string | undefined, originModule: Module | null }[]} sortedDependencies item list to append to
*/
unlazyForDependency(module, dependency, sortedDependencies) {
const unlazyItems = this.request(module, [dependency]);
if (unlazyItems === undefined) return;
for (const item of unlazyItems) sortedDependencies.push(item);
}
}
module.exports = LazyBarrelController;