Skip to content

Commit c877316

Browse files
committed
fix: reset lazy barrel deferral in every compilation
1 parent cb47bb9 commit c877316

23 files changed

Lines changed: 188 additions & 102 deletions

‎lib/dependencies/esm/ESMExportDependencyParserPlugin.js‎

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ const {
1818
} = require("../core/ImportPhase");
1919
const ESMExportExpressionDependency = require("./ESMExportExpressionDependency");
2020
const ESMExportHeaderDependency = require("./ESMExportHeaderDependency");
21+
const ESMExportImportedSideEffectDependency = require("./ESMExportImportedSideEffectDependency");
2122
const ESMExportImportedSpecifierDependency = require("./ESMExportImportedSpecifierDependency");
2223
const ESMExportSpecifierDependency = require("./ESMExportSpecifierDependency");
2324
const { ExportPresenceModes } = require("./ESMImportDependency");
2425
const { harmonySpecifierTag } = require("./ESMImportDependencyParserPlugin");
25-
const ESMImportSideEffectDependency = require("./ESMImportSideEffectDependency");
2626

2727
/**
2828
* @import {
@@ -98,18 +98,13 @@ module.exports = class HarmonyExportDependencyParserPlugin {
9898
error.loc = parser.getLocation(statement) || undefined;
9999
parser.state.current.addError(error);
100100
}
101-
const sideEffectDep = new ESMImportSideEffectDependency(
101+
const sideEffectDep = new ESMExportImportedSideEffectDependency(
102102
/** @type {string} */ (source),
103103
parser.state.lastHarmonyImportOrder,
104104
phase,
105105
getImportAttributes(statement)
106106
);
107107
sideEffectDep.setLocWithIndex(parser.getLocation(statement), -1);
108-
// lazy barrel: defer at parse time when the importing module is side-effect-free
109-
const moduleFactoryMeta = parser.state.module.factoryMeta;
110-
if (moduleFactoryMeta !== undefined && moduleFactoryMeta.sideEffectFree) {
111-
sideEffectDep.setLazy(true);
112-
}
113108
parser.state.current.addDependency(sideEffectDep);
114109
return true;
115110
});
@@ -153,14 +148,6 @@ module.exports = class HarmonyExportDependencyParserPlugin {
153148
settings.attributes
154149
);
155150

156-
// lazy barrel: defer the re-export at parse time when the importing module is side-effect-free
157-
const moduleFactoryMeta = parser.state.module.factoryMeta;
158-
if (
159-
moduleFactoryMeta !== undefined &&
160-
moduleFactoryMeta.sideEffectFree
161-
) {
162-
dep.setLazy(true);
163-
}
164151
dep.setLocWithIndex(parser.getLocation(statement), -1);
165152
parser.state.current.addDependency(dep);
166153

@@ -292,15 +279,6 @@ module.exports = class HarmonyExportDependencyParserPlugin {
292279
name,
293280
tagData ? tagData.value || null : undefined
294281
);
295-
// lazy barrel: defer the re-export at parse time when the importing module is side-effect-free
296-
const moduleFactoryMeta = parser.state.module.factoryMeta;
297-
if (
298-
settings &&
299-
moduleFactoryMeta !== undefined &&
300-
moduleFactoryMeta.sideEffectFree
301-
) {
302-
dep.setLazy(true);
303-
}
304282
dep.setLocWithIndex(
305283
parser.getLocation(statement),
306284
/** @type {number} */ (idx)
@@ -348,14 +326,6 @@ module.exports = class HarmonyExportDependencyParserPlugin {
348326
if (harmonyStarExports) {
349327
harmonyStarExports.push(dep);
350328
}
351-
// lazy barrel: defer the re-export at parse time when the importing module is side-effect-free
352-
const moduleFactoryMeta = parser.state.module.factoryMeta;
353-
if (
354-
moduleFactoryMeta !== undefined &&
355-
moduleFactoryMeta.sideEffectFree
356-
) {
357-
dep.setLazy(true);
358-
}
359329
dep.setLocWithIndex(
360330
parser.getLocation(statement),
361331
/** @type {number} */ (idx)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Haijie Xie @hai-x
4+
*/
5+
6+
"use strict";
7+
8+
const Dependency = require("../../graph/Dependency");
9+
const makeSerializable = require("../../util/makeSerializable");
10+
const ESMImportSideEffectDependency = require("./ESMImportSideEffectDependency");
11+
12+
/** @import { LazyUntil } from "../../graph/Dependency" */
13+
14+
/**
15+
* The evaluation of an `export … from "..."` statement's module. It is a separate
16+
* class rather than a flag so a lazy barrel defers it with the re-export, while an
17+
* import declaration's evaluation stays tied to the barrel's own code.
18+
*/
19+
class ESMExportImportedSideEffectDependency extends ESMImportSideEffectDependency {
20+
/**
21+
* Returns how this dependency may be deferred when its parent module is side-effect-free (lazy barrel optimization).
22+
* @returns {LazyUntil | null} lazy classification, null when it must be processed eagerly
23+
*/
24+
getLazyUntil() {
25+
return Dependency.LAZY_UNTIL_REQUEST;
26+
}
27+
}
28+
29+
makeSerializable(
30+
ESMExportImportedSideEffectDependency,
31+
"webpack/lib/dependencies/esm/ESMExportImportedSideEffectDependency"
32+
);
33+
34+
ESMExportImportedSideEffectDependency.Template =
35+
ESMImportSideEffectDependency.Template;
36+
37+
module.exports = ESMExportImportedSideEffectDependency;

‎lib/dependencies/esm/ESMImportDependency.js‎

Lines changed: 3 additions & 5 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 | undefined} */
156-
this._lazyMake = undefined;
155+
/** @type {boolean} */
156+
this._lazyMake = false;
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 | undefined} true while deferred, false when activated, undefined before classification
165+
* @returns {boolean} true while deferred, otherwise false
166166
*/
167167
isLazy() {
168168
return this._lazyMake;
@@ -431,7 +431,6 @@ class HarmonyImportDependency extends ModuleDependency {
431431
const { write } = context;
432432
write(this.attributes);
433433
write(this.phase);
434-
write(this._lazyMake);
435434
super.serialize(context);
436435
}
437436

@@ -443,7 +442,6 @@ class HarmonyImportDependency extends ModuleDependency {
443442
const { read } = context;
444443
this.attributes = read();
445444
this.phase = read();
446-
this._lazyMake = read();
447445
super.deserialize(context);
448446
}
449447
}

‎lib/dependencies/esm/ESMImportSideEffectDependency.js‎

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
"use strict";
77

8-
const Dependency = require("../../graph/Dependency");
98
const Module = require("../../module/Module");
109
const { JAVASCRIPT_TYPE } = require("../../module/ModuleSourceTypeConstants");
1110
const InitFragment = require("../../template/InitFragment");
@@ -16,7 +15,7 @@ const ESMImportDependency = require("./ESMImportDependency");
1615
const { ExportPresenceModes } = ESMImportDependency;
1716

1817
/** @import { ReplaceSource } from "webpack-sources" */
19-
/** @import { GetConditionFn, LazyUntil } from "../../graph/Dependency" */
18+
/** @import Dependency, { GetConditionFn } from "../../graph/Dependency" */
2019
/** @import { DependencyTemplateContext } from "../../template/DependencyTemplate" */
2120
/** @import ModuleGraph from "../../graph/ModuleGraph" */
2221
/** @import { ConnectionState } from "../../graph/ModuleGraphConnection" */
@@ -164,14 +163,6 @@ class HarmonyImportSideEffectDependency extends ESMImportDependency {
164163
return null;
165164
}
166165

167-
/**
168-
* Returns how this dependency may be deferred when its parent module is side-effect-free (lazy barrel optimization).
169-
* @returns {LazyUntil | null} lazy classification, null when it must be processed eagerly
170-
*/
171-
getLazyUntil() {
172-
return Dependency.LAZY_UNTIL_REQUEST;
173-
}
174-
175166
/**
176167
* Returns function to determine if the connection is active.
177168
* @param {ModuleGraph} moduleGraph module graph

‎lib/dependencies/esm/ESMModulesPlugin.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const ESMEvaluatedImportSpecifierDependency = require("./ESMEvaluatedImportSpeci
1818
const ESMExportDependencyParserPlugin = require("./ESMExportDependencyParserPlugin");
1919
const ESMExportExpressionDependency = require("./ESMExportExpressionDependency");
2020
const ESMExportHeaderDependency = require("./ESMExportHeaderDependency");
21+
const ESMExportImportedSideEffectDependency = require("./ESMExportImportedSideEffectDependency");
2122
const ESMExportImportedSpecifierDependency = require("./ESMExportImportedSpecifierDependency");
2223
const ESMExportSpecifierDependency = require("./ESMExportSpecifierDependency");
2324
const ESMImportBareSideEffectDependency = require("./ESMImportBareSideEffectDependency");
@@ -94,6 +95,15 @@ class HarmonyModulesPlugin {
9495
new ESMImportBareSideEffectDependency.Template()
9596
);
9697

98+
compilation.dependencyFactories.set(
99+
ESMExportImportedSideEffectDependency,
100+
normalModuleFactory
101+
);
102+
compilation.dependencyTemplates.set(
103+
ESMExportImportedSideEffectDependency,
104+
new ESMExportImportedSideEffectDependency.Template()
105+
);
106+
97107
compilation.dependencyFactories.set(
98108
ESMImportSpecifierDependency,
99109
normalModuleFactory

‎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 | undefined} true while deferred, false when activated, undefined before classification
345+
* @returns {boolean} true while deferred, otherwise false
346346
*/
347347
isLazy() {
348348
return false;

‎lib/optimize/LazyBarrels.js‎

Lines changed: 20 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -82,28 +82,8 @@ const getRequestKey = (dependency) => {
8282
};
8383

8484
/**
85-
* Returns the module requests of the import entries.
86-
* @param {Dependency[]} dependencies dependencies of the barrel
87-
* @returns {Set<string>} import entry requests
88-
*/
89-
const getImportEntryRequests = (dependencies) => {
90-
/** @type {Set<string>} */
91-
const requests = new Set();
92-
for (const dep of dependencies) {
93-
if (
94-
getLazyUntil(dep) !== null ||
95-
dep.category !== Dependency.ESM_CATEGORY
96-
) {
97-
continue;
98-
}
99-
const resourceIdent = dep.getResourceIdentifier();
100-
if (resourceIdent !== null) requests.add(resourceIdent);
101-
}
102-
return requests;
103-
};
104-
105-
/**
106-
* Returns whether import declarations can be deferred: only names not provided locally were requested.
85+
* Returns true if names were requested and none of them is a local export.
86+
* Only local exports use the import declarations, so the barrel can defer them until a local export is requested.
10787
* @param {Set<string> | true | undefined} forwardedIds names requested before classification
10888
* @param {Set<string>} localIds locally provided export names
10989
* @returns {boolean} true, when import declarations can be deferred
@@ -424,11 +404,9 @@ class LazyBarrels {
424404
let info;
425405
/** @type {Set<string>} */
426406
const localIds = new Set();
427-
/** @type {Set<string>} */
428-
const reexportRequests = new Set();
429407
let hasFallbackStarReexport = false;
430408

431-
// re-exports
409+
// re-export starts deferred and this compilation's requests activate the ones it needs
432410
for (const dep of dependencies) {
433411
const until = getLazyUntil(dep);
434412
if (until === Dependency.LAZY_UNTIL_LOCAL) {
@@ -441,50 +419,39 @@ class LazyBarrels {
441419
if (factory === undefined) continue;
442420
const requestKey = getRequestKey(dep);
443421
if (info === undefined) info = new LazyBarrelInfo();
422+
dep.setLazy(true);
444423
if (dep.isLazy()) info.addLazy(requestKey, dep, factory);
445424
if (until === Dependency.LAZY_UNTIL_ID) {
446425
info.addForwardId(
447426
/** @type {string} */ (dep.getLazyName()),
448427
requestKey
449428
);
450-
reexportRequests.add(requestKey);
451429
} else if (until === Dependency.LAZY_UNTIL_FALLBACK) {
452430
info.addFallback(requestKey);
453-
reexportRequests.add(requestKey);
454431
hasFallbackStarReexport = true;
455432
}
456433
}
457434

458435
// import declarations
436+
const deferImports = canDeferImports(forwardedIds, localIds);
459437
/** @type {Set<string> | undefined} */
460438
let importRequests;
461-
if (canDeferImports(forwardedIds, localIds)) {
462-
const importEntryRequests = getImportEntryRequests(dependencies);
463-
for (const dep of dependencies) {
464-
const until = getLazyUntil(dep);
465-
// Only for ESM specifiers (null) and ESM side-effect ("@")
466-
if (
467-
(until !== null && until !== Dependency.LAZY_UNTIL_REQUEST) ||
468-
dep.category !== Dependency.ESM_CATEGORY
469-
) {
470-
continue;
471-
}
472-
const factory = this._getDepFactory(dep);
473-
if (factory === undefined || dep.isLazy() === false) continue;
474-
const requestKey = getRequestKey(dep);
475-
// a re-export's own side-effect import stays in the re-export group
476-
if (
477-
reexportRequests.has(requestKey) &&
478-
!importEntryRequests.has(requestKey)
479-
) {
480-
continue;
481-
}
482-
if (info === undefined) info = new LazyBarrelInfo();
483-
dep.setLazy(true);
484-
info.addLazy(requestKey, dep, factory);
485-
if (importRequests === undefined) importRequests = new Set();
486-
importRequests.add(requestKey);
439+
for (const dep of dependencies) {
440+
if (
441+
getLazyUntil(dep) !== null ||
442+
dep.category !== Dependency.ESM_CATEGORY
443+
) {
444+
continue;
487445
}
446+
const factory = this._getDepFactory(dep);
447+
if (factory === undefined) continue;
448+
dep.setLazy(deferImports);
449+
if (!deferImports) continue;
450+
const requestKey = getRequestKey(dep);
451+
if (info === undefined) info = new LazyBarrelInfo();
452+
if (dep.isLazy()) info.addLazy(requestKey, dep, factory);
453+
if (importRequests === undefined) importRequests = new Set();
454+
importRequests.add(requestKey);
488455
}
489456

490457
if (info === undefined) return;

‎lib/util/internalSerializables.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,8 @@ module.exports = {
325325
require("../dependencies/esm/ESMExportExpressionDependency"),
326326
"dependencies/esm/ESMExportHeaderDependency": () =>
327327
require("../dependencies/esm/ESMExportHeaderDependency"),
328+
"dependencies/esm/ESMExportImportedSideEffectDependency": () =>
329+
require("../dependencies/esm/ESMExportImportedSideEffectDependency"),
328330
"dependencies/esm/ESMExportImportedSpecifierDependency": () =>
329331
require("../dependencies/esm/ESMExportImportedSpecifierDependency"),
330332
"dependencies/esm/ESMExportSpecifierDependency": () =>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { a } from "./pkg";
2+
3+
const built = (file) => STATS_JSON.modules.some((m) => m.name.endsWith(file));
4+
5+
// only a re-export is requested, so pkg's own `import { x }` is deferred too
6+
it("should defer the barrel's import declaration initially", () => {
7+
expect(a).toBe("a");
8+
expect(built("pkg/x.js")).toBe(false);
9+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const a = "a";

0 commit comments

Comments
 (0)