Skip to content

Commit dbb4605

Browse files
fix: keep lazy barrel deferral per compilation (#22676)
* fix: keep lazy barrel deferral per compilation * test: cover lazy barrel deferral across rebuilds in both directions and with filesystem cache --------- Co-authored-by: alexander-akait <[email protected]>
1 parent 1a6127d commit dbb4605

47 files changed

Lines changed: 294 additions & 319 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.changeset/021-lazy-barrel-requested-imports.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"webpack": patch
33
---
44

5-
Make lazy barrels defer their import declarations, not only re-exports.
5+
Make lazy barrels defer import declarations and keep no deferral across compilations.

‎lib/Compilation.js‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2012,15 +2012,7 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
20122012
*/
20132013
const processDependency = (dep, index) => {
20142014
this.moduleGraph.setParents(dep, currentBlock, module, index);
2015-
if (
2016-
hasLazyBarrel &&
2017-
// TODO in the next major release: remove
2018-
// It may be missing on custom dependency types not extending the base Dependency
2019-
"isLazy" in dep &&
2020-
dep.isLazy()
2021-
) {
2022-
return;
2023-
}
2015+
if (hasLazyBarrel && this._lazyBarrels.isLazy(dep)) return;
20242016

20252017
if (this._unsafeCache) {
20262018
try {

‎lib/dependencies/esm/ESMExportExpressionDependency.js‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const { ExportBindingType } = ESMExportInitFragment;
2121
const DEFAULT_EXPORT_NAME = "default";
2222

2323
/** @import { ReplaceSource } from "webpack-sources" */
24-
/** @import { ExportsSpec, LazyUntil, UpdateHashContext } from "../../graph/Dependency" */
24+
/** @import { ExportsSpec, LazyRole, UpdateHashContext } from "../../graph/Dependency" */
2525
/** @import Module, { BuildInfo } from "../../module/Module" */
2626
/** @import Hash from "../../util/Hash" */
2727
/** @import { DependencyTemplateContext } from "../../template/DependencyTemplate" */
@@ -68,17 +68,17 @@ class ESMExportExpressionDependency extends NullDependency {
6868

6969
/**
7070
* Returns how this dependency may be deferred when its parent module is side-effect-free (lazy barrel optimization).
71-
* @returns {LazyUntil | null} lazy classification, null when it must be processed eagerly
71+
* @returns {LazyRole | null} lazy classification, null when it must be processed eagerly
7272
*/
73-
getLazyUntil() {
74-
return Dependency.LAZY_UNTIL_LOCAL;
73+
get lazyRole() {
74+
return Dependency.LAZY_LOCAL_EXPORT;
7575
}
7676

7777
/**
78-
* Returns the export name for a `LAZY_UNTIL_LOCAL`/`LAZY_UNTIL_ID` classification (lazy barrel optimization).
78+
* Returns the export name for a `LAZY_LOCAL_EXPORT`/`LAZY_NAMED_REEXPORT` classification (lazy barrel optimization).
7979
* @returns {string | null} export name, null when not applicable
8080
*/
81-
getLazyName() {
81+
get lazyName() {
8282
return DEFAULT_EXPORT_NAME;
8383
}
8484

‎lib/dependencies/esm/ESMExportImportedSideEffectDependency.js‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const Dependency = require("../../graph/Dependency");
99
const makeSerializable = require("../../util/makeSerializable");
1010
const ESMImportSideEffectDependency = require("./ESMImportSideEffectDependency");
1111

12-
/** @import { LazyUntil } from "../../graph/Dependency" */
12+
/** @import { LazyRole } from "../../graph/Dependency" */
1313

1414
/**
1515
* The evaluation of an `export … from "..."` statement's module. It is a separate
@@ -19,10 +19,10 @@ const ESMImportSideEffectDependency = require("./ESMImportSideEffectDependency")
1919
class ESMExportImportedSideEffectDependency extends ESMImportSideEffectDependency {
2020
/**
2121
* 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
22+
* @returns {LazyRole | null} lazy classification, null when it must be processed eagerly
2323
*/
24-
getLazyUntil() {
25-
return Dependency.LAZY_UNTIL_REQUEST;
24+
get lazyRole() {
25+
return Dependency.LAZY_REEXPORT_SIDE_EFFECT;
2626
}
2727
}
2828

‎lib/dependencies/esm/ESMExportImportedSpecifierDependency.js‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const { ExportBindingType } = ESMExportInitFragment;
4444
* @import {
4545
* ExportsSpec,
4646
* GetConditionFn,
47-
* LazyUntil,
47+
* LazyRole,
4848
* RawReferencedExports,
4949
* ReferencedExports,
5050
* TRANSITIVE,
@@ -631,25 +631,25 @@ class ESMExportImportedSpecifierDependency extends ESMImportDependency {
631631
* Returns the export name this dependency requests from its target module (lazy barrel optimization).
632632
* @returns {string | true | null} export name, true for all exports, null for none
633633
*/
634-
getForwardId() {
634+
get forwardId() {
635635
return this.ids.length > 0 ? this.ids[0] : true;
636636
}
637637

638638
/**
639639
* Returns how this dependency may be deferred when its parent module is side-effect-free (lazy barrel optimization).
640-
* @returns {LazyUntil | null} lazy classification, null when it must be processed eagerly
640+
* @returns {LazyRole | null} lazy classification, null when it must be processed eagerly
641641
*/
642-
getLazyUntil() {
642+
get lazyRole() {
643643
return this.name !== null
644-
? Dependency.LAZY_UNTIL_ID
645-
: Dependency.LAZY_UNTIL_FALLBACK;
644+
? Dependency.LAZY_NAMED_REEXPORT
645+
: Dependency.LAZY_STAR_REEXPORT;
646646
}
647647

648648
/**
649-
* Returns the export name for a `LAZY_UNTIL_LOCAL`/`LAZY_UNTIL_ID` classification (lazy barrel optimization).
649+
* Returns the export name for a `LAZY_LOCAL_EXPORT`/`LAZY_NAMED_REEXPORT` classification (lazy barrel optimization).
650650
* @returns {string | null} export name, null when not applicable
651651
*/
652-
getLazyName() {
652+
get lazyName() {
653653
return this.name;
654654
}
655655

‎lib/dependencies/esm/ESMExportSpecifierDependency.js‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const ESMExportInitFragment = require("./ESMExportInitFragment");
1414
const { ExportBindingType } = ESMExportInitFragment;
1515

1616
/** @import { ReplaceSource } from "webpack-sources" */
17-
/** @import { ExportsSpec, LazyUntil, UpdateHashContext } from "../../graph/Dependency" */
17+
/** @import { ExportsSpec, LazyRole, UpdateHashContext } from "../../graph/Dependency" */
1818
/** @import Module, { BuildInfo } from "../../module/Module" */
1919
/** @import Hash from "../../util/Hash" */
2020
/** @import { DependencyTemplateContext } from "../../template/DependencyTemplate" */
@@ -53,17 +53,17 @@ class ESMExportSpecifierDependency extends NullDependency {
5353

5454
/**
5555
* Returns how this dependency may be deferred when its parent module is side-effect-free (lazy barrel optimization).
56-
* @returns {LazyUntil | null} lazy classification, null when it must be processed eagerly
56+
* @returns {LazyRole | null} lazy classification, null when it must be processed eagerly
5757
*/
58-
getLazyUntil() {
59-
return Dependency.LAZY_UNTIL_LOCAL;
58+
get lazyRole() {
59+
return Dependency.LAZY_LOCAL_EXPORT;
6060
}
6161

6262
/**
63-
* Returns the export name for a `LAZY_UNTIL_LOCAL`/`LAZY_UNTIL_ID` classification (lazy barrel optimization).
63+
* Returns the export name for a `LAZY_LOCAL_EXPORT`/`LAZY_NAMED_REEXPORT` classification (lazy barrel optimization).
6464
* @returns {string | null} export name, null when not applicable
6565
*/
66-
getLazyName() {
66+
get lazyName() {
6767
return this.name;
6868
}
6969

‎lib/dependencies/esm/ESMImportDependency.js‎

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const AwaitDependenciesInitFragment = require("../../async-modules/AwaitDependen
99
const isGeneratorLowered = require("../../async-modules/isGeneratorLowered");
1010
const ESMLinkingError = require("../../errors/ESMLinkingError");
1111
const Dependency = require("../../graph/Dependency");
12+
const LazyBarrels = require("../../optimize/LazyBarrels");
1213
const ConditionalInitFragment = require("../../template/ConditionalInitFragment");
1314
const InitFragment = require("../../template/InitFragment");
1415
const Template = require("../../template/Template");
@@ -22,7 +23,7 @@ const ModuleDependency = require("../core/ModuleDependency");
2223
* JavascriptParserOptions
2324
* } from "../../../declarations/WebpackOptions"
2425
*/
25-
/** @import { ReferencedExports, DependencyKinds } from "../../graph/Dependency" */
26+
/** @import { ReferencedExports, DependencyKinds, LazyRole } from "../../graph/Dependency" */
2627
/** @import { DependencyTemplateContext } from "../../template/DependencyTemplate" */
2728
/** @import ExportsInfo from "../../graph/ExportsInfo" */
2829
/** @import Module, { BuildMeta } from "../../module/Module" */
@@ -100,29 +101,22 @@ const getNonOptionalPart = (members, membersOptionals) => {
100101
/**
101102
* Export names a lazy barrel still defers, so they are missing from its provided
102103
* exports without being absent from the module.
104+
* @param {ModuleGraph} moduleGraph the module graph
103105
* @param {Module} module the imported module
104106
* @returns {Set<string> | true | undefined} deferred names, true when a deferred star re-export may provide any name, undefined when none
105107
*/
106-
const getDeferredExports = (module) => {
108+
const getDeferredExports = (moduleGraph, module) => {
107109
const factoryMeta = module.factoryMeta;
108110
if (factoryMeta === undefined || !factoryMeta.sideEffectFree) return;
109111
/** @type {Set<string> | undefined} */
110112
let names;
111113
for (const dep of module.dependencies) {
112-
// TODO in the next major release: remove
113-
// These may be missing on custom dependency types not extending the base Dependency
114-
if (
115-
typeof dep.getLazyUntil !== "function" ||
116-
typeof dep.isLazy !== "function"
117-
) {
118-
continue;
119-
}
120-
if (!dep.isLazy()) continue;
121-
const until = dep.getLazyUntil();
122-
if (until === Dependency.LAZY_UNTIL_FALLBACK) return true;
123-
if (until !== Dependency.LAZY_UNTIL_ID) continue;
114+
if (!LazyBarrels.isLazy(moduleGraph, dep)) continue;
115+
const role = dep.lazyRole;
116+
if (role === Dependency.LAZY_STAR_REEXPORT) return true;
117+
if (role !== Dependency.LAZY_NAMED_REEXPORT) continue;
124118
if (names === undefined) names = new Set();
125-
names.add(/** @type {string} */ (dep.getLazyName()));
119+
names.add(/** @type {string} */ (dep.lazyName));
126120
}
127121
return names;
128122
};
@@ -146,28 +140,18 @@ class ESMImportDependency extends ModuleDependency {
146140
this.phase = phase;
147141
/** @type {ImportAttributes | undefined} */
148142
this.attributes = attributes;
149-
/** @type {boolean} */
150-
this._lazyMake = false;
151143
}
152144

153145
get category() {
154146
return "esm";
155147
}
156148

157149
/**
158-
* Whether the lazy barrel currently defers creating this dependency's target module (lazy barrel optimization).
159-
* @returns {boolean} true while deferred, otherwise false
160-
*/
161-
isLazy() {
162-
return this._lazyMake;
163-
}
164-
165-
/**
166-
* Sets whether the lazy barrel defers creating this dependency's target module (lazy barrel optimization).
167-
* @param {boolean} value true to defer, false to create it now
150+
* Returns how this dependency may be deferred when its parent module is side-effect-free (lazy barrel optimization).
151+
* @returns {LazyRole | null} lazy classification, null when it must be processed eagerly
168152
*/
169-
setLazy(value) {
170-
this._lazyMake = value;
153+
get lazyRole() {
154+
return Dependency.LAZY_IMPORT_DECLARATION;
171155
}
172156

173157
/**
@@ -318,7 +302,7 @@ class ESMImportDependency extends ModuleDependency {
318302
(exportsType !== "default-with-named" || ids[0] !== "default") &&
319303
moduleGraph.isExportProvided(importedModule, ids) === false
320304
) {
321-
const deferredExports = getDeferredExports(importedModule);
305+
const deferredExports = getDeferredExports(moduleGraph, importedModule);
322306

323307
if (
324308
deferredExports !== true &&
@@ -459,7 +443,7 @@ ESMImportDependency.Template = class ESMImportDependencyTemplate extends (
459443

460444
const connection = moduleGraph.getConnection(dep);
461445
// deferred by lazy barrel: never resolved, must not render a missing module
462-
if (connection === undefined && dep.isLazy()) {
446+
if (connection === undefined && LazyBarrels.isLazy(moduleGraph, dep)) {
463447
return;
464448
}
465449
if (connection && !connection.isTargetActive(runtime)) return;

‎lib/dependencies/esm/ESMImportSideEffectDependency.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ class ESMImportSideEffectDependency extends ESMImportDependency {
158158
* Returns the export name this dependency requests from its target module (lazy barrel optimization).
159159
* @returns {string | true | null} export name, true for all exports, null for none
160160
*/
161-
getForwardId() {
161+
get forwardId() {
162162
return null;
163163
}
164164

‎lib/dependencies/esm/ESMImportSpecifierDependency.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ class ESMImportSpecifierDependency extends ESMImportDependency {
173173
* Returns the export name this dependency requests from its target module (lazy barrel optimization).
174174
* @returns {string | true | null} export name, true for all exports, null for none
175175
*/
176-
getForwardId() {
176+
get forwardId() {
177177
return this.ids.length > 0 ? this.ids[0] : true;
178178
}
179179

‎lib/graph/Dependency.js‎

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -135,17 +135,19 @@ const memoize = require("../util/memoize");
135135

136136
/**
137137
* Lazy barrel classification of a dependency within a side-effect-free module.
138-
* `LAZY_UNTIL_LOCAL`: locally provided export name (`getLazyName`), requesting it requires no dependency.
139-
* `LAZY_UNTIL_ID`: named re-export (`getLazyName`) deferred until the export name is requested.
140-
* `LAZY_UNTIL_FALLBACK`: star re-export, deferred until an unknown name or all names are requested.
141-
* `LAZY_UNTIL_REQUEST`: deferred together with other dependencies of the same request.
142-
* @typedef {"local" | "id" | "*" | "@"} LazyUntil
138+
* `LAZY_LOCAL_EXPORT`: locally provided export name (`lazyName`), requesting it requires no dependency.
139+
* `LAZY_NAMED_REEXPORT`: named re-export (`lazyName`) deferred until the export name is requested.
140+
* `LAZY_STAR_REEXPORT`: star re-export, deferred until an unknown name or all names are requested.
141+
* `LAZY_REEXPORT_SIDE_EFFECT`: evaluation of the module an `export … from` names, deferred together with its re-exports.
142+
* `LAZY_IMPORT_DECLARATION`: import declaration, deferred while only re-exported names are requested.
143+
* @typedef {"local" | "id" | "*" | "@" | "import"} LazyRole
143144
*/
144145

145-
const LAZY_UNTIL_LOCAL = /** @type {"local"} */ ("local");
146-
const LAZY_UNTIL_ID = /** @type {"id"} */ ("id");
147-
const LAZY_UNTIL_FALLBACK = /** @type {"*"} */ ("*");
148-
const LAZY_UNTIL_REQUEST = /** @type {"@"} */ ("@");
146+
const LAZY_LOCAL_EXPORT = /** @type {"local"} */ ("local");
147+
const LAZY_NAMED_REEXPORT = /** @type {"id"} */ ("id");
148+
const LAZY_STAR_REEXPORT = /** @type {"*"} */ ("*");
149+
const LAZY_REEXPORT_SIDE_EFFECT = /** @type {"@"} */ ("@");
150+
const LAZY_IMPORT_DECLARATION = /** @type {"import"} */ ("import");
149151

150152
/**
151153
* The category used by dependencies created from ECMAScript module syntax.
@@ -388,41 +390,27 @@ class Dependency {
388390
* Returns the export name this dependency requests from its target module (lazy barrel optimization).
389391
* @returns {string | true | null} export name, true for all exports, null for none
390392
*/
391-
getForwardId() {
393+
get forwardId() {
392394
// unknown dependency types conservatively request all exports
393395
return true;
394396
}
395397

396398
/**
397399
* Returns how this dependency may be deferred when its parent module is side-effect-free (lazy barrel optimization).
398-
* @returns {LazyUntil | null} lazy classification, null when it must be processed eagerly
400+
* @returns {LazyRole | null} lazy classification, null when it must be processed eagerly
399401
*/
400-
getLazyUntil() {
402+
get lazyRole() {
401403
return null;
402404
}
403405

404406
/**
405-
* Returns the export name for a `LAZY_UNTIL_LOCAL`/`LAZY_UNTIL_ID` classification (lazy barrel optimization).
407+
* Returns the export name for a `LAZY_LOCAL_EXPORT`/`LAZY_NAMED_REEXPORT` classification (lazy barrel optimization).
406408
* @returns {string | null} export name, null when not applicable
407409
*/
408-
getLazyName() {
410+
get lazyName() {
409411
return null;
410412
}
411413

412-
/**
413-
* Whether the lazy barrel currently defers creating this dependency's target module (lazy barrel optimization).
414-
* @returns {boolean} true while deferred, otherwise false
415-
*/
416-
isLazy() {
417-
return false;
418-
}
419-
420-
/**
421-
* Sets whether the lazy barrel defers creating this dependency's target module (lazy barrel optimization).
422-
* @param {boolean} value true to defer, false to create it now
423-
*/
424-
setLazy(value) {}
425-
426414
/**
427415
* Returns the referenced module and export
428416
* @deprecated
@@ -625,10 +613,11 @@ Object.defineProperty(Dependency.prototype, "disconnect", {
625613
});
626614

627615
Dependency.TRANSITIVE = TRANSITIVE;
628-
Dependency.LAZY_UNTIL_LOCAL = LAZY_UNTIL_LOCAL;
629-
Dependency.LAZY_UNTIL_ID = LAZY_UNTIL_ID;
630-
Dependency.LAZY_UNTIL_FALLBACK = LAZY_UNTIL_FALLBACK;
631-
Dependency.LAZY_UNTIL_REQUEST = LAZY_UNTIL_REQUEST;
616+
Dependency.LAZY_LOCAL_EXPORT = LAZY_LOCAL_EXPORT;
617+
Dependency.LAZY_NAMED_REEXPORT = LAZY_NAMED_REEXPORT;
618+
Dependency.LAZY_STAR_REEXPORT = LAZY_STAR_REEXPORT;
619+
Dependency.LAZY_REEXPORT_SIDE_EFFECT = LAZY_REEXPORT_SIDE_EFFECT;
620+
Dependency.LAZY_IMPORT_DECLARATION = LAZY_IMPORT_DECLARATION;
632621
Dependency.ESM_CATEGORY = ESM_CATEGORY;
633622

634623
/** @type {Set<keyof DependencyKinds>} */

0 commit comments

Comments
 (0)