webpack 5.108.3 → 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 (191) 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 +50 -8
  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/FlagDependencyExportsPlugin.js +55 -8
  16. package/lib/HotModuleReplacementPlugin.js +18 -15
  17. package/lib/LazyBarrel.js +30 -3
  18. package/lib/Module.js +3 -0
  19. package/lib/ModuleFilenameHelpers.js +2 -2
  20. package/lib/ModuleTypeConstants.js +8 -1
  21. package/lib/MultiCompiler.js +13 -2
  22. package/lib/MultiWatching.js +1 -0
  23. package/lib/NodeStuffPlugin.js +51 -33
  24. package/lib/NormalModule.js +169 -405
  25. package/lib/NormalModuleFactory.js +76 -2
  26. package/lib/ProgressPlugin.js +211 -119
  27. package/lib/ProvidePlugin.js +2 -2
  28. package/lib/RuntimeGlobals.js +47 -0
  29. package/lib/RuntimePlugin.js +33 -0
  30. package/lib/RuntimeTemplate.js +304 -16
  31. package/lib/UseStrictPlugin.js +1 -1
  32. package/lib/WebpackIsIncludedPlugin.js +1 -1
  33. package/lib/WebpackOptionsApply.js +24 -19
  34. package/lib/asset/AssetGenerator.js +110 -28
  35. package/lib/asset/AssetModulesPlugin.js +92 -2
  36. package/lib/asset/WebManifestGenerator.js +164 -0
  37. package/lib/asset/WebManifestParser.js +130 -0
  38. package/lib/async-modules/AwaitDependenciesInitFragment.js +14 -4
  39. package/lib/async-modules/isGeneratorLowered.js +26 -0
  40. package/lib/cache/PackFileCacheStrategy.js +9 -3
  41. package/lib/config/browserslistTargetHandler.js +40 -0
  42. package/lib/config/defaults.js +222 -32
  43. package/lib/config/normalization.js +26 -0
  44. package/lib/config/target.js +9 -1
  45. package/lib/css/CssGenerator.js +73 -73
  46. package/lib/css/CssLoadingRuntimeModule.js +5 -1
  47. package/lib/css/CssModulesPlugin.js +92 -53
  48. package/lib/css/CssParser.js +843 -302
  49. package/lib/css/syntax.js +1219 -483
  50. package/lib/dependencies/AMDDefineDependencyParserPlugin.js +6 -6
  51. package/lib/dependencies/AMDPlugin.js +2 -2
  52. package/lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js +13 -13
  53. package/lib/dependencies/CommonJsDependencyHelpers.js +39 -0
  54. package/lib/dependencies/CommonJsExportsDependency.js +47 -2
  55. package/lib/dependencies/CommonJsExportsParserPlugin.js +10 -10
  56. package/lib/dependencies/CommonJsImportsParserPlugin.js +18 -18
  57. package/lib/dependencies/CommonJsPlugin.js +2 -2
  58. package/lib/dependencies/CommonJsRequireDependency.js +26 -1
  59. package/lib/dependencies/CommonJsSelfReferenceDependency.js +32 -1
  60. package/lib/dependencies/ContextDependencyHelpers.js +3 -3
  61. package/lib/dependencies/CreateRequireParserPlugin.js +6 -6
  62. package/lib/dependencies/CssIcssExportDependency.js +190 -87
  63. package/lib/dependencies/CssUrlDependency.js +35 -4
  64. package/lib/dependencies/ExternalModuleInitFragmentDependency.js +2 -2
  65. package/lib/dependencies/HarmonyCompatibilityDependency.js +32 -14
  66. package/lib/dependencies/HarmonyDetectionParserPlugin.js +43 -13
  67. package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +7 -19
  68. package/lib/dependencies/HarmonyImportDependency.js +12 -1
  69. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +34 -107
  70. package/lib/dependencies/HarmonyImportGuard.js +192 -20
  71. package/lib/dependencies/HarmonyModulesPlugin.js +6 -0
  72. package/lib/dependencies/HarmonyTopLevelThisParserPlugin.js +1 -1
  73. package/lib/dependencies/HtmlEntryDependency.js +797 -172
  74. package/lib/dependencies/HtmlSourceDependency.js +26 -0
  75. package/lib/dependencies/ImportDependency.js +43 -1
  76. package/lib/dependencies/ImportMetaContextDependencyParserPlugin.js +31 -18
  77. package/lib/dependencies/ImportMetaContextPlugin.js +38 -7
  78. package/lib/dependencies/ImportMetaGlobDependency.js +81 -0
  79. package/lib/dependencies/ImportMetaGlobDependencyParserPlugin.js +78 -0
  80. package/lib/dependencies/ImportMetaGlobHelpers.js +660 -0
  81. package/lib/dependencies/ImportMetaPlugin.js +532 -180
  82. package/lib/dependencies/ImportMetaResolveDependency.js +90 -0
  83. package/lib/dependencies/ImportParserPlugin.js +87 -26
  84. package/lib/dependencies/ImportPhase.js +10 -8
  85. package/lib/dependencies/LoaderPlugin.js +5 -1
  86. package/lib/dependencies/RequireContextDependencyParserPlugin.js +1 -1
  87. package/lib/dependencies/RequireEnsureDependenciesBlockParserPlugin.js +3 -6
  88. package/lib/dependencies/RequireIncludeDependencyParserPlugin.js +4 -11
  89. package/lib/dependencies/SystemPlugin.js +2 -4
  90. package/lib/dependencies/TopLevelAwaitDependency.js +93 -0
  91. package/lib/dependencies/URLDependency.js +141 -21
  92. package/lib/dependencies/{WorkerPlugin.js → WorkerAndWorkletPlugin.js} +324 -112
  93. package/lib/dependencies/WorkerDependency.js +86 -18
  94. package/lib/dependencies/WorkletDependency.js +278 -0
  95. package/lib/dll/DelegatedModuleFactoryPlugin.js +19 -0
  96. package/lib/errors/EnvironmentNotSupportAsyncWarning.js +1 -1
  97. package/lib/errors/LoaderLoadingError.js +20 -0
  98. package/lib/errors/ModuleParseError.js +39 -22
  99. package/lib/errors/NodeStuffInWebError.js +1 -1
  100. package/lib/esm/ModuleChunkFormatPlugin.js +47 -14
  101. package/lib/esm/ModuleChunkLoadingPlugin.js +8 -1
  102. package/lib/esm/ModuleChunkLoadingRuntimeModule.js +29 -1
  103. package/lib/hmr/lazyCompilationBackend.js +32 -8
  104. package/lib/html/HtmlGenerator.js +940 -91
  105. package/lib/html/HtmlModule.js +3 -2
  106. package/lib/html/HtmlModulesPlugin.js +625 -170
  107. package/lib/html/HtmlParser.js +812 -340
  108. package/lib/html/favicon.svg +1 -0
  109. package/lib/html/syntax.js +2116 -834
  110. package/lib/ids/IdHelpers.js +1 -1
  111. package/lib/index.js +6 -0
  112. package/lib/javascript/ArrayPushCallbackChunkFormatPlugin.js +4 -5
  113. package/lib/javascript/BasicEvaluatedExpression.js +366 -48
  114. package/lib/javascript/JavascriptGenerator.js +197 -49
  115. package/lib/javascript/JavascriptModulesPlugin.js +178 -24
  116. package/lib/javascript/JavascriptParser.js +711 -355
  117. package/lib/javascript/JavascriptParserHelpers.js +3 -6
  118. package/lib/javascript/syntax.js +4380 -0
  119. package/lib/library/ModuleLibraryPlugin.js +38 -1
  120. package/lib/loaders/LoaderRunner.js +714 -0
  121. package/lib/loaders/loadLoader.js +100 -0
  122. package/lib/node/ReadFileCompileAsyncWasmPlugin.js +19 -0
  123. package/lib/node/ReadFileCompileWasmPlugin.js +14 -0
  124. package/lib/optimize/ConcatenatedModule.js +120 -16
  125. package/lib/optimize/FlagIncludedChunksPlugin.js +13 -3
  126. package/lib/optimize/InnerGraphPlugin.js +50 -30
  127. package/lib/optimize/ModuleConcatenationPlugin.js +69 -3
  128. package/lib/optimize/SideEffectsFlagPlugin.js +86 -43
  129. package/lib/prefetch/ChunkPrefetchPreloadPlugin.js +15 -0
  130. package/lib/prefetch/ChunkPreloadTriggerRuntimeModule.js +24 -13
  131. package/lib/prefetch/ResourceHintPlugin.js +663 -0
  132. package/lib/prefetch/ResourceHintRuntimeModule.js +149 -0
  133. package/lib/prefetch/StartupAssetHintRuntimeModule.js +210 -0
  134. package/lib/prefetch/parseResourceHintOptions.js +103 -0
  135. package/lib/rules/RuleSetCompiler.js +114 -0
  136. package/lib/runtime/AsyncModuleGeneratorRuntimeModule.js +56 -0
  137. package/lib/runtime/AsyncModuleRuntimeModule.js +24 -8
  138. package/lib/runtime/AutoPublicPathRuntimeModule.js +7 -1
  139. package/lib/runtime/CommonJsWrapRuntimeModule.js +37 -0
  140. package/lib/runtime/GetWorkletBootstrapRuntimeModule.js +59 -0
  141. package/lib/runtime/MakeDeferredNamespaceObjectRuntime.js +204 -8
  142. package/lib/runtime/SetAnonymousDefaultNameRuntimeModule.js +4 -1
  143. package/lib/schemes/HttpUriPlugin.js +40 -1
  144. package/lib/schemes/VirtualUrlPlugin.js +1 -2
  145. package/lib/serialization/FileMiddleware.js +29 -3
  146. package/lib/stats/DefaultStatsFactoryPlugin.js +32 -3
  147. package/lib/stats/DefaultStatsPresetPlugin.js +2 -0
  148. package/lib/stats/DefaultStatsPrinterPlugin.js +12 -2
  149. package/lib/stats/StatsFactory.js +15 -8
  150. package/lib/url/URLParserPlugin.js +98 -19
  151. package/lib/util/AsyncQueue.js +122 -72
  152. package/lib/util/LocConverter.js +9 -8
  153. package/lib/util/SetHelpers.js +1 -1
  154. package/lib/util/SourceProcessor.js +41 -28
  155. package/lib/util/StackedMap.js +122 -48
  156. package/lib/util/binarySearchBounds.js +2 -2
  157. package/lib/util/concatenate.js +40 -14
  158. package/lib/util/createHooksRegistry.js +13 -5
  159. package/lib/util/dataURL.js +2 -2
  160. package/lib/util/deterministicGrouping.js +7 -5
  161. package/lib/util/formatSize.js +6 -2
  162. package/lib/util/globUtils.js +710 -0
  163. package/lib/util/identifier.js +23 -6
  164. package/lib/util/implicitTypeLoaderFallback.js +62 -0
  165. package/lib/util/internalSerializables.js +88 -71
  166. package/lib/util/magicComment.js +121 -25
  167. package/lib/util/numberHash.js +2 -2
  168. package/lib/wasm-async/AsyncWasmCompileRuntimeModule.js +24 -18
  169. package/lib/wasm-async/AsyncWasmLoadingRuntimeModule.js +33 -22
  170. package/lib/wasm-sync/WasmChunkLoadingRuntimeModule.js +79 -16
  171. package/lib/web/FetchCompileAsyncWasmPlugin.js +19 -0
  172. package/lib/web/FetchCompileWasmPlugin.js +14 -0
  173. package/lib/web/JsonpChunkLoadingRuntimeModule.js +2 -0
  174. package/lib/webpack.js +65 -0
  175. package/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js +3 -0
  176. package/module.d.ts +21 -0
  177. package/package.json +14 -13
  178. package/schemas/WebpackOptions.check.js +1 -1
  179. package/schemas/WebpackOptions.json +759 -36
  180. package/schemas/plugins/HtmlParserOptions.check.js +1 -1
  181. package/schemas/plugins/ProgressPlugin.check.js +1 -1
  182. package/schemas/plugins/ProgressPlugin.json +17 -1
  183. package/schemas/plugins/container/ContainerReferencePlugin.check.js +1 -1
  184. package/schemas/plugins/container/ContainerReferencePlugin.json +1 -0
  185. package/schemas/plugins/container/ExternalsType.check.js +1 -1
  186. package/schemas/plugins/container/ModuleFederationPlugin.check.js +1 -1
  187. package/schemas/plugins/container/ModuleFederationPlugin.json +1 -0
  188. package/schemas/plugins/css/CssAutoOrModuleParserOptions.check.js +1 -1
  189. package/schemas/plugins/css/CssModuleParserOptions.check.js +1 -1
  190. package/schemas/plugins/css/CssParserOptions.check.js +1 -1
  191. package/types.d.ts +1300 -325
@@ -0,0 +1,660 @@
1
+ /*
2
+ MIT License http://www.opensource.org/licenses/mit-license.php
3
+ Author Natsu @xiaoxiaojx
4
+ */
5
+
6
+ "use strict";
7
+
8
+ const path = require("path");
9
+ const UnsupportedFeatureWarning = require("../errors/UnsupportedFeatureWarning");
10
+ const WebpackError = require("../errors/WebpackError");
11
+ const {
12
+ commonGlobBaseDir,
13
+ globPatternsAreRecursive,
14
+ importMetaGlobPathParts,
15
+ joinImportMetaGlobFsPath,
16
+ joinImportMetaGlobPath,
17
+ normalizePathSeparators,
18
+ normalizePathSeparatorsForPath,
19
+ resolveContextModuleGlobPattern
20
+ } = require("../util/globUtils");
21
+
22
+ /**
23
+ * @param {string} relativePath relative path
24
+ * @returns {string} request
25
+ */
26
+ const relativePathToRequest = (relativePath) => {
27
+ if (relativePath === "") return "./.";
28
+ if (relativePath === "..") return "../.";
29
+ if (relativePath.startsWith("../")) return relativePath;
30
+ return `./${relativePath}`;
31
+ };
32
+
33
+ /** @typedef {import("../ContextModule").ContextModuleOptions} ContextModuleOptions */
34
+ /** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
35
+ /** @typedef {import("../javascript/JavascriptParser").Range} Range */
36
+ /** @typedef {import("../Dependency").RawReferencedExports} RawReferencedExports */
37
+ /** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
38
+ /** @typedef {import("estree").CallExpression} CallExpression */
39
+ /** @typedef {import("estree").Expression} Expression */
40
+ /** @typedef {import("estree").ObjectExpression} ObjectExpression */
41
+ /** @typedef {import("estree").Property} Property */
42
+ /** @typedef {import("./ImportMetaGlobDependency").ImportMetaGlobDependencyOptions} ImportMetaGlobDependencyOptions */
43
+
44
+ /**
45
+ * @typedef {object} ParsedImportMetaGlobOptions
46
+ * @property {string[]} patterns patterns
47
+ * @property {string} requestContext request context
48
+ * @property {string} baseDir base directory
49
+ * @property {boolean} recursive recursive
50
+ * @property {ContextModuleOptions["mode"]} mode mode
51
+ * @property {string | undefined} importName import name
52
+ * @property {string} query query
53
+ * @property {boolean} exhaustive exhaustive
54
+ * @property {boolean} caseSensitive case sensitive
55
+ * @property {RawReferencedExports | undefined} referencedExports referenced exports
56
+ * @property {Range} range range
57
+ */
58
+
59
+ const SUPPORTED_IMPORT_META_GLOB_OPTIONS =
60
+ "eager, import, query, base, exhaustive, caseSensitive";
61
+
62
+ /**
63
+ * @param {string} msg message
64
+ * @param {DependencyLocation} loc location
65
+ * @returns {WebpackError} error
66
+ */
67
+ const createImportMetaGlobError = (msg, loc) => {
68
+ const error = new WebpackError(msg);
69
+ error.name = "ImportMetaGlobError";
70
+ error.loc = loc;
71
+ return error;
72
+ };
73
+
74
+ /**
75
+ * @param {string} msg message
76
+ * @param {DependencyLocation} loc location
77
+ * @returns {UnsupportedFeatureWarning} warning
78
+ */
79
+ const createImportMetaGlobWarning = (msg, loc) =>
80
+ new UnsupportedFeatureWarning(msg, loc);
81
+
82
+ /**
83
+ * @param {Expression} expr expression
84
+ * @param {JavascriptParser} parser parser
85
+ * @returns {string | undefined} static string
86
+ */
87
+ const staticStringFromExpr = (expr, parser) => {
88
+ const evaluated = parser.evaluateExpression(expr);
89
+ if (evaluated.isString()) {
90
+ return /** @type {string} */ (evaluated.string);
91
+ }
92
+ if (expr.type === "TemplateLiteral" && expr.expressions.length === 0) {
93
+ const cooked = /** @type {import("estree").TemplateElement} */ (
94
+ expr.quasis[0]
95
+ ).value.cooked;
96
+ if (cooked !== null && cooked !== undefined) return cooked;
97
+ }
98
+ };
99
+
100
+ /**
101
+ * @param {import("estree").Property["key"]} propName property name
102
+ * @returns {string | undefined} key
103
+ */
104
+ const staticOptionKeyFromPropName = (propName) => {
105
+ if (propName.type === "Identifier") return propName.name;
106
+ if (propName.type === "Literal" && typeof propName.value === "string") {
107
+ return propName.value;
108
+ }
109
+ };
110
+
111
+ /**
112
+ * @param {Expression} expr expression
113
+ * @param {JavascriptParser} parser parser
114
+ * @param {UnsupportedFeatureWarning[]} warnings warnings
115
+ * @returns {string[] | undefined} glob patterns
116
+ */
117
+ const parseStaticGlobPatternsFromExpr = (expr, parser, warnings) => {
118
+ const single = staticStringFromExpr(expr, parser);
119
+ if (single !== undefined) return [single];
120
+ if (expr.type !== "ArrayExpression") {
121
+ warnings.push(
122
+ createImportMetaGlobWarning(
123
+ "import.meta.glob() first argument must be a string literal or array of string literals",
124
+ parser.getLocation(expr)
125
+ )
126
+ );
127
+ return;
128
+ }
129
+ /** @type {string[]} */
130
+ const patterns = [];
131
+ for (const elem of expr.elements) {
132
+ if (!elem) {
133
+ warnings.push(
134
+ createImportMetaGlobWarning(
135
+ "import.meta.glob() pattern array elements must be constant strings",
136
+ parser.getLocation(expr)
137
+ )
138
+ );
139
+ return;
140
+ }
141
+ if (elem.type === "SpreadElement") {
142
+ warnings.push(
143
+ createImportMetaGlobWarning(
144
+ "import.meta.glob() pattern array elements must be constant strings",
145
+ parser.getLocation(elem)
146
+ )
147
+ );
148
+ return;
149
+ }
150
+ const pattern = staticStringFromExpr(elem, parser);
151
+ if (pattern === undefined) {
152
+ warnings.push(
153
+ createImportMetaGlobWarning(
154
+ "import.meta.glob() pattern array elements must be constant strings",
155
+ parser.getLocation(elem)
156
+ )
157
+ );
158
+ return;
159
+ }
160
+ patterns.push(pattern);
161
+ }
162
+ if (patterns.length === 0) {
163
+ warnings.push(
164
+ createImportMetaGlobWarning(
165
+ "import.meta.glob() requires at least one pattern",
166
+ parser.getLocation(expr)
167
+ )
168
+ );
169
+ return;
170
+ }
171
+ return patterns;
172
+ };
173
+
174
+ /**
175
+ * @param {string} query query
176
+ * @returns {string} normalized query
177
+ */
178
+ const normalizeImportMetaGlobQuery = (query) => {
179
+ if (query === "" || query.startsWith("?")) return query;
180
+ return `?${query}`;
181
+ };
182
+
183
+ /**
184
+ * @param {import("estree").Property["key"]} propName property name
185
+ * @param {JavascriptParser} parser parser
186
+ * @returns {string | undefined} key
187
+ */
188
+ const staticImportMetaGlobQueryKeyFromPropName = (propName, parser) => {
189
+ if (propName.type === "Identifier") return propName.name;
190
+ if (propName.type === "Literal") {
191
+ if (typeof propName.value === "string") return propName.value;
192
+ if (typeof propName.value === "number") return String(propName.value);
193
+ if (typeof propName.value === "boolean") return String(propName.value);
194
+ }
195
+ const keyNode = /** @type {unknown} */ (propName);
196
+ if (
197
+ typeof keyNode === "object" &&
198
+ keyNode !== null &&
199
+ "type" in keyNode &&
200
+ keyNode.type === "Computed" &&
201
+ "expression" in keyNode
202
+ ) {
203
+ return staticImportMetaGlobQueryKeyFromExpr(
204
+ /** @type {Expression} */ (keyNode.expression),
205
+ parser
206
+ );
207
+ }
208
+ };
209
+
210
+ /**
211
+ * @param {Expression} expr expression
212
+ * @param {JavascriptParser} parser parser
213
+ * @returns {string | undefined} key
214
+ */
215
+ const staticImportMetaGlobQueryKeyFromExpr = (expr, parser) => {
216
+ const str = staticStringFromExpr(expr, parser);
217
+ if (str !== undefined) return str;
218
+ if (expr.type === "Literal") {
219
+ if (typeof expr.value === "number") return String(expr.value);
220
+ if (typeof expr.value === "boolean") return String(expr.value);
221
+ if (expr.value === null) return "null";
222
+ }
223
+ };
224
+
225
+ /**
226
+ * @param {Expression} expr expression
227
+ * @param {JavascriptParser} parser parser
228
+ * @param {UnsupportedFeatureWarning[]} warnings warnings
229
+ * @returns {string | undefined} query
230
+ */
231
+ const parseStaticImportMetaGlobQueryFromExpr = (expr, parser, warnings) => {
232
+ const query = staticStringFromExpr(expr, parser);
233
+ if (query !== undefined) return normalizeImportMetaGlobQuery(query);
234
+ if (expr.type !== "ObjectExpression") {
235
+ warnings.push(
236
+ createImportMetaGlobWarning(
237
+ "import.meta.glob() 'query' option must be a constant string or object",
238
+ parser.getLocation(expr)
239
+ )
240
+ );
241
+ return;
242
+ }
243
+ const params = new URLSearchParams();
244
+ for (const prop of expr.properties) {
245
+ if (prop.type !== "Property") {
246
+ warnings.push(
247
+ createImportMetaGlobWarning(
248
+ "import.meta.glob() 'query' object must only contain constant key-value pairs",
249
+ parser.getLocation(expr)
250
+ )
251
+ );
252
+ return;
253
+ }
254
+ const key = staticImportMetaGlobQueryKeyFromPropName(prop.key, parser);
255
+ if (key === undefined) {
256
+ warnings.push(
257
+ createImportMetaGlobWarning(
258
+ "import.meta.glob() 'query' object keys must be constant strings",
259
+ parser.getLocation(prop.key)
260
+ )
261
+ );
262
+ return;
263
+ }
264
+ const valueExpr = /** @type {Expression} */ (prop.value);
265
+ const valueStr = staticStringFromExpr(valueExpr, parser);
266
+ if (valueStr !== undefined) {
267
+ params.append(key, valueStr);
268
+ } else if (valueExpr.type === "Literal") {
269
+ if (typeof valueExpr.value === "boolean") {
270
+ params.append(key, String(valueExpr.value));
271
+ } else if (typeof valueExpr.value === "number") {
272
+ params.append(key, String(valueExpr.value));
273
+ } else {
274
+ warnings.push(
275
+ createImportMetaGlobWarning(
276
+ `import.meta.glob() 'query' object value for key '${key}' must be a constant string or boolean`,
277
+ parser.getLocation(valueExpr)
278
+ )
279
+ );
280
+ return;
281
+ }
282
+ } else {
283
+ warnings.push(
284
+ createImportMetaGlobWarning(
285
+ `import.meta.glob() 'query' object value for key '${key}' must be a constant string or boolean`,
286
+ parser.getLocation(valueExpr)
287
+ )
288
+ );
289
+ return;
290
+ }
291
+ }
292
+ return normalizeImportMetaGlobQuery(params.toString());
293
+ };
294
+
295
+ /**
296
+ * @param {ObjectExpression | undefined} globOptions options object
297
+ * @param {JavascriptParser} parser parser
298
+ * @param {UnsupportedFeatureWarning[]} warnings warnings
299
+ * @returns {{ eager: boolean, importName: string | undefined, query: string, base: string | undefined, exhaustive: boolean, caseSensitive: boolean }} parsed options
300
+ */
301
+ const parseImportMetaGlobOptionsObject = (globOptions, parser, warnings) => {
302
+ let eager = false;
303
+ /** @type {string | undefined} */
304
+ let importName;
305
+ let query = "";
306
+ /** @type {string | undefined} */
307
+ let base;
308
+ let exhaustive = false;
309
+ let caseSensitive = true;
310
+
311
+ if (!globOptions) {
312
+ return { eager, importName, query, base, exhaustive, caseSensitive };
313
+ }
314
+
315
+ for (const prop of globOptions.properties) {
316
+ if (prop.type !== "Property") {
317
+ warnings.push(
318
+ createImportMetaGlobWarning(
319
+ "import.meta.glob() options object must only contain constant key-value pairs",
320
+ parser.getLocation(globOptions)
321
+ )
322
+ );
323
+ continue;
324
+ }
325
+ if (prop.computed) {
326
+ warnings.push(
327
+ createImportMetaGlobWarning(
328
+ "import.meta.glob() option keys must be constant strings",
329
+ parser.getLocation(prop.key)
330
+ )
331
+ );
332
+ continue;
333
+ }
334
+ const key = staticOptionKeyFromPropName(prop.key);
335
+ if (key === undefined) {
336
+ warnings.push(
337
+ createImportMetaGlobWarning(
338
+ "import.meta.glob() option keys must be constant strings",
339
+ parser.getLocation(prop.key)
340
+ )
341
+ );
342
+ continue;
343
+ }
344
+ const valueExpr = /** @type {Expression} */ (prop.value);
345
+ switch (key) {
346
+ case "eager": {
347
+ const evaluated = parser.evaluateExpression(valueExpr);
348
+ if (evaluated.isBoolean()) {
349
+ eager = /** @type {boolean} */ (evaluated.bool);
350
+ } else {
351
+ warnings.push(
352
+ createImportMetaGlobWarning(
353
+ "import.meta.glob() 'eager' option must be a constant boolean (true or false), defaulting to false",
354
+ parser.getLocation(valueExpr)
355
+ )
356
+ );
357
+ }
358
+ break;
359
+ }
360
+ case "import": {
361
+ const importOption = staticStringFromExpr(valueExpr, parser);
362
+ if (importOption === undefined) {
363
+ warnings.push(
364
+ createImportMetaGlobWarning(
365
+ "import.meta.glob() 'import' option must be a constant string, ignoring",
366
+ parser.getLocation(valueExpr)
367
+ )
368
+ );
369
+ } else if (importOption !== "*") {
370
+ importName = importOption;
371
+ }
372
+ break;
373
+ }
374
+ case "query": {
375
+ const queryValue = parseStaticImportMetaGlobQueryFromExpr(
376
+ valueExpr,
377
+ parser,
378
+ warnings
379
+ );
380
+ if (queryValue !== undefined) query = queryValue;
381
+ break;
382
+ }
383
+ case "base": {
384
+ const baseValue = staticStringFromExpr(valueExpr, parser);
385
+ if (baseValue === undefined) {
386
+ warnings.push(
387
+ createImportMetaGlobWarning(
388
+ "import.meta.glob() 'base' option must be a constant string, ignoring",
389
+ parser.getLocation(valueExpr)
390
+ )
391
+ );
392
+ } else {
393
+ base = baseValue;
394
+ }
395
+ break;
396
+ }
397
+ case "exhaustive": {
398
+ const evaluated = parser.evaluateExpression(valueExpr);
399
+ if (evaluated.isBoolean()) {
400
+ exhaustive = /** @type {boolean} */ (evaluated.bool);
401
+ } else {
402
+ warnings.push(
403
+ createImportMetaGlobWarning(
404
+ "import.meta.glob() 'exhaustive' option must be a constant boolean (true or false), defaulting to false",
405
+ parser.getLocation(valueExpr)
406
+ )
407
+ );
408
+ }
409
+ break;
410
+ }
411
+ case "caseSensitive": {
412
+ const evaluated = parser.evaluateExpression(valueExpr);
413
+ if (evaluated.isBoolean()) {
414
+ caseSensitive = /** @type {boolean} */ (evaluated.bool);
415
+ } else {
416
+ warnings.push(
417
+ createImportMetaGlobWarning(
418
+ "import.meta.glob() 'caseSensitive' option must be a constant boolean (true or false), defaulting to true",
419
+ parser.getLocation(valueExpr)
420
+ )
421
+ );
422
+ }
423
+ break;
424
+ }
425
+ case "as":
426
+ warnings.push(
427
+ createImportMetaGlobWarning(
428
+ "import.meta.glob() 'as' option is not supported. Use 'query' instead (e.g. { query: '?raw' })",
429
+ parser.getLocation(prop)
430
+ )
431
+ );
432
+ break;
433
+ default:
434
+ warnings.push(
435
+ createImportMetaGlobWarning(
436
+ `import.meta.glob() unsupported option '${key}'. Supported options are: ${SUPPORTED_IMPORT_META_GLOB_OPTIONS}`,
437
+ parser.getLocation(prop)
438
+ )
439
+ );
440
+ }
441
+ }
442
+
443
+ return { eager, importName, query, base, exhaustive, caseSensitive };
444
+ };
445
+
446
+ /**
447
+ * @param {string} pattern pattern
448
+ * @param {string} baseContext base context
449
+ * @param {string} compilerContext compiler context
450
+ * @returns {string} normalized pattern
451
+ */
452
+ const normalizeBaseGlobPattern = (pattern, baseContext, compilerContext) => {
453
+ let negative = false;
454
+ let normalizedPattern = pattern;
455
+ if (normalizedPattern.startsWith("!")) {
456
+ negative = true;
457
+ normalizedPattern = normalizedPattern.slice(1);
458
+ }
459
+ normalizedPattern = normalizePathSeparators(normalizedPattern);
460
+ if (!normalizedPattern.startsWith("/")) {
461
+ return negative ? `!${normalizedPattern}` : normalizedPattern;
462
+ }
463
+ const normalizedCompilerContext =
464
+ normalizePathSeparatorsForPath(compilerContext);
465
+ const absolutePattern = joinImportMetaGlobPath(
466
+ normalizedCompilerContext,
467
+ normalizedPattern.slice(1)
468
+ );
469
+ const relativePattern = absolutePathToGlobRequest(
470
+ baseContext,
471
+ absolutePattern
472
+ );
473
+ return negative ? `!${relativePattern}` : relativePattern;
474
+ };
475
+
476
+ /**
477
+ * @param {string} context context
478
+ * @param {string} absolutePath absolute path
479
+ * @returns {string} glob request
480
+ */
481
+ const absolutePathToGlobRequest = (context, absolutePath) => {
482
+ const relativePath = path.posix.relative(context, absolutePath);
483
+ const normalizedPath = normalizePathSeparatorsForPath(relativePath);
484
+ return relativePathToRequest(normalizedPath);
485
+ };
486
+
487
+ /**
488
+ * @param {string[]} patterns patterns
489
+ * @param {string} baseContext base context
490
+ * @param {string} compilerContext compiler context
491
+ * @param {boolean} hasCustomBase has custom base
492
+ * @returns {string[]} normalized patterns
493
+ */
494
+ const normalizeImportMetaGlobPatterns = (
495
+ patterns,
496
+ baseContext,
497
+ compilerContext,
498
+ hasCustomBase
499
+ ) => {
500
+ if (!hasCustomBase) return patterns;
501
+ return patterns.map((pattern) =>
502
+ normalizeBaseGlobPattern(pattern, baseContext, compilerContext)
503
+ );
504
+ };
505
+
506
+ /**
507
+ * @param {string} context context
508
+ * @param {string} compilerContext compiler context
509
+ * @param {string | undefined} base base
510
+ * @returns {string} resolved context
511
+ */
512
+ const resolveImportMetaGlobContext = (context, compilerContext, base) => {
513
+ if (!base) return context;
514
+ const normalizedBase = normalizePathSeparatorsForPath(base);
515
+ const [baseContext, pathToJoin] = importMetaGlobPathParts(
516
+ context,
517
+ compilerContext,
518
+ normalizedBase
519
+ );
520
+ return joinImportMetaGlobFsPath(baseContext, pathToJoin);
521
+ };
522
+
523
+ /**
524
+ * @param {CallExpression} expr call expression
525
+ * @param {JavascriptParser} parser parser
526
+ * @returns {{ options: ParsedImportMetaGlobOptions | undefined, errors: WebpackError[], warnings: UnsupportedFeatureWarning[] }} parse result
527
+ */
528
+ const parseImportMetaGlobCall = (expr, parser) => {
529
+ /** @type {WebpackError[]} */
530
+ const errors = [];
531
+ /** @type {UnsupportedFeatureWarning[]} */
532
+ const warnings = [];
533
+ const callLoc = parser.getLocation(expr);
534
+
535
+ if (expr.arguments.length < 1 || expr.arguments.length > 2) {
536
+ errors.push(
537
+ createImportMetaGlobError(
538
+ "import.meta.glob() requires 1 or 2 arguments",
539
+ callLoc
540
+ )
541
+ );
542
+ return { options: undefined, errors, warnings };
543
+ }
544
+
545
+ const directoryNode = expr.arguments[0];
546
+ if (directoryNode.type === "SpreadElement") {
547
+ warnings.push(
548
+ createImportMetaGlobWarning(
549
+ "import.meta.glob() first argument must be a string literal or array of string literals",
550
+ parser.getLocation(directoryNode)
551
+ )
552
+ );
553
+ return { options: undefined, errors, warnings };
554
+ }
555
+
556
+ const rawGlobPatterns = parseStaticGlobPatternsFromExpr(
557
+ /** @type {Expression} */ (directoryNode),
558
+ parser,
559
+ warnings
560
+ );
561
+ if (!rawGlobPatterns) {
562
+ return { options: undefined, errors, warnings };
563
+ }
564
+
565
+ for (const pattern of rawGlobPatterns) {
566
+ const body = pattern.startsWith("!") ? pattern.slice(1) : pattern;
567
+ if (
568
+ !body.startsWith("/") &&
569
+ !body.startsWith("./") &&
570
+ !body.startsWith("../") &&
571
+ !body.startsWith("**")
572
+ ) {
573
+ warnings.push(
574
+ createImportMetaGlobWarning(
575
+ `import.meta.glob() pattern '${pattern}' must be relative (start with './' or '../'), absolute (start with '/'), or a globstar ('**'); aliases are not resolved`,
576
+ parser.getLocation(directoryNode)
577
+ )
578
+ );
579
+ }
580
+ }
581
+
582
+ const optionsNode = expr.arguments[1];
583
+ if (optionsNode) {
584
+ if (optionsNode.type === "SpreadElement") {
585
+ errors.push(
586
+ createImportMetaGlobError(
587
+ "import.meta.glob() second argument must be an object literal",
588
+ parser.getLocation(optionsNode)
589
+ )
590
+ );
591
+ return { options: undefined, errors, warnings };
592
+ }
593
+ if (optionsNode.type !== "ObjectExpression") {
594
+ errors.push(
595
+ createImportMetaGlobError(
596
+ "import.meta.glob() second argument must be an object literal",
597
+ parser.getLocation(optionsNode)
598
+ )
599
+ );
600
+ return { options: undefined, errors, warnings };
601
+ }
602
+ }
603
+
604
+ const importerContext = parser.state.module.context;
605
+ if (importerContext === null || importerContext === undefined) {
606
+ return { options: undefined, errors, warnings };
607
+ }
608
+ const compilerContext = parser.state.compilation.compiler.context;
609
+ const globOptions =
610
+ optionsNode && optionsNode.type === "ObjectExpression"
611
+ ? /** @type {ObjectExpression} */ (optionsNode)
612
+ : undefined;
613
+ const { eager, importName, query, base, exhaustive, caseSensitive } =
614
+ parseImportMetaGlobOptionsObject(globOptions, parser, warnings);
615
+ /** @type {ContextModuleOptions["mode"]} */
616
+ const mode = eager ? "sync" : "lazy";
617
+ const requestContext = resolveImportMetaGlobContext(
618
+ importerContext,
619
+ compilerContext,
620
+ base
621
+ );
622
+ const patterns = normalizeImportMetaGlobPatterns(
623
+ rawGlobPatterns,
624
+ requestContext,
625
+ compilerContext,
626
+ base !== undefined
627
+ );
628
+
629
+ const resolvedPatterns = patterns.map((pattern) =>
630
+ resolveContextModuleGlobPattern(pattern, requestContext, compilerContext)
631
+ );
632
+ const baseDir = commonGlobBaseDir(resolvedPatterns, requestContext);
633
+ const recursive = globPatternsAreRecursive(resolvedPatterns, baseDir);
634
+ /** @type {RawReferencedExports | undefined} */
635
+ let referencedExports;
636
+ if (importName && importName !== "*") {
637
+ referencedExports = [[importName]];
638
+ }
639
+ return {
640
+ options: {
641
+ patterns,
642
+ requestContext,
643
+ baseDir,
644
+ recursive,
645
+ mode,
646
+ importName,
647
+ query,
648
+ exhaustive,
649
+ caseSensitive,
650
+ referencedExports,
651
+ range: /** @type {Range} */ (expr.range)
652
+ },
653
+ errors,
654
+ warnings
655
+ };
656
+ };
657
+
658
+ module.exports = {
659
+ parseImportMetaGlobCall
660
+ };