webpack 5.77.0 → 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.

Files changed (66) hide show
  1. package/bin/webpack.js +0 -0
  2. package/lib/APIPlugin.js +25 -18
  3. package/lib/CompatibilityPlugin.js +53 -52
  4. package/lib/ConstPlugin.js +22 -15
  5. package/lib/ContextModule.js +3 -2
  6. package/lib/DefinePlugin.js +44 -36
  7. package/lib/DelegatedModule.js +2 -1
  8. package/lib/DllModule.js +2 -1
  9. package/lib/ErrorHelpers.js +61 -22
  10. package/lib/ExportsInfoApiPlugin.js +16 -9
  11. package/lib/ExternalModule.js +2 -1
  12. package/lib/FlagAllModulesAsUsedPlugin.js +22 -27
  13. package/lib/FlagDependencyExportsPlugin.js +336 -348
  14. package/lib/FlagDependencyUsagePlugin.js +6 -8
  15. package/lib/FlagEntryExportAsUsedPlugin.js +22 -23
  16. package/lib/HotModuleReplacementPlugin.js +50 -45
  17. package/lib/JavascriptMetaInfoPlugin.js +16 -9
  18. package/lib/ModuleTypeConstants.js +50 -0
  19. package/lib/NodeStuffPlugin.js +35 -31
  20. package/lib/NormalModule.js +2 -1
  21. package/lib/NormalModuleFactory.js +7 -1
  22. package/lib/ProvidePlugin.js +17 -10
  23. package/lib/RawModule.js +2 -1
  24. package/lib/RequireJsStuffPlugin.js +15 -15
  25. package/lib/UseStrictPlugin.js +15 -8
  26. package/lib/WebpackIsIncludedPlugin.js +16 -9
  27. package/lib/config/defaults.js +16 -8
  28. package/lib/config/normalization.js +4 -0
  29. package/lib/container/ContainerEntryModule.js +2 -1
  30. package/lib/css/CssParser.js +22 -2
  31. package/lib/debug/ProfilingPlugin.js +20 -12
  32. package/lib/dependencies/AMDPlugin.js +26 -20
  33. package/lib/dependencies/CommonJsImportsParserPlugin.js +5 -4
  34. package/lib/dependencies/CommonJsPlugin.js +29 -25
  35. package/lib/dependencies/HarmonyDetectionParserPlugin.js +3 -1
  36. package/lib/dependencies/HarmonyModulesPlugin.js +11 -5
  37. package/lib/dependencies/ImportMetaContextPlugin.js +11 -5
  38. package/lib/dependencies/ImportMetaPlugin.js +26 -20
  39. package/lib/dependencies/ImportPlugin.js +14 -7
  40. package/lib/dependencies/RequireContextPlugin.js +12 -6
  41. package/lib/dependencies/RequireEnsurePlugin.js +13 -7
  42. package/lib/dependencies/RequireIncludePlugin.js +11 -5
  43. package/lib/dependencies/SystemPlugin.js +21 -15
  44. package/lib/dependencies/URLPlugin.js +15 -9
  45. package/lib/dependencies/WorkerPlugin.js +14 -8
  46. package/lib/javascript/JavascriptModulesPlugin.js +157 -164
  47. package/lib/json/JsonModulesPlugin.js +13 -5
  48. package/lib/library/AmdLibraryPlugin.js +22 -6
  49. package/lib/node/ReadFileCompileAsyncWasmPlugin.js +2 -1
  50. package/lib/node/ReadFileCompileWasmPlugin.js +2 -1
  51. package/lib/optimize/ConcatenatedModule.js +2 -1
  52. package/lib/optimize/InnerGraphPlugin.js +47 -46
  53. package/lib/optimize/SideEffectsFlagPlugin.js +43 -43
  54. package/lib/sharing/ConsumeSharedPlugin.js +4 -0
  55. package/lib/wasm-async/AsyncWebAssemblyModulesPlugin.js +9 -6
  56. package/lib/wasm-sync/WebAssemblyModulesPlugin.js +42 -43
  57. package/lib/web/FetchCompileAsyncWasmPlugin.js +2 -1
  58. package/lib/web/FetchCompileWasmPlugin.js +40 -40
  59. package/package.json +1 -1
  60. package/schemas/WebpackOptions.check.js +1 -1
  61. package/schemas/WebpackOptions.json +18 -0
  62. package/schemas/plugins/container/ContainerPlugin.check.js +1 -1
  63. package/schemas/plugins/container/ContainerPlugin.json +8 -0
  64. package/schemas/plugins/container/ModuleFederationPlugin.check.js +1 -1
  65. package/schemas/plugins/container/ModuleFederationPlugin.json +8 -0
  66. package/types.d.ts +10 -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
- "APIPlugin",
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("APIPlugin", chunk => {
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("APIPlugin", (chunk, set) => {
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
- "APIPlugin",
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("APIPlugin", expr => {
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("APIPlugin", evaluateToString(info.type));
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("APIPlugin", expr => {
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("APIPlugin", expr =>
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("APIPlugin", expr =>
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("APIPlugin", expr => {
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("APIPlugin", expr => {
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("APIPlugin", evaluateToString("object"));
243
+ .tap(PLUGIN_NAME, evaluateToString("object"));
237
244
  };
238
245
 
239
246
  normalModuleFactory.hooks.parser
240
- .for("javascript/auto")
241
- .tap("APIPlugin", handler);
247
+ .for(JAVASCRIPT_MODULE_TYPE_AUTO)
248
+ .tap(PLUGIN_NAME, handler);
242
249
  normalModuleFactory.hooks.parser
243
- .for("javascript/dynamic")
244
- .tap("APIPlugin", handler);
250
+ .for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
251
+ .tap(PLUGIN_NAME, handler);
245
252
  normalModuleFactory.hooks.parser
246
- .for("javascript/esm")
247
- .tap("APIPlugin", handler);
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
- "CompatibilityPlugin",
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("javascript/auto")
32
- .tap("CompatibilityPlugin", (parser, parserOptions) => {
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
- .for("require")
41
- .tap("CompatibilityPlugin", expr => {
42
- // support for browserify style require delegator: "require(o, !0)"
43
- if (expr.arguments.length !== 2) return;
44
- const second = parser.evaluateExpression(expr.arguments[1]);
45
- if (!second.isBoolean()) return;
46
- if (second.asBool() !== true) return;
47
- const dep = new ConstDependency("require", expr.callee.range);
48
- dep.loc = expr.loc;
49
- if (parser.state.current.dependencies.length > 0) {
50
- const last =
51
- parser.state.current.dependencies[
52
- parser.state.current.dependencies.length - 1
53
- ];
54
- if (
55
- last.critical &&
56
- last.options &&
57
- last.options.request === "." &&
58
- last.userRequest === "." &&
59
- last.options.recursive
60
- )
61
- parser.state.current.dependencies.pop();
62
- }
63
- parser.state.module.addPresentationalDependency(dep);
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("CompatibilityPlugin", statement => {
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("CompatibilityPlugin", pattern => {
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("CompatibilityPlugin", expr => {
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
- "CompatibilityPlugin",
125
- (program, comments) => {
126
- if (comments.length === 0) return;
127
- const c = comments[0];
128
- if (c.type === "Line" && c.range[0] === 0) {
129
- if (parser.state.source.slice(0, 2).toString() !== "#!") return;
130
- // this is a hashbang comment
131
- const dep = new ConstDependency("//", 0);
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("javascript/auto")
141
- .tap("CompatibilityPlugin", handler);
141
+ .for(JAVASCRIPT_MODULE_TYPE_AUTO)
142
+ .tap(PLUGIN_NAME, handler);
142
143
  normalModuleFactory.hooks.parser
143
- .for("javascript/dynamic")
144
- .tap("CompatibilityPlugin", handler);
144
+ .for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
145
+ .tap(PLUGIN_NAME, handler);
145
146
  normalModuleFactory.hooks.parser
146
- .for("javascript/esm")
147
- .tap("CompatibilityPlugin", handler);
147
+ .for(JAVASCRIPT_MODULE_TYPE_ESM)
148
+ .tap(PLUGIN_NAME, handler);
148
149
  }
149
150
  );
150
151
  }
@@ -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
- "ConstPlugin",
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("ConstPlugin", statement => {
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
- "ConstPlugin",
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
- "ConstPlugin",
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("ConstPlugin", expr => {
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("ConstPlugin", expr => {
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("ConstPlugin", expr => {
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("ConstPlugin", expr => {
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("ConstPlugin", expr => {
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("javascript/auto")
485
- .tap("ConstPlugin", handler);
491
+ .for(JAVASCRIPT_MODULE_TYPE_AUTO)
492
+ .tap(PLUGIN_NAME, handler);
486
493
  normalModuleFactory.hooks.parser
487
- .for("javascript/dynamic")
488
- .tap("ConstPlugin", handler);
494
+ .for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
495
+ .tap(PLUGIN_NAME, handler);
489
496
  normalModuleFactory.hooks.parser
490
- .for("javascript/esm")
491
- .tap("ConstPlugin", handler);
497
+ .for(JAVASCRIPT_MODULE_TYPE_ESM)
498
+ .tap(PLUGIN_NAME, handler);
492
499
  }
493
500
  );
494
501
  }
@@ -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("javascript/dynamic", resource);
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("javascript/dynamic");
118
+ super(JAVASCRIPT_MODULE_TYPE_DYNAMIC);
118
119
  /** @type {ContextModuleOptions} */
119
120
  this.options = {
120
121
  ...options,
@@ -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 VALUE_DEP_PREFIX = "webpack/DefinePlugin ";
253
- const VALUE_DEP_MAIN = "webpack/DefinePlugin_hash";
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
- "DefinePlugin",
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("DefinePlugin", () => {
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("DefinePlugin", () => {
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 = /^typeof\s+/.test(key);
375
- if (isTypeof) key = key.replace(/^typeof\s+/, "");
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("DefinePlugin", () => {
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("DefinePlugin", expr => {
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("DefinePlugin", expr => {
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 (/__webpack_require__\s*(!?\.)/.test(strCode)) {
431
+ if (WEBPACK_REQUIRE_FUNCTION_REGEXP.test(strCode)) {
422
432
  return toConstantDependency(parser, strCode, [
423
433
  RuntimeGlobals.require
424
434
  ])(expr);
425
- } else if (/__webpack_require__/.test(strCode)) {
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("DefinePlugin", expr => {
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("DefinePlugin", expr => {
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("DefinePlugin", () => {
501
+ parser.hooks.canRename.for(key).tap(PLUGIN_NAME, () => {
492
502
  addValueDependency(key);
493
503
  return true;
494
504
  });
495
- parser.hooks.evaluateIdentifier
496
- .for(key)
497
- .tap("DefinePlugin", expr => {
498
- addValueDependency(key);
499
- return new BasicEvaluatedExpression()
500
- .setTruthy()
501
- .setSideEffects(false)
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
- "DefinePlugin",
515
+ PLUGIN_NAME,
508
516
  withValueDependency(key, evaluateToString("object"))
509
517
  );
510
- parser.hooks.expression.for(key).tap("DefinePlugin", expr => {
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 (/__webpack_require__\s*(!?\.)/.test(strCode)) {
529
+ if (WEBPACK_REQUIRE_FUNCTION_REGEXP.test(strCode)) {
522
530
  return toConstantDependency(parser, strCode, [
523
531
  RuntimeGlobals.require
524
532
  ])(expr);
525
- } else if (/__webpack_require__/.test(strCode)) {
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
- "DefinePlugin",
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("javascript/auto")
549
- .tap("DefinePlugin", handler);
556
+ .for(JAVASCRIPT_MODULE_TYPE_AUTO)
557
+ .tap(PLUGIN_NAME, handler);
550
558
  normalModuleFactory.hooks.parser
551
- .for("javascript/dynamic")
552
- .tap("DefinePlugin", handler);
559
+ .for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
560
+ .tap(PLUGIN_NAME, handler);
553
561
  normalModuleFactory.hooks.parser
554
- .for("javascript/esm")
555
- .tap("DefinePlugin", handler);
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
- `DefinePlugin\nConflicting values for '${prefix + key}'`
582
+ `${PLUGIN_NAME}\nConflicting values for '${prefix + key}'`
575
583
  );
576
584
  warning.details = `'${oldVersion}' !== '${version}'`;
577
585
  warning.hideStack = true;