Skip to content

Commit a4f1777

Browse files
committed
fix dependOn behavior with runtimes, fix runtime passing by entry
1 parent 838f135 commit a4f1777

File tree

16 files changed

+296
-69
lines changed

16 files changed

+296
-69
lines changed

lib/EntryOptionPlugin.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class EntryOptionPlugin {
4848
const options = {
4949
name,
5050
filename: desc.filename,
51+
runtime: desc.runtime,
5152
dependOn: desc.dependOn,
5253
library: desc.library
5354
};

lib/ExportsInfo.js

Lines changed: 69 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77

88
const SortableSet = require("./util/SortableSet");
99
const makeSerializable = require("./util/makeSerializable");
10+
const { forEachRuntime } = require("./util/runtime");
1011

1112
/** @typedef {import("./Dependency").RuntimeSpec} RuntimeSpec */
13+
/** @typedef {import("./util/Hash")} Hash */
1214

1315
/** @typedef {typeof UsageState.OnlyPropertiesUsed | typeof UsageState.NoInfo | typeof UsageState.Unknown | typeof UsageState.Used} RuntimeUsageStateType */
1416
/** @typedef {typeof UsageState.Unused | RuntimeUsageStateType} UsageStateType */
@@ -282,7 +284,7 @@ class ExportsInfo {
282284
}
283285

284286
/**
285-
* @param {string} runtime the runtime
287+
* @param {RuntimeSpec} runtime the runtime
286288
* @returns {boolean} true, when something changed
287289
*/
288290
setUsedInUnknownWay(runtime) {
@@ -315,7 +317,7 @@ class ExportsInfo {
315317
}
316318

317319
/**
318-
* @param {string} runtime the runtime
320+
* @param {RuntimeSpec} runtime the runtime
319321
* @returns {boolean} true, when something changed
320322
*/
321323
setUsedWithoutInfo(runtime) {
@@ -342,7 +344,7 @@ class ExportsInfo {
342344
}
343345

344346
/**
345-
* @param {string} runtime the runtime
347+
* @param {RuntimeSpec} runtime the runtime
346348
* @returns {boolean} true, when something changed
347349
*/
348350
setAllKnownExportsUsed(runtime) {
@@ -356,7 +358,7 @@ class ExportsInfo {
356358
}
357359

358360
/**
359-
* @param {string} runtime the runtime
361+
* @param {RuntimeSpec} runtime the runtime
360362
* @returns {boolean} true, when something changed
361363
*/
362364
setUsedForSideEffectsOnly(runtime) {
@@ -367,6 +369,10 @@ class ExportsInfo {
367369
);
368370
}
369371

372+
/**
373+
* @param {RuntimeSpec} runtime the runtime
374+
* @returns {boolean} true, when the module is used in any way
375+
*/
370376
isUsed(runtime) {
371377
if (this._redirectTo !== undefined) {
372378
if (this._redirectTo.isUsed(runtime)) {
@@ -388,6 +394,10 @@ class ExportsInfo {
388394
return false;
389395
}
390396

397+
/**
398+
* @param {RuntimeSpec} runtime the runtime
399+
* @returns {SortableSet<string> | boolean | null} set of used exports, or true (when namespace object is used), or false (when unused), or null (when unknown)
400+
*/
391401
getUsedExports(runtime) {
392402
if (!this._redirectTo !== undefined) {
393403
switch (this._otherExportsInfo.getUsed(runtime)) {
@@ -473,6 +483,10 @@ class ExportsInfo {
473483
return array;
474484
}
475485

486+
/**
487+
* @param {RuntimeSpec} runtime the runtime
488+
* @returns {boolean} true, when the module has enough information to create a list of exports
489+
*/
476490
hasStaticExportsList(runtime) {
477491
if (
478492
this._redirectTo !== undefined &&
@@ -610,6 +624,11 @@ class ExportsInfo {
610624
}
611625
}
612626

627+
/**
628+
* @param {Hash} hash the hash
629+
* @param {RuntimeSpec} runtime the runtime
630+
* @returns {void}
631+
*/
613632
updateHash(hash, runtime) {
614633
for (const exportInfo of this.orderedExports) {
615634
exportInfo.updateHash(hash, runtime);
@@ -759,6 +778,10 @@ class ExportInfo {
759778
);
760779
}
761780

781+
/**
782+
* @param {RuntimeSpec} runtime only apply to this runtime
783+
* @returns {boolean} true, when something changed
784+
*/
762785
setUsedInUnknownWay(runtime) {
763786
let changed = false;
764787
if (
@@ -777,6 +800,10 @@ class ExportInfo {
777800
return changed;
778801
}
779802

803+
/**
804+
* @param {RuntimeSpec} runtime only apply to this runtime
805+
* @returns {boolean} true, when something changed
806+
*/
780807
setUsedWithoutInfo(runtime) {
781808
let changed = false;
782809
if (this.setUsed(UsageState.NoInfo, runtime)) {
@@ -804,27 +831,35 @@ class ExportInfo {
804831
/**
805832
* @param {function(UsageStateType): boolean} condition compare with old value
806833
* @param {UsageStateType} newValue set when condition is true
807-
* @param {string} runtime only apply to this runtime
834+
* @param {RuntimeSpec} runtime only apply to this runtime
808835
* @returns {boolean} true when something has changed
809836
*/
810837
setUsedConditionally(condition, newValue, runtime) {
811838
if (this._usedInRuntime === undefined) {
812839
if (newValue !== UsageState.Unused && condition(UsageState.Unused)) {
813840
this._usedInRuntime = new Map();
814-
this._usedInRuntime.set(runtime, newValue);
841+
forEachRuntime(runtime, runtime =>
842+
this._usedInRuntime.set(runtime, newValue)
843+
);
815844
return true;
816845
}
817846
} else {
818-
/** @type {UsageStateType} */
819-
let oldValue = this._usedInRuntime.get(runtime);
820-
if (oldValue === undefined) oldValue = UsageState.Unused;
821-
if (newValue !== oldValue && condition(oldValue)) {
822-
if (newValue === UsageState.Unused) {
823-
this._usedInRuntime.delete(runtime);
824-
if (this._usedInRuntime.size === 0) this._usedInRuntime = undefined;
825-
} else {
826-
this._usedInRuntime.set(runtime, newValue);
847+
let changed = false;
848+
forEachRuntime(runtime, runtime => {
849+
/** @type {UsageStateType} */
850+
let oldValue = this._usedInRuntime.get(runtime);
851+
if (oldValue === undefined) oldValue = UsageState.Unused;
852+
if (newValue !== oldValue && condition(oldValue)) {
853+
if (newValue === UsageState.Unused) {
854+
this._usedInRuntime.delete(runtime);
855+
} else {
856+
this._usedInRuntime.set(runtime, newValue);
857+
}
858+
changed = true;
827859
}
860+
});
861+
if (changed) {
862+
if (this._usedInRuntime.size === 0) this._usedInRuntime = undefined;
828863
return true;
829864
}
830865
}
@@ -833,27 +868,35 @@ class ExportInfo {
833868

834869
/**
835870
* @param {UsageStateType} newValue new value of the used state
836-
* @param {string} runtime only apply to this runtime
871+
* @param {RuntimeSpec} runtime only apply to this runtime
837872
* @returns {boolean} true when something has changed
838873
*/
839874
setUsed(newValue, runtime) {
840875
if (this._usedInRuntime === undefined) {
841876
if (newValue !== UsageState.Unused) {
842877
this._usedInRuntime = new Map();
843-
this._usedInRuntime.set(runtime, newValue);
878+
forEachRuntime(runtime, runtime =>
879+
this._usedInRuntime.set(runtime, newValue)
880+
);
844881
return true;
845882
}
846883
} else {
847-
/** @type {UsageStateType} */
848-
let oldValue = this._usedInRuntime.get(runtime);
849-
if (oldValue === undefined) oldValue = UsageState.Unused;
850-
if (newValue !== oldValue) {
851-
if (newValue === UsageState.Unused) {
852-
this._usedInRuntime.delete(runtime);
853-
if (this._usedInRuntime.size === 0) this._usedInRuntime = undefined;
854-
} else {
855-
this._usedInRuntime.set(runtime, newValue);
884+
let changed = false;
885+
forEachRuntime(runtime, runtime => {
886+
/** @type {UsageStateType} */
887+
let oldValue = this._usedInRuntime.get(runtime);
888+
if (oldValue === undefined) oldValue = UsageState.Unused;
889+
if (newValue !== oldValue) {
890+
if (newValue === UsageState.Unused) {
891+
this._usedInRuntime.delete(runtime);
892+
} else {
893+
this._usedInRuntime.set(runtime, newValue);
894+
}
895+
changed = true;
856896
}
897+
});
898+
if (changed) {
899+
if (this._usedInRuntime.size === 0) this._usedInRuntime = undefined;
857900
return true;
858901
}
859902
}

lib/FlagAllModulesAsUsedPlugin.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55

66
"use strict";
77

8-
const { getEntryRuntime } = require("./util/runtime");
8+
const { getEntryRuntime, mergeRuntimeOwned } = require("./util/runtime");
99

1010
/** @typedef {import("./Compiler")} Compiler */
11+
/** @typedef {import("./util/runtime").RuntimeSpec} RuntimeSpec */
1112

1213
class FlagAllModulesAsUsedPlugin {
1314
constructor(explanation) {
@@ -27,14 +28,17 @@ class FlagAllModulesAsUsedPlugin {
2728
compilation.hooks.optimizeDependencies.tap(
2829
"FlagAllModulesAsUsedPlugin",
2930
modules => {
30-
const runtimes = new Set();
31+
/** @type {RuntimeSpec} */
32+
let runtime = undefined;
3133
for (const [name, { options }] of compilation.entries) {
32-
runtimes.add(getEntryRuntime(compilation, name, options));
34+
runtime = mergeRuntimeOwned(
35+
runtime,
36+
getEntryRuntime(compilation, name, options)
37+
);
3338
}
3439
for (const module of modules) {
3540
const exportsInfo = moduleGraph.getExportsInfo(module);
36-
for (const runtime of runtimes)
37-
exportsInfo.setUsedInUnknownWay(runtime);
41+
exportsInfo.setUsedInUnknownWay(runtime);
3842
moduleGraph.addExtraReason(module, this.explanation);
3943
}
4044
}

lib/FlagDependencyUsagePlugin.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ const Dependency = require("./Dependency");
99
const { UsageState } = require("./ExportsInfo");
1010
const { STAGE_DEFAULT } = require("./OptimizationStages");
1111
const TupleQueue = require("./util/TupleQueue");
12-
const { getEntryRuntime, mergeRuntime } = require("./util/runtime");
12+
const {
13+
getEntryRuntime,
14+
mergeRuntime,
15+
mergeRuntimeOwned
16+
} = require("./util/runtime");
1317

1418
/** @typedef {import("./Chunk")} Chunk */
1519
/** @typedef {import("./ChunkGroup")} ChunkGroup */
@@ -52,13 +56,13 @@ class FlagDependencyUsagePlugin {
5256
/** @type {Map<ExportsInfo, Module>} */
5357
const exportInfoToModuleMap = new Map();
5458

55-
/** @type {TupleQueue<[Module, string]>} */
59+
/** @type {TupleQueue<[Module, RuntimeSpec]>} */
5660
const queue = new TupleQueue();
5761

5862
/**
5963
* @param {Module} module module to process
6064
* @param {(string[] | ReferencedExport)[]} usedExports list of used exports
61-
* @param {string} runtime part of which runtime
65+
* @param {RuntimeSpec} runtime part of which runtime
6266
* @returns {void}
6367
*/
6468
const processReferencedModule = (module, usedExports, runtime) => {
@@ -151,7 +155,7 @@ class FlagDependencyUsagePlugin {
151155

152156
/**
153157
* @param {Module} module the module
154-
* @param {string} runtime part of which runtime
158+
* @param {RuntimeSpec} runtime part of which runtime
155159
* @returns {void}
156160
*/
157161
const processModule = (module, runtime) => {
@@ -257,7 +261,7 @@ class FlagDependencyUsagePlugin {
257261

258262
/**
259263
* @param {Dependency} dep dependency
260-
* @param {string} runtime runtime
264+
* @param {RuntimeSpec} runtime runtime
261265
*/
262266
const processEntryDependency = (dep, runtime) => {
263267
const module = moduleGraph.getModule(dep);
@@ -266,7 +270,8 @@ class FlagDependencyUsagePlugin {
266270
queue.enqueue(module, runtime);
267271
}
268272
};
269-
const runtimes = new Set();
273+
/** @type {RuntimeSpec} */
274+
let globalRuntime = undefined;
270275
for (const [
271276
entryName,
272277
{ dependencies: deps, includeDependencies: includeDeps, options }
@@ -280,15 +285,13 @@ class FlagDependencyUsagePlugin {
280285
for (const dep of includeDeps) {
281286
processEntryDependency(dep, runtime);
282287
}
283-
runtimes.add(runtime);
288+
globalRuntime = mergeRuntimeOwned(globalRuntime, runtime);
284289
}
285-
for (const runtime of runtimes) {
286-
for (const dep of compilation.globalEntry.dependencies) {
287-
processEntryDependency(dep, runtime);
288-
}
289-
for (const dep of compilation.globalEntry.includeDependencies) {
290-
processEntryDependency(dep, runtime);
291-
}
290+
for (const dep of compilation.globalEntry.dependencies) {
291+
processEntryDependency(dep, globalRuntime);
292+
}
293+
for (const dep of compilation.globalEntry.includeDependencies) {
294+
processEntryDependency(dep, globalRuntime);
292295
}
293296

294297
while (queue.length) {

lib/config/normalization.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ const getNormalizedEntryStatic = entry => {
371371
value.import &&
372372
(Array.isArray(value.import) ? value.import : [value.import]),
373373
filename: value.filename,
374+
runtime: value.runtime,
374375
dependOn:
375376
value.dependOn &&
376377
(Array.isArray(value.dependOn) ? value.dependOn : [value.dependOn]),

lib/optimize/ModuleConcatenationPlugin.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const ModuleHotAcceptDependency = require("../dependencies/ModuleHotAcceptDepend
1818
const ModuleHotDeclineDependency = require("../dependencies/ModuleHotDeclineDependency");
1919
const StackedMap = require("../util/StackedMap");
2020
const { compareModulesByIdentifier } = require("../util/comparators");
21-
const { mergeRuntime, intersectRuntime } = require("../util/runtime");
21+
const { intersectRuntime, mergeRuntimeOwned } = require("../util/runtime");
2222
const ConcatenatedModule = require("./ConcatenatedModule");
2323

2424
/** @typedef {import("../Compilation")} Compilation */
@@ -207,7 +207,7 @@ class ModuleConcatenationPlugin {
207207

208208
let runtime = undefined;
209209
for (const r of chunkGraph.getModuleRuntimes(currentRoot)) {
210-
runtime = mergeRuntime(runtime, r);
210+
runtime = mergeRuntimeOwned(runtime, r);
211211
}
212212

213213
// create a configuration with the root
@@ -547,7 +547,7 @@ class ModuleConcatenationPlugin {
547547
// We don't care for connections from other runtimes
548548
let originRuntime = undefined;
549549
for (const r of chunkGraph.getModuleRuntimes(connection.originModule)) {
550-
originRuntime = mergeRuntime(originRuntime, r);
550+
originRuntime = mergeRuntimeOwned(originRuntime, r);
551551
}
552552

553553
return intersectRuntime(runtime, originRuntime);

0 commit comments

Comments
 (0)