Skip to content

Commit 1f2584e

Browse files
authored
Merge pull request webpack#7408 from webpack/bugfix/side-effects-caching
SideEffectsPlugin don't modify cache entries
2 parents 29cbf98 + ae8d674 commit 1f2584e

23 files changed

+207
-85
lines changed

lib/FlagDependencyUsagePlugin.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const isSubset = (biggerSet, subset) => {
2020
class FlagDependencyUsagePlugin {
2121
apply(compiler) {
2222
compiler.hooks.compilation.tap("FlagDependencyUsagePlugin", compilation => {
23-
compilation.hooks.optimizeModulesAdvanced.tap(
23+
compilation.hooks.optimizeDependencies.tap(
2424
"FlagDependencyUsagePlugin",
2525
modules => {
2626
const processModule = (module, usedExports) => {
@@ -86,9 +86,9 @@ class FlagDependencyUsagePlugin {
8686
}
8787

8888
const queue = [];
89-
for (const chunk of compilation.chunks) {
90-
if (chunk.entryModule) {
91-
processModule(chunk.entryModule, true);
89+
for (const preparedEntrypoint of compilation._preparedEntrypoints) {
90+
if (preparedEntrypoint.module) {
91+
processModule(preparedEntrypoint.module, true);
9292
}
9393
}
9494

lib/RuntimeTemplate.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,16 @@ module.exports = class RuntimeTemplate {
6565
}
6666

6767
moduleId({ module, request }) {
68-
if (!module)
68+
if (!module) {
6969
return this.missingModule({
7070
request
7171
});
72+
}
73+
if (module.id === null) {
74+
throw new Error(
75+
`RuntimeTemplate.moduleId(): Module ${module.identifier()} has no id. This should not happen.`
76+
);
77+
}
7278
return `${this.comment({ request })}${JSON.stringify(module.id)}`;
7379
}
7480

@@ -105,10 +111,16 @@ module.exports = class RuntimeTemplate {
105111
}
106112

107113
moduleNamespacePromise({ block, module, request, message, strict, weak }) {
108-
if (!module)
114+
if (!module) {
109115
return this.missingModulePromise({
110116
request
111117
});
118+
}
119+
if (module.id === null) {
120+
throw new Error(
121+
`RuntimeTemplate.moduleNamespacePromise(): Module ${module.identifier()} has no id. This should not happen.`
122+
);
123+
}
112124
const promise = this.blockPromise({
113125
block,
114126
message
@@ -151,10 +163,16 @@ module.exports = class RuntimeTemplate {
151163
}
152164

153165
importStatement({ update, module, request, importVar, originModule }) {
154-
if (!module)
166+
if (!module) {
155167
return this.missingModuleStatement({
156168
request
157169
});
170+
}
171+
if (module.id === null) {
172+
throw new Error(
173+
`RuntimeTemplate.importStatement(): Module ${module.identifier()} has no id. This should not happen.`
174+
);
175+
}
158176
const comment = this.comment({
159177
request
160178
});

lib/dependencies/HarmonyExportImportedSpecifierDependency.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,27 @@ const HarmonyImportDependency = require("./HarmonyImportDependency");
99
const Template = require("../Template");
1010
const HarmonyLinkingError = require("../HarmonyLinkingError");
1111

12+
/** @typedef {import("../Module")} Module */
13+
14+
/** @typedef {"missing"|"unused"|"empty-star"|"reexport-non-harmony-default"|"reexport-named-default"|"reexport-namespace-object"|"reexport-non-harmony-default-strict"|"reexport-fake-namespace-object"|"rexport-non-harmony-undefined"|"safe-reexport"|"checked-reexport"|"dynamic-reexport"} ExportModeType */
15+
16+
/** @type {Map<string, string>} */
1217
const EMPTY_MAP = new Map();
1318

1419
class ExportMode {
20+
/**
21+
* @param {ExportModeType} type type of the mode
22+
*/
1523
constructor(type) {
24+
/** @type {ExportModeType} */
1625
this.type = type;
26+
/** @type {string|null} */
1727
this.name = null;
28+
/** @type {Map<string, string>} */
1829
this.map = EMPTY_MAP;
30+
/** @type {Module|null} */
1931
this.module = null;
32+
/** @type {string|null} */
2033
this.userRequest = null;
2134
}
2235
}
@@ -51,7 +64,7 @@ class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency {
5164
const name = this.name;
5265
const id = this.id;
5366
const used = this.originModule.isUsed(name);
54-
const importedModule = this.module;
67+
const importedModule = this._module;
5568

5669
if (!importedModule) {
5770
const mode = new ExportMode("missing");
@@ -237,7 +250,7 @@ class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency {
237250
const result = new Set();
238251
// try to learn impossible exports from other star exports with provided exports
239252
for (const otherStarExport of this.otherStarExports) {
240-
const otherImportedModule = otherStarExport.module;
253+
const otherImportedModule = otherStarExport._module;
241254
if (
242255
otherImportedModule &&
243256
Array.isArray(otherImportedModule.buildMeta.providedExports)
@@ -310,7 +323,7 @@ class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency {
310323
}
311324

312325
_getErrors() {
313-
const importedModule = this.module;
326+
const importedModule = this._module;
314327
if (!importedModule) {
315328
return;
316329
}
@@ -354,7 +367,7 @@ class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency {
354367

355368
updateHash(hash) {
356369
super.updateHash(hash);
357-
const hashValue = this.getHashValue(this.module);
370+
const hashValue = this.getHashValue(this._module);
358371
hash.update(hashValue);
359372
}
360373

@@ -387,7 +400,7 @@ HarmonyExportImportedSpecifierDependency.Template = class HarmonyExportImportedS
387400
const used = dep.originModule.isUsed(dep.name);
388401
if (!used) return NaN;
389402
} else {
390-
const importedModule = dep.module;
403+
const importedModule = dep._module;
391404

392405
const activeFromOtherStarExports = dep._discoverActiveExportsFromOtherStartExports();
393406

@@ -424,7 +437,7 @@ HarmonyExportImportedSpecifierDependency.Template = class HarmonyExportImportedS
424437
getContent(dep) {
425438
const mode = dep.getMode(false);
426439
const module = dep.originModule;
427-
const importedModule = dep.module;
440+
const importedModule = dep._module;
428441
const importVar = dep.getImportVar();
429442

430443
switch (mode.type) {

lib/dependencies/HarmonyImportDependency.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,37 @@ const Template = require("../Template");
1111
class HarmonyImportDependency extends ModuleDependency {
1212
constructor(request, originModule, sourceOrder, parserScope) {
1313
super(request);
14+
this.redirectedModule = undefined;
1415
this.originModule = originModule;
1516
this.sourceOrder = sourceOrder;
1617
this.parserScope = parserScope;
1718
}
1819

20+
get _module() {
21+
return this.redirectedModule || this.module;
22+
}
23+
1924
getReference() {
20-
if (!this.module) return null;
21-
return new DependencyReference(this.module, false, this.weak);
25+
if (!this._module) return null;
26+
return new DependencyReference(this._module, false, this.weak);
2227
}
2328

2429
getImportVar() {
2530
let importVarMap = this.parserScope.importVarMap;
2631
if (!importVarMap) this.parserScope.importVarMap = importVarMap = new Map();
27-
let importVar = importVarMap.get(this.module);
32+
let importVar = importVarMap.get(this._module);
2833
if (importVar) return importVar;
2934
importVar = `${Template.toIdentifier(
3035
`${this.userRequest}`
3136
)}__WEBPACK_IMPORTED_MODULE_${importVarMap.size}__`;
32-
importVarMap.set(this.module, importVar);
37+
importVarMap.set(this._module, importVar);
3338
return importVar;
3439
}
3540

3641
getImportStatement(update, runtime) {
3742
return runtime.importStatement({
3843
update,
39-
module: this.module,
44+
module: this._module,
4045
importVar: this.getImportVar(),
4146
request: this.request,
4247
originModule: this.originModule
@@ -45,14 +50,19 @@ class HarmonyImportDependency extends ModuleDependency {
4550

4651
updateHash(hash) {
4752
super.updateHash(hash);
48-
const importedModule = this.module;
53+
const importedModule = this._module;
4954
hash.update(
5055
(importedModule &&
5156
(!importedModule.buildMeta || importedModule.buildMeta.exportsType)) +
5257
""
5358
);
5459
hash.update((importedModule && importedModule.id) + "");
5560
}
61+
62+
disconnect() {
63+
super.disconnect();
64+
this.redirectedModule = undefined;
65+
}
5666
}
5767

5868
module.exports = HarmonyImportDependency;
@@ -71,7 +81,7 @@ HarmonyImportDependency.Template = class HarmonyImportDependencyTemplate {
7181
static isImportEmitted(dep, source) {
7282
let sourceInfo = importEmittedMap.get(source);
7383
if (!sourceInfo) return false;
74-
const key = dep.module || dep.request;
84+
const key = dep._module || dep.request;
7585
return key && sourceInfo.emittedImports.get(key);
7686
}
7787

@@ -85,7 +95,7 @@ HarmonyImportDependency.Template = class HarmonyImportDependencyTemplate {
8595
})
8696
);
8797
}
88-
const key = dep.module || dep.request;
98+
const key = dep._module || dep.request;
8999
if (key && sourceInfo.emittedImports.get(key)) return;
90100
sourceInfo.emittedImports.set(key, true);
91101
const content = dep.getImportStatement(false, runtime);

lib/dependencies/HarmonyImportSideEffectDependency.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class HarmonyImportSideEffectDependency extends HarmonyImportDependency {
1111
}
1212

1313
getReference() {
14-
if (this.module && this.module.factoryMeta.sideEffectFree) return null;
14+
if (this._module && this._module.factoryMeta.sideEffectFree) return null;
1515

1616
return super.getReference();
1717
}
@@ -23,7 +23,7 @@ class HarmonyImportSideEffectDependency extends HarmonyImportDependency {
2323

2424
HarmonyImportSideEffectDependency.Template = class HarmonyImportSideEffectDependencyTemplate extends HarmonyImportDependency.Template {
2525
getHarmonyInitOrder(dep) {
26-
if (dep.module && dep.module.factoryMeta.sideEffectFree) return NaN;
26+
if (dep._module && dep._module.factoryMeta.sideEffectFree) return NaN;
2727
return super.getHarmonyInitOrder(dep);
2828
}
2929
};

lib/dependencies/HarmonyImportSpecifierDependency.js

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
2121
) {
2222
super(request, originModule, sourceOrder, parserScope);
2323
this.id = id === null ? null : `${id}`;
24+
this.redirectedId = undefined;
2425
this.name = name;
2526
this.range = range;
2627
this.strictExportPresence = strictExportPresence;
@@ -35,11 +36,15 @@ class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
3536
return "harmony import specifier";
3637
}
3738

39+
get _id() {
40+
return this.redirectedId || this.id;
41+
}
42+
3843
getReference() {
39-
if (!this.module) return null;
44+
if (!this._module) return null;
4045
return new DependencyReference(
41-
this.module,
42-
this.id && !this.namespaceObjectAsContext ? [this.id] : true,
46+
this._module,
47+
this._id && !this.namespaceObjectAsContext ? [this._id] : true,
4348
false
4449
);
4550
}
@@ -65,7 +70,7 @@ class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
6570
}
6671

6772
_getErrors() {
68-
const importedModule = this.module;
73+
const importedModule = this._module;
6974
if (!importedModule) {
7075
return;
7176
}
@@ -74,11 +79,11 @@ class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
7479
// It's not an harmony module
7580
if (
7681
this.originModule.buildMeta.strictHarmonyModule &&
77-
this.id !== "default"
82+
this._id !== "default"
7883
) {
7984
// In strict harmony modules we only support the default export
80-
const exportName = this.id
81-
? `the named export '${this.id}'`
85+
const exportName = this._id
86+
? `the named export '${this._id}'`
8287
: "the namespace object";
8388
return [
8489
new HarmonyLinkingError(
@@ -89,20 +94,20 @@ class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
8994
return;
9095
}
9196

92-
if (!this.id) {
97+
if (!this._id) {
9398
return;
9499
}
95100

96-
if (importedModule.isProvided(this.id) !== false) {
101+
if (importedModule.isProvided(this._id) !== false) {
97102
// It's provided or we are not sure
98103
return;
99104
}
100105

101106
// We are sure that it's not provided
102107
const idIsNotNameMessage =
103-
this.id !== this.name ? ` (imported as '${this.name}')` : "";
108+
this._id !== this.name ? ` (imported as '${this.name}')` : "";
104109
const errorMessage = `"export '${
105-
this.id
110+
this._id
106111
}'${idIsNotNameMessage} was not found in '${this.userRequest}'`;
107112
return [new HarmonyLinkingError(errorMessage)];
108113
}
@@ -114,10 +119,10 @@ class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
114119

115120
updateHash(hash) {
116121
super.updateHash(hash);
117-
const importedModule = this.module;
118-
hash.update((importedModule && this.id) + "");
122+
const importedModule = this._module;
123+
hash.update((importedModule && this._id) + "");
119124
hash.update(
120-
(importedModule && this.id && importedModule.isUsed(this.id)) + ""
125+
(importedModule && this._id && importedModule.isUsed(this._id)) + ""
121126
);
122127
hash.update(
123128
(importedModule &&
@@ -129,6 +134,11 @@ class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
129134
importedModule.used + JSON.stringify(importedModule.usedExports)) + ""
130135
);
131136
}
137+
138+
disconnect() {
139+
super.disconnect();
140+
this.redirectedId = undefined;
141+
}
132142
}
133143

134144
HarmonyImportSpecifierDependency.Template = class HarmonyImportSpecifierDependencyTemplate extends HarmonyImportDependency.Template {
@@ -140,9 +150,9 @@ HarmonyImportSpecifierDependency.Template = class HarmonyImportSpecifierDependen
140150

141151
getContent(dep, runtime) {
142152
const exportExpr = runtime.exportFromImport({
143-
module: dep.module,
153+
module: dep._module,
144154
request: dep.request,
145-
exportName: dep.id,
155+
exportName: dep._id,
146156
originModule: dep.originModule,
147157
asiSafe: dep.shorthand,
148158
isCall: dep.call,

0 commit comments

Comments
 (0)