webpack 5.108.4 → 5.109.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.
Files changed (186) hide show
  1. package/lib/APIPlugin.js +48 -29
  2. package/lib/CacheFacade.js +8 -0
  3. package/lib/ChunkGroup.js +1 -0
  4. package/lib/CompatibilityPlugin.js +31 -29
  5. package/lib/Compilation.js +49 -4
  6. package/lib/ConstPlugin.js +11 -11
  7. package/lib/ContextModule.js +239 -6
  8. package/lib/ContextModuleFactory.js +139 -14
  9. package/lib/DefinePlugin.js +396 -101
  10. package/lib/Entrypoint.js +1 -1
  11. package/lib/ExportsInfoApiPlugin.js +2 -2
  12. package/lib/ExternalModule.js +107 -5
  13. package/lib/ExternalModuleFactoryPlugin.js +20 -1
  14. package/lib/FileSystemInfo.js +130 -74
  15. package/lib/HotModuleReplacementPlugin.js +18 -15
  16. package/lib/Module.js +3 -0
  17. package/lib/ModuleFilenameHelpers.js +2 -2
  18. package/lib/ModuleTypeConstants.js +8 -1
  19. package/lib/MultiCompiler.js +13 -2
  20. package/lib/MultiWatching.js +1 -0
  21. package/lib/NodeStuffPlugin.js +51 -33
  22. package/lib/NormalModule.js +169 -401
  23. package/lib/NormalModuleFactory.js +76 -2
  24. package/lib/ProgressPlugin.js +211 -119
  25. package/lib/ProvidePlugin.js +2 -2
  26. package/lib/RuntimeGlobals.js +47 -0
  27. package/lib/RuntimePlugin.js +33 -0
  28. package/lib/RuntimeTemplate.js +304 -16
  29. package/lib/UseStrictPlugin.js +1 -1
  30. package/lib/WebpackIsIncludedPlugin.js +1 -1
  31. package/lib/WebpackOptionsApply.js +24 -19
  32. package/lib/asset/AssetGenerator.js +110 -28
  33. package/lib/asset/AssetModulesPlugin.js +92 -2
  34. package/lib/asset/WebManifestGenerator.js +164 -0
  35. package/lib/asset/WebManifestParser.js +130 -0
  36. package/lib/async-modules/AwaitDependenciesInitFragment.js +14 -4
  37. package/lib/async-modules/isGeneratorLowered.js +26 -0
  38. package/lib/cache/PackFileCacheStrategy.js +9 -3
  39. package/lib/config/browserslistTargetHandler.js +40 -0
  40. package/lib/config/defaults.js +219 -29
  41. package/lib/config/normalization.js +26 -0
  42. package/lib/config/target.js +9 -1
  43. package/lib/css/CssGenerator.js +40 -31
  44. package/lib/css/CssLoadingRuntimeModule.js +5 -1
  45. package/lib/css/CssModulesPlugin.js +13 -0
  46. package/lib/css/CssParser.js +594 -263
  47. package/lib/css/syntax.js +875 -462
  48. package/lib/dependencies/AMDDefineDependencyParserPlugin.js +6 -6
  49. package/lib/dependencies/AMDPlugin.js +2 -2
  50. package/lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js +13 -13
  51. package/lib/dependencies/CommonJsDependencyHelpers.js +39 -0
  52. package/lib/dependencies/CommonJsExportsDependency.js +47 -2
  53. package/lib/dependencies/CommonJsExportsParserPlugin.js +10 -10
  54. package/lib/dependencies/CommonJsImportsParserPlugin.js +18 -18
  55. package/lib/dependencies/CommonJsPlugin.js +2 -2
  56. package/lib/dependencies/CommonJsRequireDependency.js +26 -1
  57. package/lib/dependencies/CommonJsSelfReferenceDependency.js +32 -1
  58. package/lib/dependencies/ContextDependencyHelpers.js +3 -3
  59. package/lib/dependencies/CreateRequireParserPlugin.js +6 -6
  60. package/lib/dependencies/CssIcssExportDependency.js +162 -71
  61. package/lib/dependencies/CssUrlDependency.js +35 -4
  62. package/lib/dependencies/ExternalModuleInitFragmentDependency.js +2 -2
  63. package/lib/dependencies/HarmonyCompatibilityDependency.js +32 -14
  64. package/lib/dependencies/HarmonyDetectionParserPlugin.js +43 -13
  65. package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +7 -19
  66. package/lib/dependencies/HarmonyImportDependency.js +12 -1
  67. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +11 -11
  68. package/lib/dependencies/HarmonyModulesPlugin.js +6 -0
  69. package/lib/dependencies/HarmonyTopLevelThisParserPlugin.js +1 -1
  70. package/lib/dependencies/HtmlEntryDependency.js +797 -172
  71. package/lib/dependencies/HtmlSourceDependency.js +26 -0
  72. package/lib/dependencies/ImportDependency.js +43 -1
  73. package/lib/dependencies/ImportMetaContextDependencyParserPlugin.js +31 -18
  74. package/lib/dependencies/ImportMetaContextPlugin.js +38 -7
  75. package/lib/dependencies/ImportMetaGlobDependency.js +81 -0
  76. package/lib/dependencies/ImportMetaGlobDependencyParserPlugin.js +78 -0
  77. package/lib/dependencies/ImportMetaGlobHelpers.js +660 -0
  78. package/lib/dependencies/ImportMetaPlugin.js +532 -180
  79. package/lib/dependencies/ImportMetaResolveDependency.js +90 -0
  80. package/lib/dependencies/ImportParserPlugin.js +80 -17
  81. package/lib/dependencies/ImportPhase.js +10 -8
  82. package/lib/dependencies/LoaderPlugin.js +5 -1
  83. package/lib/dependencies/RequireContextDependencyParserPlugin.js +1 -1
  84. package/lib/dependencies/RequireEnsureDependenciesBlockParserPlugin.js +3 -6
  85. package/lib/dependencies/RequireIncludeDependencyParserPlugin.js +4 -11
  86. package/lib/dependencies/SystemPlugin.js +2 -4
  87. package/lib/dependencies/TopLevelAwaitDependency.js +93 -0
  88. package/lib/dependencies/URLDependency.js +141 -21
  89. package/lib/dependencies/{WorkerPlugin.js → WorkerAndWorkletPlugin.js} +324 -112
  90. package/lib/dependencies/WorkerDependency.js +86 -18
  91. package/lib/dependencies/WorkletDependency.js +278 -0
  92. package/lib/dll/DelegatedModuleFactoryPlugin.js +19 -0
  93. package/lib/errors/EnvironmentNotSupportAsyncWarning.js +1 -1
  94. package/lib/errors/LoaderLoadingError.js +20 -0
  95. package/lib/errors/ModuleParseError.js +39 -22
  96. package/lib/errors/NodeStuffInWebError.js +1 -1
  97. package/lib/esm/ModuleChunkFormatPlugin.js +47 -14
  98. package/lib/esm/ModuleChunkLoadingPlugin.js +8 -1
  99. package/lib/esm/ModuleChunkLoadingRuntimeModule.js +29 -1
  100. package/lib/hmr/lazyCompilationBackend.js +32 -8
  101. package/lib/html/HtmlGenerator.js +887 -15
  102. package/lib/html/HtmlModule.js +2 -2
  103. package/lib/html/HtmlModulesPlugin.js +610 -166
  104. package/lib/html/HtmlParser.js +603 -247
  105. package/lib/html/favicon.svg +1 -0
  106. package/lib/html/syntax.js +1264 -661
  107. package/lib/ids/IdHelpers.js +1 -1
  108. package/lib/index.js +6 -0
  109. package/lib/javascript/ArrayPushCallbackChunkFormatPlugin.js +4 -5
  110. package/lib/javascript/BasicEvaluatedExpression.js +366 -48
  111. package/lib/javascript/JavascriptGenerator.js +197 -49
  112. package/lib/javascript/JavascriptModulesPlugin.js +178 -24
  113. package/lib/javascript/JavascriptParser.js +701 -355
  114. package/lib/javascript/JavascriptParserHelpers.js +3 -6
  115. package/lib/javascript/syntax.js +4380 -0
  116. package/lib/library/ModuleLibraryPlugin.js +38 -1
  117. package/lib/loaders/LoaderRunner.js +714 -0
  118. package/lib/loaders/loadLoader.js +100 -0
  119. package/lib/node/ReadFileCompileAsyncWasmPlugin.js +19 -0
  120. package/lib/node/ReadFileCompileWasmPlugin.js +14 -0
  121. package/lib/optimize/ConcatenatedModule.js +120 -16
  122. package/lib/optimize/FlagIncludedChunksPlugin.js +13 -3
  123. package/lib/optimize/InnerGraphPlugin.js +50 -30
  124. package/lib/optimize/ModuleConcatenationPlugin.js +69 -3
  125. package/lib/optimize/SideEffectsFlagPlugin.js +86 -43
  126. package/lib/prefetch/ChunkPrefetchPreloadPlugin.js +15 -0
  127. package/lib/prefetch/ChunkPreloadTriggerRuntimeModule.js +24 -13
  128. package/lib/prefetch/ResourceHintPlugin.js +663 -0
  129. package/lib/prefetch/ResourceHintRuntimeModule.js +149 -0
  130. package/lib/prefetch/StartupAssetHintRuntimeModule.js +210 -0
  131. package/lib/prefetch/parseResourceHintOptions.js +103 -0
  132. package/lib/rules/RuleSetCompiler.js +114 -0
  133. package/lib/runtime/AsyncModuleGeneratorRuntimeModule.js +56 -0
  134. package/lib/runtime/AsyncModuleRuntimeModule.js +24 -8
  135. package/lib/runtime/AutoPublicPathRuntimeModule.js +7 -1
  136. package/lib/runtime/CommonJsWrapRuntimeModule.js +37 -0
  137. package/lib/runtime/GetWorkletBootstrapRuntimeModule.js +59 -0
  138. package/lib/runtime/MakeDeferredNamespaceObjectRuntime.js +204 -8
  139. package/lib/runtime/SetAnonymousDefaultNameRuntimeModule.js +4 -1
  140. package/lib/schemes/HttpUriPlugin.js +40 -1
  141. package/lib/schemes/VirtualUrlPlugin.js +1 -2
  142. package/lib/serialization/FileMiddleware.js +29 -3
  143. package/lib/stats/DefaultStatsFactoryPlugin.js +32 -3
  144. package/lib/stats/DefaultStatsPresetPlugin.js +2 -0
  145. package/lib/stats/DefaultStatsPrinterPlugin.js +12 -2
  146. package/lib/stats/StatsFactory.js +15 -8
  147. package/lib/url/URLParserPlugin.js +66 -13
  148. package/lib/util/AsyncQueue.js +122 -72
  149. package/lib/util/SetHelpers.js +1 -1
  150. package/lib/util/StackedMap.js +122 -48
  151. package/lib/util/binarySearchBounds.js +2 -2
  152. package/lib/util/concatenate.js +40 -14
  153. package/lib/util/createHooksRegistry.js +13 -5
  154. package/lib/util/dataURL.js +2 -2
  155. package/lib/util/deterministicGrouping.js +7 -5
  156. package/lib/util/formatSize.js +6 -2
  157. package/lib/util/globUtils.js +710 -0
  158. package/lib/util/identifier.js +23 -6
  159. package/lib/util/implicitTypeLoaderFallback.js +62 -0
  160. package/lib/util/internalSerializables.js +88 -71
  161. package/lib/util/magicComment.js +121 -25
  162. package/lib/util/numberHash.js +2 -2
  163. package/lib/wasm-async/AsyncWasmCompileRuntimeModule.js +24 -18
  164. package/lib/wasm-async/AsyncWasmLoadingRuntimeModule.js +33 -22
  165. package/lib/wasm-sync/WasmChunkLoadingRuntimeModule.js +79 -16
  166. package/lib/web/FetchCompileAsyncWasmPlugin.js +19 -0
  167. package/lib/web/FetchCompileWasmPlugin.js +14 -0
  168. package/lib/web/JsonpChunkLoadingRuntimeModule.js +2 -0
  169. package/lib/webpack.js +65 -0
  170. package/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js +3 -0
  171. package/module.d.ts +21 -0
  172. package/package.json +13 -12
  173. package/schemas/WebpackOptions.check.js +1 -1
  174. package/schemas/WebpackOptions.json +759 -36
  175. package/schemas/plugins/HtmlParserOptions.check.js +1 -1
  176. package/schemas/plugins/ProgressPlugin.check.js +1 -1
  177. package/schemas/plugins/ProgressPlugin.json +17 -1
  178. package/schemas/plugins/container/ContainerReferencePlugin.check.js +1 -1
  179. package/schemas/plugins/container/ContainerReferencePlugin.json +1 -0
  180. package/schemas/plugins/container/ExternalsType.check.js +1 -1
  181. package/schemas/plugins/container/ModuleFederationPlugin.check.js +1 -1
  182. package/schemas/plugins/container/ModuleFederationPlugin.json +1 -0
  183. package/schemas/plugins/css/CssAutoOrModuleParserOptions.check.js +1 -1
  184. package/schemas/plugins/css/CssModuleParserOptions.check.js +1 -1
  185. package/schemas/plugins/css/CssParserOptions.check.js +1 -1
  186. package/types.d.ts +1213 -96
@@ -39,6 +39,18 @@ class HtmlSourceDependency extends ModuleDependency {
39
39
  constructor(request, range) {
40
40
  super(request);
41
41
  this.range = range;
42
+ /** @type {true | undefined} */
43
+ this.prefetch = undefined;
44
+ /** @type {true | undefined} */
45
+ this.preload = undefined;
46
+ /** @type {"low" | "high" | "auto" | undefined} */
47
+ this.fetchPriority = undefined;
48
+ /** @type {string | undefined} */
49
+ this.asAttribute = undefined;
50
+ /** @type {string | undefined} */
51
+ this.typeAttribute = undefined;
52
+ /** @type {string | undefined} */
53
+ this.mediaAttribute = undefined;
42
54
  }
43
55
 
44
56
  get type() {
@@ -78,6 +90,13 @@ class HtmlSourceDependency extends ModuleDependency {
78
90
  * @param {ObjectSerializerContext} context context
79
91
  */
80
92
  serialize(context) {
93
+ const { write } = context;
94
+ write(this.prefetch);
95
+ write(this.preload);
96
+ write(this.fetchPriority);
97
+ write(this.asAttribute);
98
+ write(this.typeAttribute);
99
+ write(this.mediaAttribute);
81
100
  super.serialize(context);
82
101
  }
83
102
 
@@ -86,6 +105,13 @@ class HtmlSourceDependency extends ModuleDependency {
86
105
  * @param {ObjectDeserializerContext} context context
87
106
  */
88
107
  deserialize(context) {
108
+ const { read } = context;
109
+ this.prefetch = read();
110
+ this.preload = read();
111
+ this.fetchPriority = read();
112
+ this.asAttribute = read();
113
+ this.typeAttribute = read();
114
+ this.mediaAttribute = read();
89
115
  super.deserialize(context);
90
116
  }
91
117
  }
@@ -6,6 +6,7 @@
6
6
  "use strict";
7
7
 
8
8
  const Dependency = require("../Dependency");
9
+ const Template = require("../Template");
9
10
  const makeSerializable = require("../util/makeSerializable");
10
11
  const memoize = require("../util/memoize");
11
12
  const { ImportPhaseUtils } = require("./ImportPhase");
@@ -50,6 +51,11 @@ class ImportDependency extends ModuleDependency {
50
51
  this.attributes = attributes;
51
52
  /** @type {DependencyGuard[] | undefined} */
52
53
  this.branchGuards = undefined;
54
+ // Range of the `import(specifier, options)` second argument, set only when
55
+ // it is not a statically extractable attributes object and must therefore
56
+ // be evaluated and validated at runtime.
57
+ /** @type {Range | undefined} */
58
+ this.optionsRange = undefined;
53
59
  }
54
60
 
55
61
  get type() {
@@ -137,6 +143,7 @@ class ImportDependency extends ModuleDependency {
137
143
  context.write(this.phase);
138
144
  context.write(this.attributes);
139
145
  context.write(this.branchGuards);
146
+ context.write(this.optionsRange);
140
147
  super.serialize(context);
141
148
  }
142
149
 
@@ -150,6 +157,7 @@ class ImportDependency extends ModuleDependency {
150
157
  this.phase = context.read();
151
158
  this.attributes = context.read();
152
159
  this.branchGuards = context.read();
160
+ this.optionsRange = context.read();
153
161
  super.deserialize(context);
154
162
  }
155
163
  }
@@ -200,7 +208,8 @@ ImportDependency.Template = class ImportDependencyTemplate extends (
200
208
  strict: /** @type {BuildMeta} */ (module.buildMeta).strictHarmonyModule,
201
209
  dependency: dep,
202
210
  message: "import()",
203
- runtimeRequirements
211
+ runtimeRequirements,
212
+ originModule: module
204
213
  });
205
214
 
206
215
  // For source phase imports, unwrap the default export
@@ -212,6 +221,39 @@ ImportDependency.Template = class ImportDependencyTemplate extends (
212
221
  )})`;
213
222
  }
214
223
 
224
+ // A non-static second argument must still be evaluated (for its side
225
+ // effects and evaluation order) and validated per spec. Keep it in place
226
+ // and wrap it in an inline validator that mirrors the runtime checks of
227
+ // the spec's `import(specifier, options)` evaluation.
228
+ if (dep.optionsRange) {
229
+ const validator = runtimeTemplate.basicFunction("o", [
230
+ "try {",
231
+ Template.indent([
232
+ "if (o !== undefined) {",
233
+ Template.indent([
234
+ 'if ((typeof o !== "object" && typeof o !== "function") || o === null) throw new TypeError("The second argument to import() must be an object");',
235
+ 'var a = o["with"];',
236
+ "if (a !== undefined) {",
237
+ Template.indent([
238
+ 'if ((typeof a !== "object" && typeof a !== "function") || a === null) throw new TypeError("The \'with\' option must be an object");',
239
+ "for (var k = Object.keys(a), i = 0; i < k.length; i++) {",
240
+ Template.indent([
241
+ 'if (typeof a[k[i]] !== "string") throw new TypeError("Import attribute values must be strings");'
242
+ ]),
243
+ "}"
244
+ ]),
245
+ "}"
246
+ ]),
247
+ "}"
248
+ ]),
249
+ "} catch (e) { return Promise.reject(e); }",
250
+ `return ${content};`
251
+ ]);
252
+ source.replace(dep.range[0], dep.optionsRange[0] - 1, `(${validator})(`);
253
+ source.replace(dep.optionsRange[1], dep.range[1] - 1, ")");
254
+ return;
255
+ }
256
+
215
257
  source.replace(dep.range[0], dep.range[1] - 1, content);
216
258
  }
217
259
  };
@@ -29,18 +29,18 @@ const ImportMetaContextDependency = require("./ImportMetaContextDependency");
29
29
 
30
30
  /**
31
31
  * Creates a property parse error.
32
+ * @param {JavascriptParser} parser the parser
32
33
  * @param {Property} prop property
33
34
  * @param {string} expect except message
34
35
  * @returns {WebpackError} error
35
36
  */
36
- function createPropertyParseError(prop, expect) {
37
+ function createPropertyParseError(parser, prop, expect) {
37
38
  return createError(
38
39
  `Parsing import.meta.webpackContext options failed. Unknown value for property ${JSON.stringify(
39
40
  /** @type {Identifier} */
40
41
  (prop.key).name
41
42
  )}, expected type ${expect}.`,
42
- /** @type {DependencyLocation} */
43
- (prop.value.loc)
43
+ parser.getLocation(prop.value)
44
44
  );
45
45
  }
46
46
 
@@ -110,8 +110,7 @@ module.exports = class ImportMetaContextDependencyParserPlugin {
110
110
  errors.push(
111
111
  createError(
112
112
  "Parsing import.meta.webpackContext options failed.",
113
- /** @type {DependencyLocation} */
114
- (optionsNode.loc)
113
+ parser.getLocation(optionsNode)
115
114
  )
116
115
  );
117
116
  break;
@@ -122,7 +121,7 @@ module.exports = class ImportMetaContextDependencyParserPlugin {
122
121
  /** @type {Expression} */ (prop.value)
123
122
  );
124
123
  if (!regExpExpr.isRegExp()) {
125
- errors.push(createPropertyParseError(prop, "RegExp"));
124
+ errors.push(createPropertyParseError(parser, prop, "RegExp"));
126
125
  } else {
127
126
  regExp = /** @type {RegExp} */ (regExpExpr.regExp);
128
127
  }
@@ -133,7 +132,7 @@ module.exports = class ImportMetaContextDependencyParserPlugin {
133
132
  /** @type {Expression} */ (prop.value)
134
133
  );
135
134
  if (!regExpExpr.isRegExp()) {
136
- errors.push(createPropertyParseError(prop, "RegExp"));
135
+ errors.push(createPropertyParseError(parser, prop, "RegExp"));
137
136
  } else {
138
137
  include = regExpExpr.regExp;
139
138
  }
@@ -144,7 +143,7 @@ module.exports = class ImportMetaContextDependencyParserPlugin {
144
143
  /** @type {Expression} */ (prop.value)
145
144
  );
146
145
  if (!regExpExpr.isRegExp()) {
147
- errors.push(createPropertyParseError(prop, "RegExp"));
146
+ errors.push(createPropertyParseError(parser, prop, "RegExp"));
148
147
  } else {
149
148
  exclude = regExpExpr.regExp;
150
149
  }
@@ -155,7 +154,7 @@ module.exports = class ImportMetaContextDependencyParserPlugin {
155
154
  /** @type {Expression} */ (prop.value)
156
155
  );
157
156
  if (!modeExpr.isString()) {
158
- errors.push(createPropertyParseError(prop, "string"));
157
+ errors.push(createPropertyParseError(parser, prop, "string"));
159
158
  } else {
160
159
  mode = /** @type {ContextModuleOptions["mode"]} */ (
161
160
  modeExpr.string
@@ -168,7 +167,7 @@ module.exports = class ImportMetaContextDependencyParserPlugin {
168
167
  /** @type {Expression} */ (prop.value)
169
168
  );
170
169
  if (!expr.isString()) {
171
- errors.push(createPropertyParseError(prop, "string"));
170
+ errors.push(createPropertyParseError(parser, prop, "string"));
172
171
  } else {
173
172
  chunkName = expr.string;
174
173
  }
@@ -206,12 +205,16 @@ module.exports = class ImportMetaContextDependencyParserPlugin {
206
205
  }
207
206
  } else {
208
207
  errors.push(
209
- createPropertyParseError(prop, "string|string[][]")
208
+ createPropertyParseError(
209
+ parser,
210
+ prop,
211
+ "string|string[][]"
212
+ )
210
213
  );
211
214
  }
212
215
  } else {
213
216
  errors.push(
214
- createPropertyParseError(prop, "string|string[][]")
217
+ createPropertyParseError(parser, prop, "string|string[][]")
215
218
  );
216
219
  }
217
220
  break;
@@ -225,7 +228,9 @@ module.exports = class ImportMetaContextDependencyParserPlugin {
225
228
  } else if (expr.isNumber()) {
226
229
  groupOptions.prefetchOrder = expr.number;
227
230
  } else {
228
- errors.push(createPropertyParseError(prop, "boolean|number"));
231
+ errors.push(
232
+ createPropertyParseError(parser, prop, "boolean|number")
233
+ );
229
234
  }
230
235
  break;
231
236
  }
@@ -238,7 +243,9 @@ module.exports = class ImportMetaContextDependencyParserPlugin {
238
243
  } else if (expr.isNumber()) {
239
244
  groupOptions.preloadOrder = expr.number;
240
245
  } else {
241
- errors.push(createPropertyParseError(prop, "boolean|number"));
246
+ errors.push(
247
+ createPropertyParseError(parser, prop, "boolean|number")
248
+ );
242
249
  }
243
250
  break;
244
251
  }
@@ -258,7 +265,11 @@ module.exports = class ImportMetaContextDependencyParserPlugin {
258
265
  );
259
266
  } else {
260
267
  errors.push(
261
- createPropertyParseError(prop, '"high"|"low"|"auto"')
268
+ createPropertyParseError(
269
+ parser,
270
+ prop,
271
+ '"high"|"low"|"auto"'
272
+ )
262
273
  );
263
274
  }
264
275
  break;
@@ -268,7 +279,9 @@ module.exports = class ImportMetaContextDependencyParserPlugin {
268
279
  /** @type {Expression} */ (prop.value)
269
280
  );
270
281
  if (!recursiveExpr.isBoolean()) {
271
- errors.push(createPropertyParseError(prop, "boolean"));
282
+ errors.push(
283
+ createPropertyParseError(parser, prop, "boolean")
284
+ );
272
285
  } else {
273
286
  recursive = /** @type {boolean} */ (recursiveExpr.bool);
274
287
  }
@@ -280,7 +293,7 @@ module.exports = class ImportMetaContextDependencyParserPlugin {
280
293
  `Parsing import.meta.webpackContext options failed. Unknown property ${JSON.stringify(
281
294
  prop.key.name
282
295
  )}.`,
283
- /** @type {DependencyLocation} */ (optionsNode.loc)
296
+ parser.getLocation(optionsNode)
284
297
  )
285
298
  );
286
299
  }
@@ -306,7 +319,7 @@ module.exports = class ImportMetaContextDependencyParserPlugin {
306
319
  },
307
320
  /** @type {Range} */ (expr.range)
308
321
  );
309
- dep.loc = /** @type {DependencyLocation} */ (expr.loc);
322
+ dep.loc = parser.getLocation(expr);
310
323
  dep.optional = Boolean(parser.scope.inTry);
311
324
  parser.state.current.addDependency(dep);
312
325
  return true;
@@ -12,6 +12,9 @@ const {
12
12
  const ContextElementDependency = require("./ContextElementDependency");
13
13
  const ImportMetaContextDependency = require("./ImportMetaContextDependency");
14
14
  const ImportMetaContextDependencyParserPlugin = require("./ImportMetaContextDependencyParserPlugin");
15
+ const ImportMetaGlobDependency = require("./ImportMetaGlobDependency");
16
+ const ImportMetaGlobDependencyParserPlugin = require("./ImportMetaGlobDependencyParserPlugin");
17
+ const { isImportMetaFieldEnabled } = require("./ImportMetaPlugin");
15
18
 
16
19
  /** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
17
20
  /** @typedef {import("../Compiler")} Compiler */
@@ -19,6 +22,28 @@ const ImportMetaContextDependencyParserPlugin = require("./ImportMetaContextDepe
19
22
 
20
23
  const PLUGIN_NAME = "ImportMetaContextPlugin";
21
24
 
25
+ /** @type {(parserOptions: JavascriptParserOptions) => boolean} */
26
+ const isImportMetaContextEnabled = (parserOptions) => {
27
+ const importMeta = parserOptions.importMeta;
28
+ if (
29
+ Boolean(importMeta) &&
30
+ typeof importMeta === "object" &&
31
+ Object.prototype.hasOwnProperty.call(importMeta, "webpackContext")
32
+ ) {
33
+ return isImportMetaFieldEnabled(importMeta, "webpackContext");
34
+ }
35
+ if (parserOptions.importMetaContext === false) {
36
+ return false;
37
+ }
38
+ return isImportMetaFieldEnabled(importMeta, "webpackContext");
39
+ };
40
+
41
+ // Gated only by the `importMeta.glob` field — independent of the legacy
42
+ // `importMetaContext` toggle, which controls `import.meta.webpackContext`.
43
+ /** @type {(parserOptions: JavascriptParserOptions) => boolean} */
44
+ const isImportMetaGlobEnabled = (parserOptions) =>
45
+ isImportMetaFieldEnabled(parserOptions.importMeta, "glob");
46
+
22
47
  class ImportMetaContextPlugin {
23
48
  /**
24
49
  * Applies the plugin by registering its hooks on the compiler.
@@ -37,6 +62,14 @@ class ImportMetaContextPlugin {
37
62
  ImportMetaContextDependency,
38
63
  new ImportMetaContextDependency.Template()
39
64
  );
65
+ compilation.dependencyFactories.set(
66
+ ImportMetaGlobDependency,
67
+ contextModuleFactory
68
+ );
69
+ compilation.dependencyTemplates.set(
70
+ ImportMetaGlobDependency,
71
+ new ImportMetaGlobDependency.Template()
72
+ );
40
73
  compilation.dependencyFactories.set(
41
74
  ContextElementDependency,
42
75
  normalModuleFactory
@@ -49,14 +82,12 @@ class ImportMetaContextPlugin {
49
82
  * @returns {void}
50
83
  */
51
84
  const handler = (parser, parserOptions) => {
52
- if (
53
- parserOptions.importMetaContext !== undefined &&
54
- !parserOptions.importMetaContext
55
- ) {
56
- return;
85
+ if (isImportMetaContextEnabled(parserOptions)) {
86
+ new ImportMetaContextDependencyParserPlugin().apply(parser);
87
+ }
88
+ if (isImportMetaGlobEnabled(parserOptions)) {
89
+ new ImportMetaGlobDependencyParserPlugin().apply(parser);
57
90
  }
58
-
59
- new ImportMetaContextDependencyParserPlugin().apply(parser);
60
91
  };
61
92
 
62
93
  normalModuleFactory.hooks.parser
@@ -0,0 +1,81 @@
1
+ /*
2
+ MIT License http://www.opensource.org/licenses/mit-license.php
3
+ Author Natsu @xiaoxiaojx
4
+ */
5
+
6
+ "use strict";
7
+
8
+ const makeSerializable = require("../util/makeSerializable");
9
+ const ContextDependency = require("./ContextDependency");
10
+ const ModuleDependencyTemplateAsRequireId = require("./ModuleDependencyTemplateAsRequireId");
11
+
12
+ /** @typedef {import("../javascript/JavascriptParser").Range} Range */
13
+ /** @typedef {import("./ContextDependency").ContextDependencyOptions} ContextDependencyOptions */
14
+
15
+ /**
16
+ * @typedef {ContextDependencyOptions & {
17
+ * patterns: string[],
18
+ * requestContext: string,
19
+ * importName?: string,
20
+ * exhaustive?: boolean,
21
+ * caseSensitive?: boolean,
22
+ * }} ImportMetaGlobDependencyOptions
23
+ */
24
+
25
+ class ImportMetaGlobDependency extends ContextDependency {
26
+ /**
27
+ * @param {ImportMetaGlobDependencyOptions} options options
28
+ * @param {Range} range range
29
+ */
30
+ constructor(options, range) {
31
+ super(options, options ? options.requestContext : undefined);
32
+
33
+ this.range = range;
34
+ }
35
+
36
+ get category() {
37
+ return "esm";
38
+ }
39
+
40
+ get type() {
41
+ return `import.meta.glob ${this.options.mode}`;
42
+ }
43
+
44
+ /**
45
+ * Returns an identifier to merge equal requests.
46
+ * @returns {string | null} an identifier to merge equal requests
47
+ */
48
+ getResourceIdentifier() {
49
+ const {
50
+ patterns,
51
+ request,
52
+ recursive,
53
+ mode,
54
+ exhaustive,
55
+ caseSensitive,
56
+ importName,
57
+ referencedExports
58
+ } = /** @type {ImportMetaGlobDependencyOptions} */ (this.options);
59
+ return (
60
+ `context${this.getContext() || ""}|glob request${request} ` +
61
+ `${recursive} ${patterns.join(",")} ` +
62
+ `${mode}${exhaustive ? " exhaustive" : ""}` +
63
+ `${caseSensitive === false ? " case-insensitive" : ""}` +
64
+ `${importName ? ` import:${importName}` : ""}` +
65
+ `${
66
+ referencedExports
67
+ ? ` referencedExports:${JSON.stringify(referencedExports)}`
68
+ : ""
69
+ }`
70
+ );
71
+ }
72
+ }
73
+
74
+ makeSerializable(
75
+ ImportMetaGlobDependency,
76
+ "webpack/lib/dependencies/ImportMetaGlobDependency"
77
+ );
78
+
79
+ ImportMetaGlobDependency.Template = ModuleDependencyTemplateAsRequireId;
80
+
81
+ module.exports = ImportMetaGlobDependency;
@@ -0,0 +1,78 @@
1
+ /*
2
+ MIT License http://www.opensource.org/licenses/mit-license.php
3
+ Author Natsu @xiaoxiaojx
4
+ */
5
+
6
+ "use strict";
7
+
8
+ const {
9
+ evaluateToIdentifier
10
+ } = require("../javascript/JavascriptParserHelpers");
11
+ const ImportMetaGlobDependency = require("./ImportMetaGlobDependency");
12
+ const { parseImportMetaGlobCall } = require("./ImportMetaGlobHelpers");
13
+
14
+ /** @typedef {import("estree").CallExpression} CallExpression */
15
+ /** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
16
+ /** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
17
+
18
+ const PLUGIN_NAME = "ImportMetaGlobDependencyParserPlugin";
19
+
20
+ module.exports = class ImportMetaGlobDependencyParserPlugin {
21
+ /**
22
+ * Applies the plugin by registering its hooks on the compiler.
23
+ * @param {JavascriptParser} parser the parser
24
+ * @returns {void}
25
+ */
26
+ apply(parser) {
27
+ parser.hooks.evaluateIdentifier
28
+ .for("import.meta.glob")
29
+ .tap(PLUGIN_NAME, (expr) =>
30
+ evaluateToIdentifier(
31
+ "import.meta.glob",
32
+ "import.meta",
33
+ () => ["glob"],
34
+ true
35
+ )(expr)
36
+ );
37
+ parser.hooks.call.for("import.meta.glob").tap(PLUGIN_NAME, (expr) => {
38
+ const {
39
+ options: globOptions,
40
+ errors,
41
+ warnings
42
+ } = parseImportMetaGlobCall(/** @type {CallExpression} */ (expr), parser);
43
+ for (const warning of warnings) {
44
+ parser.state.module.addWarning(warning);
45
+ }
46
+ if (errors.length) {
47
+ for (const error of errors) parser.state.current.addError(error);
48
+ return;
49
+ }
50
+ if (!globOptions) return;
51
+ const dep = new ImportMetaGlobDependency(
52
+ {
53
+ request: `${globOptions.baseDir}${globOptions.query}`,
54
+ recursive: globOptions.recursive,
55
+ regExp: false,
56
+ patterns: globOptions.patterns,
57
+ requestContext: globOptions.requestContext,
58
+ importName: globOptions.importName,
59
+ exhaustive: globOptions.exhaustive,
60
+ caseSensitive: globOptions.caseSensitive,
61
+ referencedExports: globOptions.referencedExports,
62
+ namespaceObject:
63
+ /** @type {import("../Module").BuildMeta} */
64
+ (parser.state.module.buildMeta).strictHarmonyModule
65
+ ? "strict"
66
+ : true,
67
+ mode: globOptions.mode,
68
+ category: "esm"
69
+ },
70
+ globOptions.range
71
+ );
72
+ dep.loc = parser.getLocation(expr);
73
+ dep.optional = Boolean(parser.scope.inTry);
74
+ parser.state.current.addDependency(dep);
75
+ return true;
76
+ });
77
+ }
78
+ };