webpack 5.59.0 → 5.76.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/README.md +22 -24
- package/bin/webpack.js +0 -0
- package/hot/dev-server.js +17 -4
- package/hot/lazy-compilation-node.js +3 -1
- package/hot/poll.js +1 -1
- package/hot/signal.js +1 -1
- package/lib/APIPlugin.js +33 -0
- package/lib/BannerPlugin.js +13 -5
- package/lib/Cache.js +1 -1
- package/lib/CacheFacade.js +4 -11
- package/lib/Chunk.js +6 -3
- package/lib/ChunkGraph.js +94 -8
- package/lib/ChunkGroup.js +1 -1
- package/lib/CleanPlugin.js +81 -20
- package/lib/Compilation.js +188 -93
- package/lib/Compiler.js +87 -18
- package/lib/ConstPlugin.js +2 -2
- package/lib/ContextModule.js +142 -51
- package/lib/ContextModuleFactory.js +65 -25
- package/lib/DelegatedModule.js +1 -1
- package/lib/DelegatedModuleFactoryPlugin.js +1 -1
- package/lib/Dependency.js +17 -0
- package/lib/DependencyTemplate.js +9 -0
- package/lib/DependencyTemplates.js +1 -1
- package/lib/DllModule.js +1 -1
- package/lib/DllReferencePlugin.js +1 -1
- package/lib/EntryOptionPlugin.js +2 -0
- package/lib/ErrorHelpers.js +2 -2
- package/lib/EvalDevToolModulePlugin.js +16 -1
- package/lib/EvalSourceMapDevToolPlugin.js +25 -4
- package/lib/ExportsInfo.js +5 -5
- package/lib/ExternalModule.js +94 -54
- package/lib/ExternalModuleFactoryPlugin.js +5 -5
- package/lib/FileSystemInfo.js +124 -58
- package/lib/Generator.js +3 -0
- package/lib/HookWebpackError.js +1 -1
- package/lib/HotModuleReplacementPlugin.js +3 -1
- package/lib/LoaderOptionsPlugin.js +1 -1
- package/lib/Module.js +28 -4
- package/lib/ModuleFilenameHelpers.js +8 -4
- package/lib/ModuleHashingError.js +29 -0
- package/lib/MultiCompiler.js +1 -1
- package/lib/MultiWatching.js +1 -1
- package/lib/NodeStuffPlugin.js +13 -3
- package/lib/NormalModule.js +51 -33
- package/lib/NormalModuleFactory.js +42 -37
- package/lib/ProgressPlugin.js +4 -5
- package/lib/RawModule.js +1 -1
- package/lib/RuntimeGlobals.js +29 -1
- package/lib/RuntimeModule.js +1 -1
- package/lib/RuntimePlugin.js +84 -1
- package/lib/RuntimeTemplate.js +114 -2
- package/lib/Template.js +3 -2
- package/lib/TemplatedPathPlugin.js +48 -23
- package/lib/WatchIgnorePlugin.js +19 -7
- package/lib/Watching.js +33 -19
- package/lib/WebpackOptionsApply.js +79 -11
- package/lib/asset/AssetGenerator.js +228 -71
- package/lib/asset/AssetModulesPlugin.js +3 -0
- package/lib/asset/AssetParser.js +1 -0
- package/lib/asset/AssetSourceGenerator.js +31 -6
- package/lib/asset/AssetSourceParser.js +1 -0
- package/lib/asset/RawDataUrlModule.js +148 -0
- package/lib/async-modules/AwaitDependenciesInitFragment.js +4 -4
- package/lib/buildChunkGraph.js +38 -7
- package/lib/cache/PackFileCacheStrategy.js +15 -8
- package/lib/cache/ResolverCachePlugin.js +90 -29
- package/lib/cli.js +44 -3
- package/lib/config/browserslistTargetHandler.js +41 -6
- package/lib/config/defaults.js +123 -19
- package/lib/config/normalization.js +10 -2
- package/lib/config/target.js +10 -0
- package/lib/container/ContainerEntryModule.js +8 -5
- package/lib/container/FallbackModule.js +4 -4
- package/lib/container/ModuleFederationPlugin.js +2 -0
- package/lib/container/RemoteModule.js +4 -2
- package/lib/container/RemoteRuntimeModule.js +8 -7
- package/lib/css/CssExportsGenerator.js +139 -0
- package/lib/css/CssGenerator.js +109 -0
- package/lib/css/CssLoadingRuntimeModule.js +442 -0
- package/lib/css/CssModulesPlugin.js +462 -0
- package/lib/css/CssParser.js +618 -0
- package/lib/css/walkCssTokens.js +659 -0
- package/lib/debug/ProfilingPlugin.js +24 -21
- package/lib/dependencies/AMDRequireDependency.js +6 -6
- package/lib/dependencies/CommonJsExportsParserPlugin.js +1 -2
- package/lib/dependencies/CommonJsFullRequireDependency.js +5 -1
- package/lib/dependencies/CommonJsImportsParserPlugin.js +344 -61
- package/lib/dependencies/CommonJsRequireContextDependency.js +6 -2
- package/lib/dependencies/CommonJsRequireDependency.js +2 -1
- package/lib/dependencies/ContextDependency.js +16 -2
- package/lib/dependencies/ContextDependencyHelpers.js +21 -8
- package/lib/dependencies/ContextDependencyTemplateAsRequireCall.js +4 -1
- package/lib/dependencies/ContextElementDependency.js +25 -3
- package/lib/dependencies/CreateScriptUrlDependency.js +12 -0
- package/lib/dependencies/CssExportDependency.js +85 -0
- package/lib/dependencies/CssImportDependency.js +75 -0
- package/lib/dependencies/CssLocalIdentifierDependency.js +119 -0
- package/lib/dependencies/CssSelfLocalIdentifierDependency.js +101 -0
- package/lib/dependencies/CssUrlDependency.js +132 -0
- package/lib/dependencies/ExportsInfoDependency.js +6 -0
- package/lib/dependencies/HarmonyAcceptImportDependency.js +5 -3
- package/lib/dependencies/HarmonyCompatibilityDependency.js +5 -5
- package/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js +127 -0
- package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +12 -3
- package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +25 -17
- package/lib/dependencies/HarmonyExportInitFragment.js +4 -1
- package/lib/dependencies/HarmonyImportDependency.js +23 -0
- package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +142 -45
- package/lib/dependencies/HarmonyImportSpecifierDependency.js +46 -22
- package/lib/dependencies/HarmonyModulesPlugin.js +10 -0
- package/lib/dependencies/ImportContextDependency.js +0 -2
- package/lib/dependencies/ImportMetaContextDependency.js +35 -0
- package/lib/dependencies/ImportMetaContextDependencyParserPlugin.js +252 -0
- package/lib/dependencies/ImportMetaContextPlugin.js +59 -0
- package/lib/dependencies/ImportMetaPlugin.js +22 -3
- package/lib/dependencies/ImportParserPlugin.js +35 -29
- package/lib/dependencies/JsonExportsDependency.js +17 -21
- package/lib/dependencies/LoaderDependency.js +13 -0
- package/lib/dependencies/LoaderImportDependency.js +13 -0
- package/lib/dependencies/LoaderPlugin.js +4 -2
- package/lib/dependencies/ModuleDependency.js +11 -1
- package/lib/dependencies/ProvidedDependency.js +31 -8
- package/lib/dependencies/RequireContextDependency.js +0 -16
- package/lib/dependencies/RequireEnsureDependency.js +2 -2
- package/lib/dependencies/RequireResolveContextDependency.js +2 -2
- package/lib/dependencies/RequireResolveDependency.js +2 -1
- package/lib/dependencies/URLDependency.js +3 -8
- package/lib/dependencies/URLPlugin.js +22 -1
- package/lib/dependencies/WorkerPlugin.js +2 -0
- package/lib/esm/ModuleChunkFormatPlugin.js +74 -49
- package/lib/esm/ModuleChunkLoadingPlugin.js +3 -1
- package/lib/esm/ModuleChunkLoadingRuntimeModule.js +25 -9
- package/lib/hmr/HotModuleReplacement.runtime.js +29 -14
- package/lib/hmr/JavascriptHotModuleReplacement.runtime.js +4 -3
- package/lib/hmr/LazyCompilationPlugin.js +54 -26
- package/lib/hmr/lazyCompilationBackend.js +51 -12
- package/lib/ids/DeterministicModuleIdsPlugin.js +55 -35
- package/lib/ids/HashedModuleIdsPlugin.js +11 -14
- package/lib/ids/IdHelpers.js +26 -12
- package/lib/ids/NamedModuleIdsPlugin.js +6 -9
- package/lib/ids/NaturalModuleIdsPlugin.js +10 -13
- package/lib/ids/OccurrenceModuleIdsPlugin.js +13 -10
- package/lib/ids/SyncModuleIdsPlugin.js +140 -0
- package/lib/index.js +20 -0
- package/lib/javascript/ArrayPushCallbackChunkFormatPlugin.js +2 -2
- package/lib/javascript/BasicEvaluatedExpression.js +5 -2
- package/lib/javascript/ChunkHelpers.js +33 -0
- package/lib/javascript/JavascriptGenerator.js +1 -0
- package/lib/javascript/JavascriptModulesPlugin.js +27 -2
- package/lib/javascript/JavascriptParser.js +143 -73
- package/lib/javascript/StartupHelpers.js +7 -30
- package/lib/json/JsonData.js +8 -0
- package/lib/json/JsonParser.js +4 -6
- package/lib/library/AssignLibraryPlugin.js +39 -15
- package/lib/library/EnableLibraryPlugin.js +11 -0
- package/lib/library/UmdLibraryPlugin.js +5 -3
- package/lib/node/NodeTargetPlugin.js +3 -0
- package/lib/node/NodeWatchFileSystem.js +85 -31
- package/lib/node/ReadFileChunkLoadingRuntimeModule.js +23 -8
- package/lib/node/RequireChunkLoadingRuntimeModule.js +24 -9
- package/lib/optimize/ConcatenatedModule.js +62 -26
- package/lib/optimize/ModuleConcatenationPlugin.js +26 -4
- package/lib/optimize/RealContentHashPlugin.js +45 -15
- package/lib/optimize/SplitChunksPlugin.js +8 -1
- package/lib/runtime/AsyncModuleRuntimeModule.js +50 -66
- package/lib/runtime/BaseUriRuntimeModule.js +31 -0
- package/lib/runtime/CreateScriptRuntimeModule.js +36 -0
- package/lib/runtime/CreateScriptUrlRuntimeModule.js +9 -34
- package/lib/runtime/GetTrustedTypesPolicyRuntimeModule.js +76 -0
- package/lib/runtime/LoadScriptRuntimeModule.js +11 -9
- package/lib/runtime/NonceRuntimeModule.js +24 -0
- package/lib/schemes/HttpUriPlugin.js +77 -14
- package/lib/serialization/FileMiddleware.js +44 -9
- package/lib/sharing/ConsumeSharedModule.js +8 -2
- package/lib/sharing/ConsumeSharedRuntimeModule.js +26 -5
- package/lib/sharing/ProvideSharedModule.js +4 -2
- package/lib/sharing/ProvideSharedPlugin.js +1 -2
- package/lib/sharing/ShareRuntimeModule.js +1 -1
- package/lib/sharing/utils.js +1 -1
- package/lib/stats/DefaultStatsFactoryPlugin.js +113 -68
- package/lib/stats/DefaultStatsPrinterPlugin.js +90 -25
- package/lib/util/ArrayHelpers.js +30 -0
- package/lib/util/AsyncQueue.js +1 -1
- package/lib/util/compileBooleanMatcher.js +1 -1
- package/lib/util/create-schema-validation.js +9 -2
- package/lib/util/createHash.js +12 -0
- package/lib/util/deprecation.js +10 -2
- package/lib/util/deterministicGrouping.js +1 -1
- package/lib/util/extractUrlAndGlobal.js +3 -0
- package/lib/util/fs.js +11 -0
- package/lib/util/hash/BatchedHash.js +7 -4
- package/lib/util/hash/md4.js +20 -0
- package/lib/util/hash/wasm-hash.js +163 -0
- package/lib/util/hash/xxhash64.js +5 -139
- package/lib/util/identifier.js +65 -44
- package/lib/util/internalSerializables.js +15 -0
- package/lib/util/nonNumericOnlyHash.js +22 -0
- package/lib/util/semver.js +17 -10
- package/lib/wasm-async/AsyncWebAssemblyJavascriptGenerator.js +9 -3
- package/lib/wasm-sync/WebAssemblyParser.js +1 -1
- package/lib/web/JsonpChunkLoadingRuntimeModule.js +31 -18
- package/lib/webpack.js +10 -3
- package/lib/webworker/ImportScriptsChunkLoadingPlugin.js +3 -11
- package/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js +33 -22
- package/module.d.ts +215 -0
- package/package.json +28 -32
- package/schemas/WebpackOptions.check.js +1 -1
- package/schemas/WebpackOptions.json +321 -30
- package/schemas/plugins/BannerPlugin.check.js +1 -1
- package/schemas/plugins/BannerPlugin.json +4 -0
- package/schemas/plugins/DllReferencePlugin.check.js +1 -1
- package/schemas/plugins/HashedModuleIdsPlugin.check.js +1 -1
- package/schemas/plugins/ProgressPlugin.check.js +1 -1
- package/schemas/plugins/asset/AssetGeneratorOptions.check.js +1 -1
- package/schemas/plugins/asset/AssetParserOptions.check.js +1 -1
- package/schemas/plugins/asset/AssetResourceGeneratorOptions.check.js +1 -1
- package/schemas/plugins/container/ContainerPlugin.check.js +1 -1
- package/schemas/plugins/container/ContainerPlugin.json +2 -1
- package/schemas/plugins/container/ContainerReferencePlugin.check.js +1 -1
- package/schemas/plugins/container/ContainerReferencePlugin.json +1 -0
- package/schemas/plugins/container/ExternalsType.check.js +1 -1
- package/schemas/plugins/container/ModuleFederationPlugin.check.js +1 -1
- package/schemas/plugins/container/ModuleFederationPlugin.json +3 -1
- package/schemas/plugins/css/CssGeneratorOptions.check.d.ts +7 -0
- package/schemas/plugins/css/CssGeneratorOptions.check.js +6 -0
- package/schemas/plugins/css/CssGeneratorOptions.json +3 -0
- package/schemas/plugins/css/CssParserOptions.check.d.ts +7 -0
- package/schemas/plugins/css/CssParserOptions.check.js +6 -0
- package/schemas/plugins/css/CssParserOptions.json +3 -0
- package/schemas/plugins/optimize/AggressiveSplittingPlugin.check.js +1 -1
- package/schemas/plugins/optimize/LimitChunkCountPlugin.check.js +1 -1
- package/schemas/plugins/optimize/MinChunkSizePlugin.check.js +1 -1
- package/schemas/plugins/schemes/HttpUriPlugin.check.js +1 -1
- package/schemas/plugins/schemes/HttpUriPlugin.json +4 -0
- package/types.d.ts +869 -296
@@ -28,6 +28,8 @@ const HarmonyImportDependency = require("./HarmonyImportDependency");
|
|
28
28
|
|
29
29
|
const idsSymbol = Symbol("HarmonyImportSpecifierDependency.ids");
|
30
30
|
|
31
|
+
const { ExportPresenceModes } = HarmonyImportDependency;
|
32
|
+
|
31
33
|
class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
|
32
34
|
constructor(
|
33
35
|
request,
|
@@ -35,14 +37,14 @@ class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
|
|
35
37
|
ids,
|
36
38
|
name,
|
37
39
|
range,
|
38
|
-
|
40
|
+
exportPresenceMode,
|
39
41
|
assertions
|
40
42
|
) {
|
41
43
|
super(request, sourceOrder, assertions);
|
42
44
|
this.ids = ids;
|
43
45
|
this.name = name;
|
44
46
|
this.range = range;
|
45
|
-
this.
|
47
|
+
this.exportPresenceMode = exportPresenceMode;
|
46
48
|
this.namespaceObjectAsContext = false;
|
47
49
|
this.call = undefined;
|
48
50
|
this.directImport = undefined;
|
@@ -153,19 +155,29 @@ class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
|
|
153
155
|
return [ids];
|
154
156
|
}
|
155
157
|
|
158
|
+
/**
|
159
|
+
* @param {ModuleGraph} moduleGraph module graph
|
160
|
+
* @returns {number} effective mode
|
161
|
+
*/
|
162
|
+
_getEffectiveExportPresenceLevel(moduleGraph) {
|
163
|
+
if (this.exportPresenceMode !== ExportPresenceModes.AUTO)
|
164
|
+
return this.exportPresenceMode;
|
165
|
+
return moduleGraph.getParentModule(this).buildMeta.strictHarmonyModule
|
166
|
+
? ExportPresenceModes.ERROR
|
167
|
+
: ExportPresenceModes.WARN;
|
168
|
+
}
|
169
|
+
|
156
170
|
/**
|
157
171
|
* Returns warnings
|
158
172
|
* @param {ModuleGraph} moduleGraph module graph
|
159
173
|
* @returns {WebpackError[]} warnings
|
160
174
|
*/
|
161
175
|
getWarnings(moduleGraph) {
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
) {
|
166
|
-
return null;
|
176
|
+
const exportsPresence = this._getEffectiveExportPresenceLevel(moduleGraph);
|
177
|
+
if (exportsPresence === ExportPresenceModes.WARN) {
|
178
|
+
return this._getErrors(moduleGraph);
|
167
179
|
}
|
168
|
-
return
|
180
|
+
return null;
|
169
181
|
}
|
170
182
|
|
171
183
|
/**
|
@@ -174,10 +186,8 @@ class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
|
|
174
186
|
* @returns {WebpackError[]} errors
|
175
187
|
*/
|
176
188
|
getErrors(moduleGraph) {
|
177
|
-
|
178
|
-
|
179
|
-
moduleGraph.getParentModule(this).buildMeta.strictHarmonyModule
|
180
|
-
) {
|
189
|
+
const exportsPresence = this._getEffectiveExportPresenceLevel(moduleGraph);
|
190
|
+
if (exportsPresence === ExportPresenceModes.ERROR) {
|
181
191
|
return this._getErrors(moduleGraph);
|
182
192
|
}
|
183
193
|
return null;
|
@@ -209,7 +219,7 @@ class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
|
|
209
219
|
write(this.ids);
|
210
220
|
write(this.name);
|
211
221
|
write(this.range);
|
212
|
-
write(this.
|
222
|
+
write(this.exportPresenceMode);
|
213
223
|
write(this.namespaceObjectAsContext);
|
214
224
|
write(this.call);
|
215
225
|
write(this.directImport);
|
@@ -224,7 +234,7 @@ class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
|
|
224
234
|
this.ids = read();
|
225
235
|
this.name = read();
|
226
236
|
this.range = read();
|
227
|
-
this.
|
237
|
+
this.exportPresenceMode = read();
|
228
238
|
this.namespaceObjectAsContext = read();
|
229
239
|
this.call = read();
|
230
240
|
this.directImport = read();
|
@@ -251,14 +261,32 @@ HarmonyImportSpecifierDependency.Template = class HarmonyImportSpecifierDependen
|
|
251
261
|
*/
|
252
262
|
apply(dependency, source, templateContext) {
|
253
263
|
const dep = /** @type {HarmonyImportSpecifierDependency} */ (dependency);
|
254
|
-
const { moduleGraph,
|
255
|
-
templateContext;
|
264
|
+
const { moduleGraph, runtime } = templateContext;
|
256
265
|
const connection = moduleGraph.getConnection(dep);
|
257
266
|
// Skip rendering depending when dependency is conditional
|
258
267
|
if (connection && !connection.isTargetActive(runtime)) return;
|
259
268
|
|
260
269
|
const ids = dep.getIds(moduleGraph);
|
270
|
+
const exportExpr = this._getCodeForIds(dep, source, templateContext, ids);
|
271
|
+
const range = dep.range;
|
272
|
+
if (dep.shorthand) {
|
273
|
+
source.insert(range[1], `: ${exportExpr}`);
|
274
|
+
} else {
|
275
|
+
source.replace(range[0], range[1] - 1, exportExpr);
|
276
|
+
}
|
277
|
+
}
|
261
278
|
|
279
|
+
/**
|
280
|
+
* @param {HarmonyImportSpecifierDependency} dep dependency
|
281
|
+
* @param {ReplaceSource} source source
|
282
|
+
* @param {DependencyTemplateContext} templateContext context
|
283
|
+
* @param {string[]} ids ids
|
284
|
+
* @returns {string} generated code
|
285
|
+
*/
|
286
|
+
_getCodeForIds(dep, source, templateContext, ids) {
|
287
|
+
const { moduleGraph, module, runtime, concatenationScope } =
|
288
|
+
templateContext;
|
289
|
+
const connection = moduleGraph.getConnection(dep);
|
262
290
|
let exportExpr;
|
263
291
|
if (
|
264
292
|
connection &&
|
@@ -289,7 +317,7 @@ HarmonyImportSpecifierDependency.Template = class HarmonyImportSpecifierDependen
|
|
289
317
|
);
|
290
318
|
}
|
291
319
|
} else {
|
292
|
-
super.apply(
|
320
|
+
super.apply(dep, source, templateContext);
|
293
321
|
|
294
322
|
const { runtimeTemplate, initFragments, runtimeRequirements } =
|
295
323
|
templateContext;
|
@@ -310,11 +338,7 @@ HarmonyImportSpecifierDependency.Template = class HarmonyImportSpecifierDependen
|
|
310
338
|
runtimeRequirements
|
311
339
|
});
|
312
340
|
}
|
313
|
-
|
314
|
-
source.insert(dep.range[1], `: ${exportExpr}`);
|
315
|
-
} else {
|
316
|
-
source.replace(dep.range[0], dep.range[1] - 1, exportExpr);
|
317
|
-
}
|
341
|
+
return exportExpr;
|
318
342
|
}
|
319
343
|
};
|
320
344
|
|
@@ -8,6 +8,7 @@
|
|
8
8
|
const HarmonyAcceptDependency = require("./HarmonyAcceptDependency");
|
9
9
|
const HarmonyAcceptImportDependency = require("./HarmonyAcceptImportDependency");
|
10
10
|
const HarmonyCompatibilityDependency = require("./HarmonyCompatibilityDependency");
|
11
|
+
const HarmonyEvaluatedImportSpecifierDependency = require("./HarmonyEvaluatedImportSpecifierDependency");
|
11
12
|
const HarmonyExportExpressionDependency = require("./HarmonyExportExpressionDependency");
|
12
13
|
const HarmonyExportHeaderDependency = require("./HarmonyExportHeaderDependency");
|
13
14
|
const HarmonyExportImportedSpecifierDependency = require("./HarmonyExportImportedSpecifierDependency");
|
@@ -59,6 +60,15 @@ class HarmonyModulesPlugin {
|
|
59
60
|
new HarmonyImportSpecifierDependency.Template()
|
60
61
|
);
|
61
62
|
|
63
|
+
compilation.dependencyFactories.set(
|
64
|
+
HarmonyEvaluatedImportSpecifierDependency,
|
65
|
+
normalModuleFactory
|
66
|
+
);
|
67
|
+
compilation.dependencyTemplates.set(
|
68
|
+
HarmonyEvaluatedImportSpecifierDependency,
|
69
|
+
new HarmonyEvaluatedImportSpecifierDependency.Template()
|
70
|
+
);
|
71
|
+
|
62
72
|
compilation.dependencyTemplates.set(
|
63
73
|
HarmonyExportHeaderDependency,
|
64
74
|
new HarmonyExportHeaderDependency.Template()
|
@@ -28,7 +28,6 @@ class ImportContextDependency extends ContextDependency {
|
|
28
28
|
serialize(context) {
|
29
29
|
const { write } = context;
|
30
30
|
|
31
|
-
write(this.range);
|
32
31
|
write(this.valueRange);
|
33
32
|
|
34
33
|
super.serialize(context);
|
@@ -37,7 +36,6 @@ class ImportContextDependency extends ContextDependency {
|
|
37
36
|
deserialize(context) {
|
38
37
|
const { read } = context;
|
39
38
|
|
40
|
-
this.range = read();
|
41
39
|
this.valueRange = read();
|
42
40
|
|
43
41
|
super.deserialize(context);
|
@@ -0,0 +1,35 @@
|
|
1
|
+
/*
|
2
|
+
MIT License http://www.opensource.org/licenses/mit-license.php
|
3
|
+
Author Ivan Kopeykin @vankop
|
4
|
+
*/
|
5
|
+
|
6
|
+
"use strict";
|
7
|
+
|
8
|
+
const makeSerializable = require("../util/makeSerializable");
|
9
|
+
const ContextDependency = require("./ContextDependency");
|
10
|
+
const ModuleDependencyTemplateAsRequireId = require("./ModuleDependencyTemplateAsRequireId");
|
11
|
+
|
12
|
+
class ImportMetaContextDependency extends ContextDependency {
|
13
|
+
constructor(options, range) {
|
14
|
+
super(options);
|
15
|
+
|
16
|
+
this.range = range;
|
17
|
+
}
|
18
|
+
|
19
|
+
get category() {
|
20
|
+
return "esm";
|
21
|
+
}
|
22
|
+
|
23
|
+
get type() {
|
24
|
+
return `import.meta.webpackContext ${this.options.mode}`;
|
25
|
+
}
|
26
|
+
}
|
27
|
+
|
28
|
+
makeSerializable(
|
29
|
+
ImportMetaContextDependency,
|
30
|
+
"webpack/lib/dependencies/ImportMetaContextDependency"
|
31
|
+
);
|
32
|
+
|
33
|
+
ImportMetaContextDependency.Template = ModuleDependencyTemplateAsRequireId;
|
34
|
+
|
35
|
+
module.exports = ImportMetaContextDependency;
|
@@ -0,0 +1,252 @@
|
|
1
|
+
/*
|
2
|
+
MIT License http://www.opensource.org/licenses/mit-license.php
|
3
|
+
Author Ivan Kopeykin @vankop
|
4
|
+
*/
|
5
|
+
|
6
|
+
"use strict";
|
7
|
+
|
8
|
+
const WebpackError = require("../WebpackError");
|
9
|
+
const {
|
10
|
+
evaluateToIdentifier
|
11
|
+
} = require("../javascript/JavascriptParserHelpers");
|
12
|
+
const ImportMetaContextDependency = require("./ImportMetaContextDependency");
|
13
|
+
|
14
|
+
/** @typedef {import("estree").Expression} ExpressionNode */
|
15
|
+
/** @typedef {import("estree").ObjectExpression} ObjectExpressionNode */
|
16
|
+
/** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
|
17
|
+
/** @typedef {import("../ContextModule").ContextModuleOptions} ContextModuleOptions */
|
18
|
+
/** @typedef {import("../ChunkGroup").RawChunkGroupOptions} RawChunkGroupOptions */
|
19
|
+
/** @typedef {Pick<ContextModuleOptions, 'mode'|'recursive'|'regExp'|'include'|'exclude'|'chunkName'>&{groupOptions: RawChunkGroupOptions, exports?: ContextModuleOptions["referencedExports"]}} ImportMetaContextOptions */
|
20
|
+
|
21
|
+
function createPropertyParseError(prop, expect) {
|
22
|
+
return createError(
|
23
|
+
`Parsing import.meta.webpackContext options failed. Unknown value for property ${JSON.stringify(
|
24
|
+
prop.key.name
|
25
|
+
)}, expected type ${expect}.`,
|
26
|
+
prop.value.loc
|
27
|
+
);
|
28
|
+
}
|
29
|
+
|
30
|
+
function createError(msg, loc) {
|
31
|
+
const error = new WebpackError(msg);
|
32
|
+
error.name = "ImportMetaContextError";
|
33
|
+
error.loc = loc;
|
34
|
+
return error;
|
35
|
+
}
|
36
|
+
|
37
|
+
module.exports = class ImportMetaContextDependencyParserPlugin {
|
38
|
+
apply(parser) {
|
39
|
+
parser.hooks.evaluateIdentifier
|
40
|
+
.for("import.meta.webpackContext")
|
41
|
+
.tap("ImportMetaContextDependencyParserPlugin", expr => {
|
42
|
+
return evaluateToIdentifier(
|
43
|
+
"import.meta.webpackContext",
|
44
|
+
"import.meta",
|
45
|
+
() => ["webpackContext"],
|
46
|
+
true
|
47
|
+
)(expr);
|
48
|
+
});
|
49
|
+
parser.hooks.call
|
50
|
+
.for("import.meta.webpackContext")
|
51
|
+
.tap("ImportMetaContextDependencyParserPlugin", expr => {
|
52
|
+
if (expr.arguments.length < 1 || expr.arguments.length > 2) return;
|
53
|
+
const [directoryNode, optionsNode] = expr.arguments;
|
54
|
+
if (optionsNode && optionsNode.type !== "ObjectExpression") return;
|
55
|
+
const requestExpr = parser.evaluateExpression(directoryNode);
|
56
|
+
if (!requestExpr.isString()) return;
|
57
|
+
const request = requestExpr.string;
|
58
|
+
const errors = [];
|
59
|
+
let regExp = /^\.\/.*$/;
|
60
|
+
let recursive = true;
|
61
|
+
/** @type {ContextModuleOptions["mode"]} */
|
62
|
+
let mode = "sync";
|
63
|
+
/** @type {ContextModuleOptions["include"]} */
|
64
|
+
let include;
|
65
|
+
/** @type {ContextModuleOptions["exclude"]} */
|
66
|
+
let exclude;
|
67
|
+
/** @type {RawChunkGroupOptions} */
|
68
|
+
const groupOptions = {};
|
69
|
+
/** @type {ContextModuleOptions["chunkName"]} */
|
70
|
+
let chunkName;
|
71
|
+
/** @type {ContextModuleOptions["referencedExports"]} */
|
72
|
+
let exports;
|
73
|
+
if (optionsNode) {
|
74
|
+
for (const prop of optionsNode.properties) {
|
75
|
+
if (prop.type !== "Property" || prop.key.type !== "Identifier") {
|
76
|
+
errors.push(
|
77
|
+
createError(
|
78
|
+
"Parsing import.meta.webpackContext options failed.",
|
79
|
+
optionsNode.loc
|
80
|
+
)
|
81
|
+
);
|
82
|
+
break;
|
83
|
+
}
|
84
|
+
switch (prop.key.name) {
|
85
|
+
case "regExp": {
|
86
|
+
const regExpExpr = parser.evaluateExpression(
|
87
|
+
/** @type {ExpressionNode} */ (prop.value)
|
88
|
+
);
|
89
|
+
if (!regExpExpr.isRegExp()) {
|
90
|
+
errors.push(createPropertyParseError(prop, "RegExp"));
|
91
|
+
} else {
|
92
|
+
regExp = regExpExpr.regExp;
|
93
|
+
}
|
94
|
+
break;
|
95
|
+
}
|
96
|
+
case "include": {
|
97
|
+
const regExpExpr = parser.evaluateExpression(
|
98
|
+
/** @type {ExpressionNode} */ (prop.value)
|
99
|
+
);
|
100
|
+
if (!regExpExpr.isRegExp()) {
|
101
|
+
errors.push(createPropertyParseError(prop, "RegExp"));
|
102
|
+
} else {
|
103
|
+
include = regExpExpr.regExp;
|
104
|
+
}
|
105
|
+
break;
|
106
|
+
}
|
107
|
+
case "exclude": {
|
108
|
+
const regExpExpr = parser.evaluateExpression(
|
109
|
+
/** @type {ExpressionNode} */ (prop.value)
|
110
|
+
);
|
111
|
+
if (!regExpExpr.isRegExp()) {
|
112
|
+
errors.push(createPropertyParseError(prop, "RegExp"));
|
113
|
+
} else {
|
114
|
+
exclude = regExpExpr.regExp;
|
115
|
+
}
|
116
|
+
break;
|
117
|
+
}
|
118
|
+
case "mode": {
|
119
|
+
const modeExpr = parser.evaluateExpression(
|
120
|
+
/** @type {ExpressionNode} */ (prop.value)
|
121
|
+
);
|
122
|
+
if (!modeExpr.isString()) {
|
123
|
+
errors.push(createPropertyParseError(prop, "string"));
|
124
|
+
} else {
|
125
|
+
mode = /** @type {ContextModuleOptions["mode"]} */ (
|
126
|
+
modeExpr.string
|
127
|
+
);
|
128
|
+
}
|
129
|
+
break;
|
130
|
+
}
|
131
|
+
case "chunkName": {
|
132
|
+
const expr = parser.evaluateExpression(
|
133
|
+
/** @type {ExpressionNode} */ (prop.value)
|
134
|
+
);
|
135
|
+
if (!expr.isString()) {
|
136
|
+
errors.push(createPropertyParseError(prop, "string"));
|
137
|
+
} else {
|
138
|
+
chunkName = expr.string;
|
139
|
+
}
|
140
|
+
break;
|
141
|
+
}
|
142
|
+
case "exports": {
|
143
|
+
const expr = parser.evaluateExpression(
|
144
|
+
/** @type {ExpressionNode} */ (prop.value)
|
145
|
+
);
|
146
|
+
if (expr.isString()) {
|
147
|
+
exports = [[expr.string]];
|
148
|
+
} else if (expr.isArray()) {
|
149
|
+
const items = expr.items;
|
150
|
+
if (
|
151
|
+
items.every(i => {
|
152
|
+
if (!i.isArray()) return false;
|
153
|
+
const innerItems = i.items;
|
154
|
+
return innerItems.every(i => i.isString());
|
155
|
+
})
|
156
|
+
) {
|
157
|
+
exports = [];
|
158
|
+
for (const i1 of items) {
|
159
|
+
const export_ = [];
|
160
|
+
for (const i2 of i1.items) {
|
161
|
+
export_.push(i2.string);
|
162
|
+
}
|
163
|
+
exports.push(export_);
|
164
|
+
}
|
165
|
+
} else {
|
166
|
+
errors.push(
|
167
|
+
createPropertyParseError(prop, "string|string[][]")
|
168
|
+
);
|
169
|
+
}
|
170
|
+
} else {
|
171
|
+
errors.push(
|
172
|
+
createPropertyParseError(prop, "string|string[][]")
|
173
|
+
);
|
174
|
+
}
|
175
|
+
break;
|
176
|
+
}
|
177
|
+
case "prefetch": {
|
178
|
+
const expr = parser.evaluateExpression(
|
179
|
+
/** @type {ExpressionNode} */ (prop.value)
|
180
|
+
);
|
181
|
+
if (expr.isBoolean()) {
|
182
|
+
groupOptions.prefetchOrder = 0;
|
183
|
+
} else if (expr.isNumber()) {
|
184
|
+
groupOptions.prefetchOrder = expr.number;
|
185
|
+
} else {
|
186
|
+
errors.push(createPropertyParseError(prop, "boolean|number"));
|
187
|
+
}
|
188
|
+
break;
|
189
|
+
}
|
190
|
+
case "preload": {
|
191
|
+
const expr = parser.evaluateExpression(
|
192
|
+
/** @type {ExpressionNode} */ (prop.value)
|
193
|
+
);
|
194
|
+
if (expr.isBoolean()) {
|
195
|
+
groupOptions.preloadOrder = 0;
|
196
|
+
} else if (expr.isNumber()) {
|
197
|
+
groupOptions.preloadOrder = expr.number;
|
198
|
+
} else {
|
199
|
+
errors.push(createPropertyParseError(prop, "boolean|number"));
|
200
|
+
}
|
201
|
+
break;
|
202
|
+
}
|
203
|
+
case "recursive": {
|
204
|
+
const recursiveExpr = parser.evaluateExpression(
|
205
|
+
/** @type {ExpressionNode} */ (prop.value)
|
206
|
+
);
|
207
|
+
if (!recursiveExpr.isBoolean()) {
|
208
|
+
errors.push(createPropertyParseError(prop, "boolean"));
|
209
|
+
} else {
|
210
|
+
recursive = recursiveExpr.bool;
|
211
|
+
}
|
212
|
+
break;
|
213
|
+
}
|
214
|
+
default:
|
215
|
+
errors.push(
|
216
|
+
createError(
|
217
|
+
`Parsing import.meta.webpackContext options failed. Unknown property ${JSON.stringify(
|
218
|
+
prop.key.name
|
219
|
+
)}.`,
|
220
|
+
optionsNode.loc
|
221
|
+
)
|
222
|
+
);
|
223
|
+
}
|
224
|
+
}
|
225
|
+
}
|
226
|
+
if (errors.length) {
|
227
|
+
for (const error of errors) parser.state.current.addError(error);
|
228
|
+
return;
|
229
|
+
}
|
230
|
+
|
231
|
+
const dep = new ImportMetaContextDependency(
|
232
|
+
{
|
233
|
+
request,
|
234
|
+
include,
|
235
|
+
exclude,
|
236
|
+
recursive,
|
237
|
+
regExp,
|
238
|
+
groupOptions,
|
239
|
+
chunkName,
|
240
|
+
referencedExports: exports,
|
241
|
+
mode,
|
242
|
+
category: "esm"
|
243
|
+
},
|
244
|
+
expr.range
|
245
|
+
);
|
246
|
+
dep.loc = expr.loc;
|
247
|
+
dep.optional = !!parser.scope.inTry;
|
248
|
+
parser.state.current.addDependency(dep);
|
249
|
+
return true;
|
250
|
+
});
|
251
|
+
}
|
252
|
+
};
|
@@ -0,0 +1,59 @@
|
|
1
|
+
/*
|
2
|
+
MIT License http://www.opensource.org/licenses/mit-license.php
|
3
|
+
Author Ivan Kopeykin @vankop
|
4
|
+
*/
|
5
|
+
|
6
|
+
"use strict";
|
7
|
+
|
8
|
+
const ContextElementDependency = require("./ContextElementDependency");
|
9
|
+
const ImportMetaContextDependency = require("./ImportMetaContextDependency");
|
10
|
+
const ImportMetaContextDependencyParserPlugin = require("./ImportMetaContextDependencyParserPlugin");
|
11
|
+
|
12
|
+
/** @typedef {import("../../declarations/WebpackOptions").ResolveOptions} ResolveOptions */
|
13
|
+
/** @typedef {import("../Compiler")} Compiler */
|
14
|
+
|
15
|
+
class ImportMetaContextPlugin {
|
16
|
+
/**
|
17
|
+
* Apply the plugin
|
18
|
+
* @param {Compiler} compiler the compiler instance
|
19
|
+
* @returns {void}
|
20
|
+
*/
|
21
|
+
apply(compiler) {
|
22
|
+
compiler.hooks.compilation.tap(
|
23
|
+
"RequireContextPlugin",
|
24
|
+
(compilation, { contextModuleFactory, normalModuleFactory }) => {
|
25
|
+
compilation.dependencyFactories.set(
|
26
|
+
ImportMetaContextDependency,
|
27
|
+
contextModuleFactory
|
28
|
+
);
|
29
|
+
compilation.dependencyTemplates.set(
|
30
|
+
ImportMetaContextDependency,
|
31
|
+
new ImportMetaContextDependency.Template()
|
32
|
+
);
|
33
|
+
compilation.dependencyFactories.set(
|
34
|
+
ContextElementDependency,
|
35
|
+
normalModuleFactory
|
36
|
+
);
|
37
|
+
|
38
|
+
const handler = (parser, parserOptions) => {
|
39
|
+
if (
|
40
|
+
parserOptions.importMetaContext !== undefined &&
|
41
|
+
!parserOptions.importMetaContext
|
42
|
+
)
|
43
|
+
return;
|
44
|
+
|
45
|
+
new ImportMetaContextDependencyParserPlugin().apply(parser);
|
46
|
+
};
|
47
|
+
|
48
|
+
normalModuleFactory.hooks.parser
|
49
|
+
.for("javascript/auto")
|
50
|
+
.tap("ImportMetaContextPlugin", handler);
|
51
|
+
normalModuleFactory.hooks.parser
|
52
|
+
.for("javascript/esm")
|
53
|
+
.tap("ImportMetaContextPlugin", handler);
|
54
|
+
}
|
55
|
+
);
|
56
|
+
}
|
57
|
+
}
|
58
|
+
|
59
|
+
module.exports = ImportMetaContextPlugin;
|
@@ -20,6 +20,7 @@ const propertyAccess = require("../util/propertyAccess");
|
|
20
20
|
const ConstDependency = require("./ConstDependency");
|
21
21
|
|
22
22
|
/** @typedef {import("estree").MemberExpression} MemberExpression */
|
23
|
+
/** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
|
23
24
|
/** @typedef {import("../Compiler")} Compiler */
|
24
25
|
/** @typedef {import("../NormalModule")} NormalModule */
|
25
26
|
/** @typedef {import("../javascript/JavascriptParser")} Parser */
|
@@ -44,11 +45,29 @@ class ImportMetaPlugin {
|
|
44
45
|
return pathToFileURL(module.resource).toString();
|
45
46
|
};
|
46
47
|
/**
|
47
|
-
* @param {Parser} parser parser
|
48
|
-
* @param {
|
48
|
+
* @param {Parser} parser parser parser
|
49
|
+
* @param {JavascriptParserOptions} parserOptions parserOptions
|
49
50
|
* @returns {void}
|
50
51
|
*/
|
51
|
-
const parserHandler = (parser,
|
52
|
+
const parserHandler = (parser, { importMeta }) => {
|
53
|
+
if (importMeta === false) {
|
54
|
+
const { importMetaName } = compilation.outputOptions;
|
55
|
+
if (importMetaName === "import.meta") return;
|
56
|
+
|
57
|
+
parser.hooks.expression
|
58
|
+
.for("import.meta")
|
59
|
+
.tap("ImportMetaPlugin", metaProperty => {
|
60
|
+
const dep = new ConstDependency(
|
61
|
+
importMetaName,
|
62
|
+
metaProperty.range
|
63
|
+
);
|
64
|
+
dep.loc = metaProperty.loc;
|
65
|
+
parser.state.module.addPresentationalDependency(dep);
|
66
|
+
return true;
|
67
|
+
});
|
68
|
+
return;
|
69
|
+
}
|
70
|
+
|
52
71
|
/// import.meta direct ///
|
53
72
|
parser.hooks.typeof
|
54
73
|
.for("import.meta")
|