webpack 5.76.3 → 5.78.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of webpack might be problematic. Click here for more details.
- package/bin/webpack.js +0 -0
- package/lib/APIPlugin.js +25 -18
- package/lib/CompatibilityPlugin.js +53 -52
- package/lib/ConstPlugin.js +22 -15
- package/lib/ContextModule.js +3 -2
- package/lib/DefinePlugin.js +44 -36
- package/lib/DelegatedModule.js +2 -1
- package/lib/DllModule.js +2 -1
- package/lib/ErrorHelpers.js +61 -22
- package/lib/ExportsInfoApiPlugin.js +16 -9
- package/lib/ExternalModule.js +2 -1
- package/lib/FlagAllModulesAsUsedPlugin.js +22 -27
- package/lib/FlagDependencyExportsPlugin.js +336 -348
- package/lib/FlagDependencyUsagePlugin.js +6 -8
- package/lib/FlagEntryExportAsUsedPlugin.js +22 -23
- package/lib/HotModuleReplacementPlugin.js +50 -45
- package/lib/JavascriptMetaInfoPlugin.js +16 -9
- package/lib/ModuleTypeConstants.js +50 -0
- package/lib/NodeStuffPlugin.js +35 -31
- package/lib/NormalModule.js +2 -1
- package/lib/NormalModuleFactory.js +27 -1
- package/lib/ProvidePlugin.js +17 -10
- package/lib/RawModule.js +2 -1
- package/lib/RequireJsStuffPlugin.js +15 -15
- package/lib/UseStrictPlugin.js +15 -8
- package/lib/WebpackIsIncludedPlugin.js +16 -9
- package/lib/WebpackOptionsApply.js +2 -1
- package/lib/config/defaults.js +17 -8
- package/lib/config/normalization.js +5 -0
- package/lib/container/ContainerEntryModule.js +2 -1
- package/lib/css/CssParser.js +22 -2
- package/lib/debug/ProfilingPlugin.js +20 -12
- package/lib/dependencies/AMDPlugin.js +26 -20
- package/lib/dependencies/CommonJsImportsParserPlugin.js +5 -4
- package/lib/dependencies/CommonJsPlugin.js +29 -25
- package/lib/dependencies/HarmonyDetectionParserPlugin.js +3 -1
- package/lib/dependencies/HarmonyModulesPlugin.js +11 -5
- package/lib/dependencies/ImportMetaContextPlugin.js +11 -5
- package/lib/dependencies/ImportMetaPlugin.js +26 -20
- package/lib/dependencies/ImportPlugin.js +14 -7
- package/lib/dependencies/RequireContextPlugin.js +12 -6
- package/lib/dependencies/RequireEnsurePlugin.js +13 -7
- package/lib/dependencies/RequireIncludePlugin.js +11 -5
- package/lib/dependencies/SystemPlugin.js +21 -15
- package/lib/dependencies/URLPlugin.js +15 -9
- package/lib/dependencies/WorkerDependency.js +37 -2
- package/lib/dependencies/WorkerPlugin.js +19 -10
- package/lib/javascript/JavascriptModulesPlugin.js +157 -164
- package/lib/json/JsonModulesPlugin.js +13 -5
- package/lib/library/AmdLibraryPlugin.js +22 -6
- package/lib/node/ReadFileCompileAsyncWasmPlugin.js +2 -1
- package/lib/node/ReadFileCompileWasmPlugin.js +2 -1
- package/lib/optimize/ConcatenatedModule.js +2 -1
- package/lib/optimize/InnerGraphPlugin.js +47 -46
- package/lib/optimize/SideEffectsFlagPlugin.js +43 -43
- package/lib/sharing/ConsumeSharedPlugin.js +4 -0
- package/lib/wasm-async/AsyncWebAssemblyModulesPlugin.js +9 -6
- package/lib/wasm-sync/WebAssemblyModulesPlugin.js +42 -43
- package/lib/web/FetchCompileAsyncWasmPlugin.js +2 -1
- package/lib/web/FetchCompileWasmPlugin.js +40 -40
- package/package.json +1 -1
- package/schemas/WebpackOptions.check.js +1 -1
- package/schemas/WebpackOptions.json +28 -0
- package/schemas/plugins/container/ContainerPlugin.check.js +1 -1
- package/schemas/plugins/container/ContainerPlugin.json +8 -0
- package/schemas/plugins/container/ModuleFederationPlugin.check.js +1 -1
- package/schemas/plugins/container/ModuleFederationPlugin.json +8 -0
- package/types.d.ts +20 -0
package/bin/webpack.js
CHANGED
File without changes
|
package/lib/APIPlugin.js
CHANGED
@@ -5,6 +5,11 @@
|
|
5
5
|
|
6
6
|
"use strict";
|
7
7
|
|
8
|
+
const {
|
9
|
+
JAVASCRIPT_MODULE_TYPE_AUTO,
|
10
|
+
JAVASCRIPT_MODULE_TYPE_DYNAMIC,
|
11
|
+
JAVASCRIPT_MODULE_TYPE_ESM
|
12
|
+
} = require("./ModuleTypeConstants");
|
8
13
|
const RuntimeGlobals = require("./RuntimeGlobals");
|
9
14
|
const WebpackError = require("./WebpackError");
|
10
15
|
const ConstDependency = require("./dependencies/ConstDependency");
|
@@ -113,6 +118,8 @@ const REPLACEMENTS = {
|
|
113
118
|
};
|
114
119
|
/* eslint-enable camelcase */
|
115
120
|
|
121
|
+
const PLUGIN_NAME = "APIPlugin";
|
122
|
+
|
116
123
|
class APIPlugin {
|
117
124
|
/**
|
118
125
|
* Apply the plugin
|
@@ -121,7 +128,7 @@ class APIPlugin {
|
|
121
128
|
*/
|
122
129
|
apply(compiler) {
|
123
130
|
compiler.hooks.compilation.tap(
|
124
|
-
|
131
|
+
PLUGIN_NAME,
|
125
132
|
(compilation, { normalModuleFactory }) => {
|
126
133
|
compilation.dependencyTemplates.set(
|
127
134
|
ConstDependency,
|
@@ -130,7 +137,7 @@ class APIPlugin {
|
|
130
137
|
|
131
138
|
compilation.hooks.runtimeRequirementInTree
|
132
139
|
.for(RuntimeGlobals.chunkName)
|
133
|
-
.tap(
|
140
|
+
.tap(PLUGIN_NAME, chunk => {
|
134
141
|
compilation.addRuntimeModule(
|
135
142
|
chunk,
|
136
143
|
new ChunkNameRuntimeModule(chunk.name)
|
@@ -140,7 +147,7 @@ class APIPlugin {
|
|
140
147
|
|
141
148
|
compilation.hooks.runtimeRequirementInTree
|
142
149
|
.for(RuntimeGlobals.getFullHash)
|
143
|
-
.tap(
|
150
|
+
.tap(PLUGIN_NAME, (chunk, set) => {
|
144
151
|
compilation.addRuntimeModule(chunk, new GetFullHashRuntimeModule());
|
145
152
|
return true;
|
146
153
|
});
|
@@ -154,11 +161,11 @@ class APIPlugin {
|
|
154
161
|
parser.hooks.expression
|
155
162
|
.for(key)
|
156
163
|
.tap(
|
157
|
-
|
164
|
+
PLUGIN_NAME,
|
158
165
|
toConstantDependency(parser, info.expr, info.req)
|
159
166
|
);
|
160
167
|
if (info.assign === false) {
|
161
|
-
parser.hooks.assign.for(key).tap(
|
168
|
+
parser.hooks.assign.for(key).tap(PLUGIN_NAME, expr => {
|
162
169
|
const err = new WebpackError(`${key} must not be assigned`);
|
163
170
|
err.loc = expr.loc;
|
164
171
|
throw err;
|
@@ -167,13 +174,13 @@ class APIPlugin {
|
|
167
174
|
if (info.type) {
|
168
175
|
parser.hooks.evaluateTypeof
|
169
176
|
.for(key)
|
170
|
-
.tap(
|
177
|
+
.tap(PLUGIN_NAME, evaluateToString(info.type));
|
171
178
|
}
|
172
179
|
});
|
173
180
|
|
174
181
|
parser.hooks.expression
|
175
182
|
.for("__webpack_layer__")
|
176
|
-
.tap(
|
183
|
+
.tap(PLUGIN_NAME, expr => {
|
177
184
|
const dep = new ConstDependency(
|
178
185
|
JSON.stringify(parser.state.module.layer),
|
179
186
|
expr.range
|
@@ -184,7 +191,7 @@ class APIPlugin {
|
|
184
191
|
});
|
185
192
|
parser.hooks.evaluateIdentifier
|
186
193
|
.for("__webpack_layer__")
|
187
|
-
.tap(
|
194
|
+
.tap(PLUGIN_NAME, expr =>
|
188
195
|
(parser.state.module.layer === null
|
189
196
|
? new BasicEvaluatedExpression().setNull()
|
190
197
|
: new BasicEvaluatedExpression().setString(
|
@@ -194,7 +201,7 @@ class APIPlugin {
|
|
194
201
|
);
|
195
202
|
parser.hooks.evaluateTypeof
|
196
203
|
.for("__webpack_layer__")
|
197
|
-
.tap(
|
204
|
+
.tap(PLUGIN_NAME, expr =>
|
198
205
|
new BasicEvaluatedExpression()
|
199
206
|
.setString(
|
200
207
|
parser.state.module.layer === null ? "object" : "string"
|
@@ -204,7 +211,7 @@ class APIPlugin {
|
|
204
211
|
|
205
212
|
parser.hooks.expression
|
206
213
|
.for("__webpack_module__.id")
|
207
|
-
.tap(
|
214
|
+
.tap(PLUGIN_NAME, expr => {
|
208
215
|
parser.state.module.buildInfo.moduleConcatenationBailout =
|
209
216
|
"__webpack_module__.id";
|
210
217
|
const dep = new ConstDependency(
|
@@ -219,7 +226,7 @@ class APIPlugin {
|
|
219
226
|
|
220
227
|
parser.hooks.expression
|
221
228
|
.for("__webpack_module__")
|
222
|
-
.tap(
|
229
|
+
.tap(PLUGIN_NAME, expr => {
|
223
230
|
parser.state.module.buildInfo.moduleConcatenationBailout =
|
224
231
|
"__webpack_module__";
|
225
232
|
const dep = new ConstDependency(
|
@@ -233,18 +240,18 @@ class APIPlugin {
|
|
233
240
|
});
|
234
241
|
parser.hooks.evaluateTypeof
|
235
242
|
.for("__webpack_module__")
|
236
|
-
.tap(
|
243
|
+
.tap(PLUGIN_NAME, evaluateToString("object"));
|
237
244
|
};
|
238
245
|
|
239
246
|
normalModuleFactory.hooks.parser
|
240
|
-
.for(
|
241
|
-
.tap(
|
247
|
+
.for(JAVASCRIPT_MODULE_TYPE_AUTO)
|
248
|
+
.tap(PLUGIN_NAME, handler);
|
242
249
|
normalModuleFactory.hooks.parser
|
243
|
-
.for(
|
244
|
-
.tap(
|
250
|
+
.for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
|
251
|
+
.tap(PLUGIN_NAME, handler);
|
245
252
|
normalModuleFactory.hooks.parser
|
246
|
-
.for(
|
247
|
-
.tap(
|
253
|
+
.for(JAVASCRIPT_MODULE_TYPE_ESM)
|
254
|
+
.tap(PLUGIN_NAME, handler);
|
248
255
|
}
|
249
256
|
);
|
250
257
|
}
|
@@ -5,12 +5,18 @@
|
|
5
5
|
|
6
6
|
"use strict";
|
7
7
|
|
8
|
+
const {
|
9
|
+
JAVASCRIPT_MODULE_TYPE_AUTO,
|
10
|
+
JAVASCRIPT_MODULE_TYPE_DYNAMIC,
|
11
|
+
JAVASCRIPT_MODULE_TYPE_ESM
|
12
|
+
} = require("./ModuleTypeConstants");
|
8
13
|
const ConstDependency = require("./dependencies/ConstDependency");
|
9
14
|
|
10
15
|
/** @typedef {import("./Compiler")} Compiler */
|
11
16
|
/** @typedef {import("./javascript/JavascriptParser")} JavascriptParser */
|
12
17
|
|
13
18
|
const nestedWebpackRequireTag = Symbol("nested __webpack_require__");
|
19
|
+
const PLUGIN_NAME = "CompatibilityPlugin";
|
14
20
|
|
15
21
|
class CompatibilityPlugin {
|
16
22
|
/**
|
@@ -20,7 +26,7 @@ class CompatibilityPlugin {
|
|
20
26
|
*/
|
21
27
|
apply(compiler) {
|
22
28
|
compiler.hooks.compilation.tap(
|
23
|
-
|
29
|
+
PLUGIN_NAME,
|
24
30
|
(compilation, { normalModuleFactory }) => {
|
25
31
|
compilation.dependencyTemplates.set(
|
26
32
|
ConstDependency,
|
@@ -28,41 +34,39 @@ class CompatibilityPlugin {
|
|
28
34
|
);
|
29
35
|
|
30
36
|
normalModuleFactory.hooks.parser
|
31
|
-
.for(
|
32
|
-
.tap(
|
37
|
+
.for(JAVASCRIPT_MODULE_TYPE_AUTO)
|
38
|
+
.tap(PLUGIN_NAME, (parser, parserOptions) => {
|
33
39
|
if (
|
34
40
|
parserOptions.browserify !== undefined &&
|
35
41
|
!parserOptions.browserify
|
36
42
|
)
|
37
43
|
return;
|
38
44
|
|
39
|
-
parser.hooks.call
|
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
|
-
return true;
|
65
|
-
});
|
45
|
+
parser.hooks.call.for("require").tap(PLUGIN_NAME, expr => {
|
46
|
+
// support for browserify style require delegator: "require(o, !0)"
|
47
|
+
if (expr.arguments.length !== 2) return;
|
48
|
+
const second = parser.evaluateExpression(expr.arguments[1]);
|
49
|
+
if (!second.isBoolean()) return;
|
50
|
+
if (second.asBool() !== true) return;
|
51
|
+
const dep = new ConstDependency("require", expr.callee.range);
|
52
|
+
dep.loc = expr.loc;
|
53
|
+
if (parser.state.current.dependencies.length > 0) {
|
54
|
+
const last =
|
55
|
+
parser.state.current.dependencies[
|
56
|
+
parser.state.current.dependencies.length - 1
|
57
|
+
];
|
58
|
+
if (
|
59
|
+
last.critical &&
|
60
|
+
last.options &&
|
61
|
+
last.options.request === "." &&
|
62
|
+
last.userRequest === "." &&
|
63
|
+
last.options.recursive
|
64
|
+
)
|
65
|
+
parser.state.current.dependencies.pop();
|
66
|
+
}
|
67
|
+
parser.state.module.addPresentationalDependency(dep);
|
68
|
+
return true;
|
69
|
+
});
|
66
70
|
});
|
67
71
|
|
68
72
|
/**
|
@@ -71,7 +75,7 @@ class CompatibilityPlugin {
|
|
71
75
|
*/
|
72
76
|
const handler = parser => {
|
73
77
|
// Handle nested requires
|
74
|
-
parser.hooks.preStatement.tap(
|
78
|
+
parser.hooks.preStatement.tap(PLUGIN_NAME, statement => {
|
75
79
|
if (
|
76
80
|
statement.type === "FunctionDeclaration" &&
|
77
81
|
statement.id &&
|
@@ -91,7 +95,7 @@ class CompatibilityPlugin {
|
|
91
95
|
});
|
92
96
|
parser.hooks.pattern
|
93
97
|
.for("__webpack_require__")
|
94
|
-
.tap(
|
98
|
+
.tap(PLUGIN_NAME, pattern => {
|
95
99
|
const newName = `__nested_webpack_require_${pattern.range[0]}__`;
|
96
100
|
parser.tagVariable(pattern.name, nestedWebpackRequireTag, {
|
97
101
|
name: newName,
|
@@ -105,7 +109,7 @@ class CompatibilityPlugin {
|
|
105
109
|
});
|
106
110
|
parser.hooks.expression
|
107
111
|
.for(nestedWebpackRequireTag)
|
108
|
-
.tap(
|
112
|
+
.tap(PLUGIN_NAME, expr => {
|
109
113
|
const { name, declaration } = parser.currentTagData;
|
110
114
|
if (!declaration.updated) {
|
111
115
|
const dep = new ConstDependency(name, declaration.range);
|
@@ -120,31 +124,28 @@ class CompatibilityPlugin {
|
|
120
124
|
});
|
121
125
|
|
122
126
|
// Handle hashbang
|
123
|
-
parser.hooks.program.tap(
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
dep.loc = c.loc;
|
133
|
-
parser.state.module.addPresentationalDependency(dep);
|
134
|
-
}
|
127
|
+
parser.hooks.program.tap(PLUGIN_NAME, (program, comments) => {
|
128
|
+
if (comments.length === 0) return;
|
129
|
+
const c = comments[0];
|
130
|
+
if (c.type === "Line" && c.range[0] === 0) {
|
131
|
+
if (parser.state.source.slice(0, 2).toString() !== "#!") return;
|
132
|
+
// this is a hashbang comment
|
133
|
+
const dep = new ConstDependency("//", 0);
|
134
|
+
dep.loc = c.loc;
|
135
|
+
parser.state.module.addPresentationalDependency(dep);
|
135
136
|
}
|
136
|
-
);
|
137
|
+
});
|
137
138
|
};
|
138
139
|
|
139
140
|
normalModuleFactory.hooks.parser
|
140
|
-
.for(
|
141
|
-
.tap(
|
141
|
+
.for(JAVASCRIPT_MODULE_TYPE_AUTO)
|
142
|
+
.tap(PLUGIN_NAME, handler);
|
142
143
|
normalModuleFactory.hooks.parser
|
143
|
-
.for(
|
144
|
-
.tap(
|
144
|
+
.for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
|
145
|
+
.tap(PLUGIN_NAME, handler);
|
145
146
|
normalModuleFactory.hooks.parser
|
146
|
-
.for(
|
147
|
-
.tap(
|
147
|
+
.for(JAVASCRIPT_MODULE_TYPE_ESM)
|
148
|
+
.tap(PLUGIN_NAME, handler);
|
148
149
|
}
|
149
150
|
);
|
150
151
|
}
|
package/lib/ConstPlugin.js
CHANGED
@@ -5,6 +5,11 @@
|
|
5
5
|
|
6
6
|
"use strict";
|
7
7
|
|
8
|
+
const {
|
9
|
+
JAVASCRIPT_MODULE_TYPE_AUTO,
|
10
|
+
JAVASCRIPT_MODULE_TYPE_DYNAMIC,
|
11
|
+
JAVASCRIPT_MODULE_TYPE_ESM
|
12
|
+
} = require("./ModuleTypeConstants");
|
8
13
|
const CachedConstDependency = require("./dependencies/CachedConstDependency");
|
9
14
|
const ConstDependency = require("./dependencies/ConstDependency");
|
10
15
|
const { evaluateToString } = require("./javascript/JavascriptParserHelpers");
|
@@ -108,6 +113,8 @@ const getHoistedDeclarations = (branch, includeFunctionDeclarations) => {
|
|
108
113
|
return Array.from(declarations);
|
109
114
|
};
|
110
115
|
|
116
|
+
const PLUGIN_NAME = "ConstPlugin";
|
117
|
+
|
111
118
|
class ConstPlugin {
|
112
119
|
/**
|
113
120
|
* Apply the plugin
|
@@ -117,7 +124,7 @@ class ConstPlugin {
|
|
117
124
|
apply(compiler) {
|
118
125
|
const cachedParseResource = parseResource.bindCache(compiler.root);
|
119
126
|
compiler.hooks.compilation.tap(
|
120
|
-
|
127
|
+
PLUGIN_NAME,
|
121
128
|
(compilation, { normalModuleFactory }) => {
|
122
129
|
compilation.dependencyTemplates.set(
|
123
130
|
ConstDependency,
|
@@ -130,7 +137,7 @@ class ConstPlugin {
|
|
130
137
|
);
|
131
138
|
|
132
139
|
const handler = parser => {
|
133
|
-
parser.hooks.statementIf.tap(
|
140
|
+
parser.hooks.statementIf.tap(PLUGIN_NAME, statement => {
|
134
141
|
if (parser.scope.isAsmJs) return;
|
135
142
|
const param = parser.evaluateExpression(statement.test);
|
136
143
|
const bool = param.asBool();
|
@@ -202,7 +209,7 @@ class ConstPlugin {
|
|
202
209
|
}
|
203
210
|
});
|
204
211
|
parser.hooks.expressionConditionalOperator.tap(
|
205
|
-
|
212
|
+
PLUGIN_NAME,
|
206
213
|
expression => {
|
207
214
|
if (parser.scope.isAsmJs) return;
|
208
215
|
const param = parser.evaluateExpression(expression.test);
|
@@ -237,7 +244,7 @@ class ConstPlugin {
|
|
237
244
|
}
|
238
245
|
);
|
239
246
|
parser.hooks.expressionLogicalOperator.tap(
|
240
|
-
|
247
|
+
PLUGIN_NAME,
|
241
248
|
expression => {
|
242
249
|
if (parser.scope.isAsmJs) return;
|
243
250
|
if (
|
@@ -374,7 +381,7 @@ class ConstPlugin {
|
|
374
381
|
}
|
375
382
|
}
|
376
383
|
);
|
377
|
-
parser.hooks.optionalChaining.tap(
|
384
|
+
parser.hooks.optionalChaining.tap(PLUGIN_NAME, expr => {
|
378
385
|
/** @type {ExpressionNode[]} */
|
379
386
|
const optionalExpressionsStack = [];
|
380
387
|
/** @type {ExpressionNode|SuperNode} */
|
@@ -429,7 +436,7 @@ class ConstPlugin {
|
|
429
436
|
});
|
430
437
|
parser.hooks.evaluateIdentifier
|
431
438
|
.for("__resourceQuery")
|
432
|
-
.tap(
|
439
|
+
.tap(PLUGIN_NAME, expr => {
|
433
440
|
if (parser.scope.isAsmJs) return;
|
434
441
|
if (!parser.state.module) return;
|
435
442
|
return evaluateToString(
|
@@ -438,7 +445,7 @@ class ConstPlugin {
|
|
438
445
|
});
|
439
446
|
parser.hooks.expression
|
440
447
|
.for("__resourceQuery")
|
441
|
-
.tap(
|
448
|
+
.tap(PLUGIN_NAME, expr => {
|
442
449
|
if (parser.scope.isAsmJs) return;
|
443
450
|
if (!parser.state.module) return;
|
444
451
|
const dep = new CachedConstDependency(
|
@@ -455,7 +462,7 @@ class ConstPlugin {
|
|
455
462
|
|
456
463
|
parser.hooks.evaluateIdentifier
|
457
464
|
.for("__resourceFragment")
|
458
|
-
.tap(
|
465
|
+
.tap(PLUGIN_NAME, expr => {
|
459
466
|
if (parser.scope.isAsmJs) return;
|
460
467
|
if (!parser.state.module) return;
|
461
468
|
return evaluateToString(
|
@@ -464,7 +471,7 @@ class ConstPlugin {
|
|
464
471
|
});
|
465
472
|
parser.hooks.expression
|
466
473
|
.for("__resourceFragment")
|
467
|
-
.tap(
|
474
|
+
.tap(PLUGIN_NAME, expr => {
|
468
475
|
if (parser.scope.isAsmJs) return;
|
469
476
|
if (!parser.state.module) return;
|
470
477
|
const dep = new CachedConstDependency(
|
@@ -481,14 +488,14 @@ class ConstPlugin {
|
|
481
488
|
};
|
482
489
|
|
483
490
|
normalModuleFactory.hooks.parser
|
484
|
-
.for(
|
485
|
-
.tap(
|
491
|
+
.for(JAVASCRIPT_MODULE_TYPE_AUTO)
|
492
|
+
.tap(PLUGIN_NAME, handler);
|
486
493
|
normalModuleFactory.hooks.parser
|
487
|
-
.for(
|
488
|
-
.tap(
|
494
|
+
.for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
|
495
|
+
.tap(PLUGIN_NAME, handler);
|
489
496
|
normalModuleFactory.hooks.parser
|
490
|
-
.for(
|
491
|
-
.tap(
|
497
|
+
.for(JAVASCRIPT_MODULE_TYPE_ESM)
|
498
|
+
.tap(PLUGIN_NAME, handler);
|
492
499
|
}
|
493
500
|
);
|
494
501
|
}
|
package/lib/ContextModule.js
CHANGED
@@ -9,6 +9,7 @@ const { OriginalSource, RawSource } = require("webpack-sources");
|
|
9
9
|
const AsyncDependenciesBlock = require("./AsyncDependenciesBlock");
|
10
10
|
const { makeWebpackError } = require("./HookWebpackError");
|
11
11
|
const Module = require("./Module");
|
12
|
+
const { JAVASCRIPT_MODULE_TYPE_DYNAMIC } = require("./ModuleTypeConstants");
|
12
13
|
const RuntimeGlobals = require("./RuntimeGlobals");
|
13
14
|
const Template = require("./Template");
|
14
15
|
const WebpackError = require("./WebpackError");
|
@@ -105,7 +106,7 @@ class ContextModule extends Module {
|
|
105
106
|
const resourceFragment =
|
106
107
|
(options && options.resourceFragment) || parsed.fragment;
|
107
108
|
|
108
|
-
super(
|
109
|
+
super(JAVASCRIPT_MODULE_TYPE_DYNAMIC, resource);
|
109
110
|
/** @type {ContextModuleOptions} */
|
110
111
|
this.options = {
|
111
112
|
...options,
|
@@ -114,7 +115,7 @@ class ContextModule extends Module {
|
|
114
115
|
resourceFragment
|
115
116
|
};
|
116
117
|
} else {
|
117
|
-
super(
|
118
|
+
super(JAVASCRIPT_MODULE_TYPE_DYNAMIC);
|
118
119
|
/** @type {ContextModuleOptions} */
|
119
120
|
this.options = {
|
120
121
|
...options,
|
package/lib/DefinePlugin.js
CHANGED
@@ -5,10 +5,16 @@
|
|
5
5
|
|
6
6
|
"use strict";
|
7
7
|
|
8
|
+
const {
|
9
|
+
JAVASCRIPT_MODULE_TYPE_AUTO,
|
10
|
+
JAVASCRIPT_MODULE_TYPE_ESM,
|
11
|
+
JAVASCRIPT_MODULE_TYPE_DYNAMIC
|
12
|
+
} = require("./ModuleTypeConstants");
|
8
13
|
const RuntimeGlobals = require("./RuntimeGlobals");
|
9
14
|
const WebpackError = require("./WebpackError");
|
10
15
|
const ConstDependency = require("./dependencies/ConstDependency");
|
11
16
|
const BasicEvaluatedExpression = require("./javascript/BasicEvaluatedExpression");
|
17
|
+
|
12
18
|
const {
|
13
19
|
evaluateToString,
|
14
20
|
toConstantDependency
|
@@ -249,8 +255,12 @@ const toCacheVersion = code => {
|
|
249
255
|
return code + "";
|
250
256
|
};
|
251
257
|
|
252
|
-
const
|
253
|
-
const
|
258
|
+
const PLUGIN_NAME = "DefinePlugin";
|
259
|
+
const VALUE_DEP_PREFIX = `webpack/${PLUGIN_NAME} `;
|
260
|
+
const VALUE_DEP_MAIN = `webpack/${PLUGIN_NAME}_hash`;
|
261
|
+
const TYPEOF_OPERATOR_REGEXP = /^typeof\s+/;
|
262
|
+
const WEBPACK_REQUIRE_FUNCTION_REGEXP = /__webpack_require__\s*(!?\.)/;
|
263
|
+
const WEBPACK_REQUIRE_IDENTIFIER_REGEXP = /__webpack_require__/;
|
254
264
|
|
255
265
|
class DefinePlugin {
|
256
266
|
/**
|
@@ -278,7 +288,7 @@ class DefinePlugin {
|
|
278
288
|
apply(compiler) {
|
279
289
|
const definitions = this.definitions;
|
280
290
|
compiler.hooks.compilation.tap(
|
281
|
-
|
291
|
+
PLUGIN_NAME,
|
282
292
|
(compilation, { normalModuleFactory }) => {
|
283
293
|
compilation.dependencyTemplates.set(
|
284
294
|
ConstDependency,
|
@@ -300,7 +310,7 @@ class DefinePlugin {
|
|
300
310
|
*/
|
301
311
|
const handler = parser => {
|
302
312
|
const mainValue = compilation.valueCacheVersions.get(VALUE_DEP_MAIN);
|
303
|
-
parser.hooks.program.tap(
|
313
|
+
parser.hooks.program.tap(PLUGIN_NAME, () => {
|
304
314
|
const { buildInfo } = parser.state.module;
|
305
315
|
if (!buildInfo.valueDependencies)
|
306
316
|
buildInfo.valueDependencies = new Map();
|
@@ -356,7 +366,7 @@ class DefinePlugin {
|
|
356
366
|
const splittedKey = key.split(".");
|
357
367
|
splittedKey.slice(1).forEach((_, i) => {
|
358
368
|
const fullKey = prefix + splittedKey.slice(0, i + 1).join(".");
|
359
|
-
parser.hooks.canRename.for(fullKey).tap(
|
369
|
+
parser.hooks.canRename.for(fullKey).tap(PLUGIN_NAME, () => {
|
360
370
|
addValueDependency(key);
|
361
371
|
return true;
|
362
372
|
});
|
@@ -371,18 +381,18 @@ class DefinePlugin {
|
|
371
381
|
*/
|
372
382
|
const applyDefine = (key, code) => {
|
373
383
|
const originalKey = key;
|
374
|
-
const isTypeof =
|
375
|
-
if (isTypeof) key = key.replace(
|
384
|
+
const isTypeof = TYPEOF_OPERATOR_REGEXP.test(key);
|
385
|
+
if (isTypeof) key = key.replace(TYPEOF_OPERATOR_REGEXP, "");
|
376
386
|
let recurse = false;
|
377
387
|
let recurseTypeof = false;
|
378
388
|
if (!isTypeof) {
|
379
|
-
parser.hooks.canRename.for(key).tap(
|
389
|
+
parser.hooks.canRename.for(key).tap(PLUGIN_NAME, () => {
|
380
390
|
addValueDependency(originalKey);
|
381
391
|
return true;
|
382
392
|
});
|
383
393
|
parser.hooks.evaluateIdentifier
|
384
394
|
.for(key)
|
385
|
-
.tap(
|
395
|
+
.tap(PLUGIN_NAME, expr => {
|
386
396
|
/**
|
387
397
|
* this is needed in case there is a recursion in the DefinePlugin
|
388
398
|
* to prevent an endless recursion
|
@@ -408,7 +418,7 @@ class DefinePlugin {
|
|
408
418
|
res.setRange(expr.range);
|
409
419
|
return res;
|
410
420
|
});
|
411
|
-
parser.hooks.expression.for(key).tap(
|
421
|
+
parser.hooks.expression.for(key).tap(PLUGIN_NAME, expr => {
|
412
422
|
addValueDependency(originalKey);
|
413
423
|
const strCode = toCode(
|
414
424
|
code,
|
@@ -418,11 +428,11 @@ class DefinePlugin {
|
|
418
428
|
runtimeTemplate,
|
419
429
|
!parser.isAsiPosition(expr.range[0])
|
420
430
|
);
|
421
|
-
if (
|
431
|
+
if (WEBPACK_REQUIRE_FUNCTION_REGEXP.test(strCode)) {
|
422
432
|
return toConstantDependency(parser, strCode, [
|
423
433
|
RuntimeGlobals.require
|
424
434
|
])(expr);
|
425
|
-
} else if (
|
435
|
+
} else if (WEBPACK_REQUIRE_IDENTIFIER_REGEXP.test(strCode)) {
|
426
436
|
return toConstantDependency(parser, strCode, [
|
427
437
|
RuntimeGlobals.requireScope
|
428
438
|
])(expr);
|
@@ -431,7 +441,7 @@ class DefinePlugin {
|
|
431
441
|
}
|
432
442
|
});
|
433
443
|
}
|
434
|
-
parser.hooks.evaluateTypeof.for(key).tap(
|
444
|
+
parser.hooks.evaluateTypeof.for(key).tap(PLUGIN_NAME, expr => {
|
435
445
|
/**
|
436
446
|
* this is needed in case there is a recursion in the DefinePlugin
|
437
447
|
* to prevent an endless recursion
|
@@ -459,7 +469,7 @@ class DefinePlugin {
|
|
459
469
|
res.setRange(expr.range);
|
460
470
|
return res;
|
461
471
|
});
|
462
|
-
parser.hooks.typeof.for(key).tap(
|
472
|
+
parser.hooks.typeof.for(key).tap(PLUGIN_NAME, expr => {
|
463
473
|
addValueDependency(originalKey);
|
464
474
|
const codeCode = toCode(
|
465
475
|
code,
|
@@ -488,26 +498,24 @@ class DefinePlugin {
|
|
488
498
|
* @returns {void}
|
489
499
|
*/
|
490
500
|
const applyObjectDefine = (key, obj) => {
|
491
|
-
parser.hooks.canRename.for(key).tap(
|
501
|
+
parser.hooks.canRename.for(key).tap(PLUGIN_NAME, () => {
|
492
502
|
addValueDependency(key);
|
493
503
|
return true;
|
494
504
|
});
|
495
|
-
parser.hooks.evaluateIdentifier
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
.setRange(expr.range);
|
503
|
-
});
|
505
|
+
parser.hooks.evaluateIdentifier.for(key).tap(PLUGIN_NAME, expr => {
|
506
|
+
addValueDependency(key);
|
507
|
+
return new BasicEvaluatedExpression()
|
508
|
+
.setTruthy()
|
509
|
+
.setSideEffects(false)
|
510
|
+
.setRange(expr.range);
|
511
|
+
});
|
504
512
|
parser.hooks.evaluateTypeof
|
505
513
|
.for(key)
|
506
514
|
.tap(
|
507
|
-
|
515
|
+
PLUGIN_NAME,
|
508
516
|
withValueDependency(key, evaluateToString("object"))
|
509
517
|
);
|
510
|
-
parser.hooks.expression.for(key).tap(
|
518
|
+
parser.hooks.expression.for(key).tap(PLUGIN_NAME, expr => {
|
511
519
|
addValueDependency(key);
|
512
520
|
const strCode = stringifyObj(
|
513
521
|
obj,
|
@@ -518,11 +526,11 @@ class DefinePlugin {
|
|
518
526
|
!parser.isAsiPosition(expr.range[0])
|
519
527
|
);
|
520
528
|
|
521
|
-
if (
|
529
|
+
if (WEBPACK_REQUIRE_FUNCTION_REGEXP.test(strCode)) {
|
522
530
|
return toConstantDependency(parser, strCode, [
|
523
531
|
RuntimeGlobals.require
|
524
532
|
])(expr);
|
525
|
-
} else if (
|
533
|
+
} else if (WEBPACK_REQUIRE_IDENTIFIER_REGEXP.test(strCode)) {
|
526
534
|
return toConstantDependency(parser, strCode, [
|
527
535
|
RuntimeGlobals.requireScope
|
528
536
|
])(expr);
|
@@ -533,7 +541,7 @@ class DefinePlugin {
|
|
533
541
|
parser.hooks.typeof
|
534
542
|
.for(key)
|
535
543
|
.tap(
|
536
|
-
|
544
|
+
PLUGIN_NAME,
|
537
545
|
withValueDependency(
|
538
546
|
key,
|
539
547
|
toConstantDependency(parser, JSON.stringify("object"))
|
@@ -545,14 +553,14 @@ class DefinePlugin {
|
|
545
553
|
};
|
546
554
|
|
547
555
|
normalModuleFactory.hooks.parser
|
548
|
-
.for(
|
549
|
-
.tap(
|
556
|
+
.for(JAVASCRIPT_MODULE_TYPE_AUTO)
|
557
|
+
.tap(PLUGIN_NAME, handler);
|
550
558
|
normalModuleFactory.hooks.parser
|
551
|
-
.for(
|
552
|
-
.tap(
|
559
|
+
.for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
|
560
|
+
.tap(PLUGIN_NAME, handler);
|
553
561
|
normalModuleFactory.hooks.parser
|
554
|
-
.for(
|
555
|
-
.tap(
|
562
|
+
.for(JAVASCRIPT_MODULE_TYPE_ESM)
|
563
|
+
.tap(PLUGIN_NAME, handler);
|
556
564
|
|
557
565
|
/**
|
558
566
|
* Walk definitions
|
@@ -571,7 +579,7 @@ class DefinePlugin {
|
|
571
579
|
compilation.valueCacheVersions.set(name, version);
|
572
580
|
} else if (oldVersion !== version) {
|
573
581
|
const warning = new WebpackError(
|
574
|
-
|
582
|
+
`${PLUGIN_NAME}\nConflicting values for '${prefix + key}'`
|
575
583
|
);
|
576
584
|
warning.details = `'${oldVersion}' !== '${version}'`;
|
577
585
|
warning.hideStack = true;
|