webpack 5.70.0 → 5.72.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/hot/poll.js +1 -1
- package/hot/signal.js +1 -1
- package/lib/BannerPlugin.js +3 -1
- package/lib/Chunk.js +1 -1
- package/lib/ChunkGraph.js +93 -6
- package/lib/ChunkGroup.js +1 -1
- package/lib/Compilation.js +10 -8
- package/lib/Compiler.js +16 -3
- package/lib/ConstPlugin.js +2 -2
- package/lib/ContextModule.js +31 -8
- package/lib/ContextModuleFactory.js +7 -11
- package/lib/DelegatedModuleFactoryPlugin.js +1 -1
- package/lib/Dependency.js +7 -0
- package/lib/ErrorHelpers.js +2 -2
- package/lib/ExportsInfo.js +1 -1
- package/lib/ExternalModuleFactoryPlugin.js +4 -4
- package/lib/FileSystemInfo.js +8 -0
- package/lib/LoaderOptionsPlugin.js +1 -1
- package/lib/Module.js +3 -0
- package/lib/ModuleFilenameHelpers.js +3 -3
- package/lib/NormalModule.js +5 -4
- package/lib/NormalModuleFactory.js +2 -2
- package/lib/RuntimePlugin.js +18 -0
- package/lib/asset/AssetGenerator.js +36 -9
- package/lib/asset/AssetParser.js +1 -0
- package/lib/asset/AssetSourceGenerator.js +31 -6
- package/lib/asset/AssetSourceParser.js +1 -0
- package/lib/cache/PackFileCacheStrategy.js +8 -4
- package/lib/config/defaults.js +9 -1
- package/lib/container/RemoteRuntimeModule.js +8 -7
- package/lib/dependencies/CommonJsExportsParserPlugin.js +1 -2
- package/lib/dependencies/ContextDependencyHelpers.js +2 -2
- package/lib/dependencies/ContextElementDependency.js +33 -1
- package/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js +95 -0
- package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +127 -43
- package/lib/dependencies/HarmonyImportSpecifierDependency.js +22 -8
- package/lib/dependencies/HarmonyModulesPlugin.js +10 -0
- package/lib/esm/ModuleChunkLoadingPlugin.js +3 -1
- package/lib/hmr/HotModuleReplacement.runtime.js +29 -14
- package/lib/hmr/JavascriptHotModuleReplacement.runtime.js +4 -3
- package/lib/ids/HashedModuleIdsPlugin.js +2 -2
- package/lib/ids/IdHelpers.js +1 -1
- package/lib/javascript/BasicEvaluatedExpression.js +5 -2
- package/lib/javascript/JavascriptParser.js +66 -40
- package/lib/library/UmdLibraryPlugin.js +5 -3
- package/lib/optimize/ConcatenatedModule.js +2 -1
- package/lib/optimize/ModuleConcatenationPlugin.js +20 -1
- package/lib/runtime/BaseUriRuntimeModule.js +31 -0
- package/lib/stats/DefaultStatsFactoryPlugin.js +1 -1
- package/lib/util/internalSerializables.js +2 -0
- package/lib/web/JsonpChunkLoadingRuntimeModule.js +2 -1
- package/package.json +1 -1
- package/schemas/plugins/BannerPlugin.check.js +1 -1
- package/schemas/plugins/BannerPlugin.json +4 -0
- package/types.d.ts +38 -11
package/lib/NormalModule.js
CHANGED
@@ -375,7 +375,7 @@ class NormalModule extends Module {
|
|
375
375
|
nameForCondition() {
|
376
376
|
const resource = this.matchResource || this.resource;
|
377
377
|
const idx = resource.indexOf("?");
|
378
|
-
if (idx >= 0) return resource.
|
378
|
+
if (idx >= 0) return resource.slice(0, idx);
|
379
379
|
return resource;
|
380
380
|
}
|
381
381
|
|
@@ -558,7 +558,7 @@ class NormalModule extends Module {
|
|
558
558
|
let { options } = loader;
|
559
559
|
|
560
560
|
if (typeof options === "string") {
|
561
|
-
if (options.
|
561
|
+
if (options.startsWith("{") && options.endsWith("}")) {
|
562
562
|
try {
|
563
563
|
options = parseJson(options);
|
564
564
|
} catch (e) {
|
@@ -1176,7 +1176,8 @@ class NormalModule extends Module {
|
|
1176
1176
|
chunkGraph,
|
1177
1177
|
runtime,
|
1178
1178
|
concatenationScope,
|
1179
|
-
codeGenerationResults
|
1179
|
+
codeGenerationResults,
|
1180
|
+
sourceTypes
|
1180
1181
|
}) {
|
1181
1182
|
/** @type {Set<string>} */
|
1182
1183
|
const runtimeRequirements = new Set();
|
@@ -1195,7 +1196,7 @@ class NormalModule extends Module {
|
|
1195
1196
|
};
|
1196
1197
|
|
1197
1198
|
const sources = new Map();
|
1198
|
-
for (const type of
|
1199
|
+
for (const type of sourceTypes || chunkGraph.getModuleSourceTypes(this)) {
|
1199
1200
|
const source = this.error
|
1200
1201
|
? new RawSource(
|
1201
1202
|
"throw new Error(" + JSON.stringify(this.error.message) + ");"
|
@@ -379,7 +379,7 @@ class NormalModuleFactory extends ModuleFactory {
|
|
379
379
|
resource: matchResource,
|
380
380
|
...cacheParseResource(matchResource)
|
381
381
|
};
|
382
|
-
requestWithoutMatchResource = request.
|
382
|
+
requestWithoutMatchResource = request.slice(
|
383
383
|
matchResourceMatch[0].length
|
384
384
|
);
|
385
385
|
}
|
@@ -437,7 +437,7 @@ class NormalModuleFactory extends ModuleFactory {
|
|
437
437
|
try {
|
438
438
|
for (const item of loaders) {
|
439
439
|
if (typeof item.options === "string" && item.options[0] === "?") {
|
440
|
-
const ident = item.options.
|
440
|
+
const ident = item.options.slice(1);
|
441
441
|
if (ident === "[[missing ident]]") {
|
442
442
|
throw new Error(
|
443
443
|
"No ident is provided by referenced loader. " +
|
package/lib/RuntimePlugin.js
CHANGED
@@ -11,6 +11,7 @@ const RuntimeRequirementsDependency = require("./dependencies/RuntimeRequirement
|
|
11
11
|
const JavascriptModulesPlugin = require("./javascript/JavascriptModulesPlugin");
|
12
12
|
const AsyncModuleRuntimeModule = require("./runtime/AsyncModuleRuntimeModule");
|
13
13
|
const AutoPublicPathRuntimeModule = require("./runtime/AutoPublicPathRuntimeModule");
|
14
|
+
const BaseUriRuntimeModule = require("./runtime/BaseUriRuntimeModule");
|
14
15
|
const CompatGetDefaultExportRuntimeModule = require("./runtime/CompatGetDefaultExportRuntimeModule");
|
15
16
|
const CompatRuntimeModule = require("./runtime/CompatRuntimeModule");
|
16
17
|
const CreateFakeNamespaceObjectRuntimeModule = require("./runtime/CreateFakeNamespaceObjectRuntimeModule");
|
@@ -96,6 +97,15 @@ class RuntimePlugin {
|
|
96
97
|
*/
|
97
98
|
apply(compiler) {
|
98
99
|
compiler.hooks.compilation.tap("RuntimePlugin", compilation => {
|
100
|
+
const globalChunkLoading = compilation.outputOptions.chunkLoading;
|
101
|
+
const isChunkLoadingDisabledForChunk = chunk => {
|
102
|
+
const options = chunk.getEntryOptions();
|
103
|
+
const chunkLoading =
|
104
|
+
options && options.chunkLoading !== undefined
|
105
|
+
? options.chunkLoading
|
106
|
+
: globalChunkLoading;
|
107
|
+
return chunkLoading === false;
|
108
|
+
};
|
99
109
|
compilation.dependencyTemplates.set(
|
100
110
|
RuntimeRequirementsDependency,
|
101
111
|
new RuntimeRequirementsDependency.Template()
|
@@ -413,6 +423,14 @@ class RuntimePlugin {
|
|
413
423
|
);
|
414
424
|
return true;
|
415
425
|
});
|
426
|
+
compilation.hooks.runtimeRequirementInTree
|
427
|
+
.for(RuntimeGlobals.baseURI)
|
428
|
+
.tap("RuntimePlugin", chunk => {
|
429
|
+
if (isChunkLoadingDisabledForChunk(chunk)) {
|
430
|
+
compilation.addRuntimeModule(chunk, new BaseUriRuntimeModule());
|
431
|
+
return true;
|
432
|
+
}
|
433
|
+
});
|
416
434
|
// TODO webpack 6: remove CompatRuntimeModule
|
417
435
|
compilation.hooks.additionalTreeRuntimeRequirements.tap(
|
418
436
|
"RuntimePlugin",
|
@@ -8,6 +8,7 @@
|
|
8
8
|
const mimeTypes = require("mime-types");
|
9
9
|
const path = require("path");
|
10
10
|
const { RawSource } = require("webpack-sources");
|
11
|
+
const ConcatenationScope = require("../ConcatenationScope");
|
11
12
|
const Generator = require("../Generator");
|
12
13
|
const RuntimeGlobals = require("../RuntimeGlobals");
|
13
14
|
const createHash = require("../util/createHash");
|
@@ -23,6 +24,7 @@ const nonNumericOnlyHash = require("../util/nonNumericOnlyHash");
|
|
23
24
|
/** @typedef {import("../Generator").GenerateContext} GenerateContext */
|
24
25
|
/** @typedef {import("../Generator").UpdateHashContext} UpdateHashContext */
|
25
26
|
/** @typedef {import("../Module")} Module */
|
27
|
+
/** @typedef {import("../Module").ConcatenationBailoutReasonContext} ConcatenationBailoutReasonContext */
|
26
28
|
/** @typedef {import("../NormalModule")} NormalModule */
|
27
29
|
/** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
|
28
30
|
/** @typedef {import("../util/Hash")} Hash */
|
@@ -145,6 +147,15 @@ class AssetGenerator extends Generator {
|
|
145
147
|
).replace(/^\.\//, "");
|
146
148
|
}
|
147
149
|
|
150
|
+
/**
|
151
|
+
* @param {NormalModule} module module for which the bailout reason should be determined
|
152
|
+
* @param {ConcatenationBailoutReasonContext} context context
|
153
|
+
* @returns {string | undefined} reason why this module can't be concatenated, undefined when it can be concatenated
|
154
|
+
*/
|
155
|
+
getConcatenationBailoutReason(module, context) {
|
156
|
+
return undefined;
|
157
|
+
}
|
158
|
+
|
148
159
|
/**
|
149
160
|
* @param {NormalModule} module module
|
150
161
|
* @returns {string} mime type
|
@@ -198,14 +209,21 @@ class AssetGenerator extends Generator {
|
|
198
209
|
*/
|
199
210
|
generate(
|
200
211
|
module,
|
201
|
-
{
|
212
|
+
{
|
213
|
+
runtime,
|
214
|
+
concatenationScope,
|
215
|
+
chunkGraph,
|
216
|
+
runtimeTemplate,
|
217
|
+
runtimeRequirements,
|
218
|
+
type,
|
219
|
+
getData
|
220
|
+
}
|
202
221
|
) {
|
203
222
|
switch (type) {
|
204
223
|
case "asset":
|
205
224
|
return module.originalSource();
|
206
225
|
default: {
|
207
|
-
|
208
|
-
|
226
|
+
let content;
|
209
227
|
const originalSource = module.originalSource();
|
210
228
|
if (module.buildInfo.dataUrl) {
|
211
229
|
let encodedSource;
|
@@ -255,11 +273,7 @@ class AssetGenerator extends Generator {
|
|
255
273
|
}
|
256
274
|
const data = getData();
|
257
275
|
data.set("url", Buffer.from(encodedSource));
|
258
|
-
|
259
|
-
`${RuntimeGlobals.module}.exports = ${JSON.stringify(
|
260
|
-
encodedSource
|
261
|
-
)};`
|
262
|
-
);
|
276
|
+
content = JSON.stringify(encodedSource);
|
263
277
|
} else {
|
264
278
|
const assetModuleFilename =
|
265
279
|
this.filename || runtimeTemplate.outputOptions.assetModuleFilename;
|
@@ -343,9 +357,22 @@ class AssetGenerator extends Generator {
|
|
343
357
|
data.set("filename", filename);
|
344
358
|
data.set("assetInfo", assetInfo);
|
345
359
|
}
|
360
|
+
content = assetPath;
|
361
|
+
}
|
346
362
|
|
363
|
+
if (concatenationScope) {
|
364
|
+
concatenationScope.registerNamespaceExport(
|
365
|
+
ConcatenationScope.NAMESPACE_OBJECT_EXPORT
|
366
|
+
);
|
367
|
+
return new RawSource(
|
368
|
+
`${runtimeTemplate.supportsConst() ? "const" : "var"} ${
|
369
|
+
ConcatenationScope.NAMESPACE_OBJECT_EXPORT
|
370
|
+
} = ${content};`
|
371
|
+
);
|
372
|
+
} else {
|
373
|
+
runtimeRequirements.add(RuntimeGlobals.module);
|
347
374
|
return new RawSource(
|
348
|
-
`${RuntimeGlobals.module}.exports = ${
|
375
|
+
`${RuntimeGlobals.module}.exports = ${content};`
|
349
376
|
);
|
350
377
|
}
|
351
378
|
}
|
package/lib/asset/AssetParser.js
CHANGED
@@ -31,6 +31,7 @@ class AssetParser extends Parser {
|
|
31
31
|
}
|
32
32
|
state.module.buildInfo.strict = true;
|
33
33
|
state.module.buildMeta.exportsType = "default";
|
34
|
+
state.module.buildMeta.defaultObject = false;
|
34
35
|
|
35
36
|
if (typeof this.dataUrlCondition === "function") {
|
36
37
|
state.module.buildInfo.dataUrl = this.dataUrlCondition(source, {
|
@@ -6,11 +6,13 @@
|
|
6
6
|
"use strict";
|
7
7
|
|
8
8
|
const { RawSource } = require("webpack-sources");
|
9
|
+
const ConcatenationScope = require("../ConcatenationScope");
|
9
10
|
const Generator = require("../Generator");
|
10
11
|
const RuntimeGlobals = require("../RuntimeGlobals");
|
11
12
|
|
12
13
|
/** @typedef {import("webpack-sources").Source} Source */
|
13
14
|
/** @typedef {import("../Generator").GenerateContext} GenerateContext */
|
15
|
+
/** @typedef {import("../Module").ConcatenationBailoutReasonContext} ConcatenationBailoutReasonContext */
|
14
16
|
/** @typedef {import("../NormalModule")} NormalModule */
|
15
17
|
|
16
18
|
const TYPES = new Set(["javascript"]);
|
@@ -21,9 +23,10 @@ class AssetSourceGenerator extends Generator {
|
|
21
23
|
* @param {GenerateContext} generateContext context for generate
|
22
24
|
* @returns {Source} generated code
|
23
25
|
*/
|
24
|
-
generate(
|
25
|
-
|
26
|
-
|
26
|
+
generate(
|
27
|
+
module,
|
28
|
+
{ concatenationScope, chunkGraph, runtimeTemplate, runtimeRequirements }
|
29
|
+
) {
|
27
30
|
const originalSource = module.originalSource();
|
28
31
|
|
29
32
|
if (!originalSource) {
|
@@ -38,9 +41,31 @@ class AssetSourceGenerator extends Generator {
|
|
38
41
|
} else {
|
39
42
|
encodedSource = content.toString("utf-8");
|
40
43
|
}
|
41
|
-
|
42
|
-
|
43
|
-
)
|
44
|
+
|
45
|
+
let sourceContent;
|
46
|
+
if (concatenationScope) {
|
47
|
+
concatenationScope.registerNamespaceExport(
|
48
|
+
ConcatenationScope.NAMESPACE_OBJECT_EXPORT
|
49
|
+
);
|
50
|
+
sourceContent = `${runtimeTemplate.supportsConst() ? "const" : "var"} ${
|
51
|
+
ConcatenationScope.NAMESPACE_OBJECT_EXPORT
|
52
|
+
} = ${JSON.stringify(encodedSource)};`;
|
53
|
+
} else {
|
54
|
+
runtimeRequirements.add(RuntimeGlobals.module);
|
55
|
+
sourceContent = `${RuntimeGlobals.module}.exports = ${JSON.stringify(
|
56
|
+
encodedSource
|
57
|
+
)};`;
|
58
|
+
}
|
59
|
+
return new RawSource(sourceContent);
|
60
|
+
}
|
61
|
+
|
62
|
+
/**
|
63
|
+
* @param {NormalModule} module module for which the bailout reason should be determined
|
64
|
+
* @param {ConcatenationBailoutReasonContext} context context
|
65
|
+
* @returns {string | undefined} reason why this module can't be concatenated, undefined when it can be concatenated
|
66
|
+
*/
|
67
|
+
getConcatenationBailoutReason(module, context) {
|
68
|
+
return undefined;
|
44
69
|
}
|
45
70
|
|
46
71
|
/**
|
@@ -639,10 +639,14 @@ class PackContentItems {
|
|
639
639
|
} catch (e) {
|
640
640
|
rollback(s);
|
641
641
|
if (e === NOT_SERIALIZABLE) continue;
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
|
642
|
+
const msg = "Skipped not serializable cache item";
|
643
|
+
if (e.message.includes("ModuleBuildError")) {
|
644
|
+
logger.log(`${msg} (in build error): ${e.message}`);
|
645
|
+
logger.debug(`${msg} '${key}' (in build error): ${e.stack}`);
|
646
|
+
} else {
|
647
|
+
logger.warn(`${msg}: ${e.message}`);
|
648
|
+
logger.debug(`${msg} '${key}': ${e.stack}`);
|
649
|
+
}
|
646
650
|
}
|
647
651
|
}
|
648
652
|
write(null);
|
package/lib/config/defaults.js
CHANGED
@@ -715,7 +715,15 @@ const applyOutputDefaults = (
|
|
715
715
|
};
|
716
716
|
|
717
717
|
F(output, "uniqueName", () => {
|
718
|
-
const libraryName = getLibraryName(output.library)
|
718
|
+
const libraryName = getLibraryName(output.library).replace(
|
719
|
+
/^\[(\\*[\w:]+\\*)\](\.)|(\.)\[(\\*[\w:]+\\*)\](?=\.|$)|\[(\\*[\w:]+\\*)\]/g,
|
720
|
+
(m, a, d1, d2, b, c) => {
|
721
|
+
const content = a || b || c;
|
722
|
+
return content.startsWith("\\") && content.endsWith("\\")
|
723
|
+
? `${d2 || ""}[${content.slice(1, -1)}]${d1 || ""}`
|
724
|
+
: "";
|
725
|
+
}
|
726
|
+
);
|
719
727
|
if (libraryName) return libraryName;
|
720
728
|
const pkgPath = path.resolve(context, "package.json");
|
721
729
|
try {
|
@@ -73,9 +73,9 @@ class RemoteRuntimeModule extends RuntimeModule {
|
|
73
73
|
Template.indent(
|
74
74
|
`error.message += '\\nwhile loading "' + data[1] + '" from ' + data[2];`
|
75
75
|
),
|
76
|
-
|
77
|
-
|
78
|
-
])}`,
|
76
|
+
`${
|
77
|
+
RuntimeGlobals.moduleFactories
|
78
|
+
}[id] = ${runtimeTemplate.basicFunction("", ["throw error;"])}`,
|
79
79
|
"data.p = 0;"
|
80
80
|
])};`,
|
81
81
|
`var handleFunction = ${runtimeTemplate.basicFunction(
|
@@ -111,10 +111,11 @@ class RemoteRuntimeModule extends RuntimeModule {
|
|
111
111
|
)};`,
|
112
112
|
`var onFactory = ${runtimeTemplate.basicFunction("factory", [
|
113
113
|
"data.p = 1;",
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
114
|
+
`${
|
115
|
+
RuntimeGlobals.moduleFactories
|
116
|
+
}[id] = ${runtimeTemplate.basicFunction("module", [
|
117
|
+
"module.exports = factory();"
|
118
|
+
])}`
|
118
119
|
])};`,
|
119
120
|
"handleFunction(__webpack_require__, data[2], 0, 0, onExternal, 1);"
|
120
121
|
])});`
|
@@ -201,7 +201,7 @@ class CommonJsExportsParserPlugin {
|
|
201
201
|
if (expr.arguments[1].type === "SpreadElement") return;
|
202
202
|
if (expr.arguments[2].type === "SpreadElement") return;
|
203
203
|
const exportsArg = parser.evaluateExpression(expr.arguments[0]);
|
204
|
-
if (!exportsArg
|
204
|
+
if (!exportsArg.isIdentifier()) return;
|
205
205
|
if (
|
206
206
|
exportsArg.identifier !== "exports" &&
|
207
207
|
exportsArg.identifier !== "module.exports" &&
|
@@ -210,7 +210,6 @@ class CommonJsExportsParserPlugin {
|
|
210
210
|
return;
|
211
211
|
}
|
212
212
|
const propertyArg = parser.evaluateExpression(expr.arguments[1]);
|
213
|
-
if (!propertyArg) return;
|
214
213
|
const property = propertyArg.asString();
|
215
214
|
if (typeof property !== "string") return;
|
216
215
|
enableStructuredExports();
|
@@ -28,8 +28,8 @@ const splitContextFromPrefix = prefix => {
|
|
28
28
|
const idx = prefix.lastIndexOf("/");
|
29
29
|
let context = ".";
|
30
30
|
if (idx >= 0) {
|
31
|
-
context = prefix.
|
32
|
-
prefix = `.${prefix.
|
31
|
+
context = prefix.slice(0, idx);
|
32
|
+
prefix = `.${prefix.slice(idx)}`;
|
33
33
|
}
|
34
34
|
return {
|
35
35
|
context,
|
@@ -14,11 +14,27 @@ const ModuleDependency = require("./ModuleDependency");
|
|
14
14
|
/** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
|
15
15
|
|
16
16
|
class ContextElementDependency extends ModuleDependency {
|
17
|
-
|
17
|
+
/**
|
18
|
+
* @param {string} request request
|
19
|
+
* @param {string|undefined} userRequest user request
|
20
|
+
* @param {string} typePrefix type prefix
|
21
|
+
* @param {string} category category
|
22
|
+
* @param {string[][]=} referencedExports referenced exports
|
23
|
+
* @param {string=} context context
|
24
|
+
*/
|
25
|
+
constructor(
|
26
|
+
request,
|
27
|
+
userRequest,
|
28
|
+
typePrefix,
|
29
|
+
category,
|
30
|
+
referencedExports,
|
31
|
+
context
|
32
|
+
) {
|
18
33
|
super(request);
|
19
34
|
this.referencedExports = referencedExports;
|
20
35
|
this._typePrefix = typePrefix;
|
21
36
|
this._category = category;
|
37
|
+
this._context = context || undefined;
|
22
38
|
|
23
39
|
if (userRequest) {
|
24
40
|
this.userRequest = userRequest;
|
@@ -33,6 +49,20 @@ class ContextElementDependency extends ModuleDependency {
|
|
33
49
|
return "context element";
|
34
50
|
}
|
35
51
|
|
52
|
+
/**
|
53
|
+
* @returns {string | undefined} a request context
|
54
|
+
*/
|
55
|
+
getContext() {
|
56
|
+
return this._context;
|
57
|
+
}
|
58
|
+
|
59
|
+
/**
|
60
|
+
* @returns {string | null} an identifier to merge equal requests
|
61
|
+
*/
|
62
|
+
getResourceIdentifier() {
|
63
|
+
return `context${this._context || ""}|${super.getResourceIdentifier()}`;
|
64
|
+
}
|
65
|
+
|
36
66
|
get category() {
|
37
67
|
return this._category;
|
38
68
|
}
|
@@ -56,6 +86,7 @@ class ContextElementDependency extends ModuleDependency {
|
|
56
86
|
const { write } = context;
|
57
87
|
write(this._typePrefix);
|
58
88
|
write(this._category);
|
89
|
+
write(this._context);
|
59
90
|
write(this.referencedExports);
|
60
91
|
super.serialize(context);
|
61
92
|
}
|
@@ -64,6 +95,7 @@ class ContextElementDependency extends ModuleDependency {
|
|
64
95
|
const { read } = context;
|
65
96
|
this._typePrefix = read();
|
66
97
|
this._category = read();
|
98
|
+
this._context = read();
|
67
99
|
this.referencedExports = read();
|
68
100
|
super.deserialize(context);
|
69
101
|
}
|
@@ -0,0 +1,95 @@
|
|
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 HarmonyImportSpecifierDependency = require("./HarmonyImportSpecifierDependency");
|
10
|
+
|
11
|
+
/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
12
|
+
/** @typedef {import("../ChunkGraph")} ChunkGraph */
|
13
|
+
/** @typedef {import("../Dependency")} Dependency */
|
14
|
+
/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
15
|
+
|
16
|
+
/**
|
17
|
+
* Dependency for static evaluating import specifier. e.g.
|
18
|
+
* @example
|
19
|
+
* import a from "a";
|
20
|
+
* "x" in a;
|
21
|
+
* a.x !== undefined; // if x value statically analyzable
|
22
|
+
*/
|
23
|
+
class HarmonyEvaluatedImportSpecifierDependency extends HarmonyImportSpecifierDependency {
|
24
|
+
constructor(request, sourceOrder, ids, name, range, assertions, operator) {
|
25
|
+
super(request, sourceOrder, ids, name, range, false, assertions);
|
26
|
+
this.operator = operator;
|
27
|
+
}
|
28
|
+
|
29
|
+
get type() {
|
30
|
+
return `evaluated X ${this.operator} harmony import specifier`;
|
31
|
+
}
|
32
|
+
|
33
|
+
serialize(context) {
|
34
|
+
super.serialize(context);
|
35
|
+
const { write } = context;
|
36
|
+
write(this.operator);
|
37
|
+
}
|
38
|
+
|
39
|
+
deserialize(context) {
|
40
|
+
super.deserialize(context);
|
41
|
+
const { read } = context;
|
42
|
+
this.operator = read();
|
43
|
+
}
|
44
|
+
}
|
45
|
+
|
46
|
+
makeSerializable(
|
47
|
+
HarmonyEvaluatedImportSpecifierDependency,
|
48
|
+
"webpack/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency"
|
49
|
+
);
|
50
|
+
|
51
|
+
HarmonyEvaluatedImportSpecifierDependency.Template = class HarmonyEvaluatedImportSpecifierDependencyTemplate extends (
|
52
|
+
HarmonyImportSpecifierDependency.Template
|
53
|
+
) {
|
54
|
+
/**
|
55
|
+
* @param {Dependency} dependency the dependency for which the template should be applied
|
56
|
+
* @param {ReplaceSource} source the current replace source which can be modified
|
57
|
+
* @param {DependencyTemplateContext} templateContext the context object
|
58
|
+
* @returns {void}
|
59
|
+
*/
|
60
|
+
apply(dependency, source, templateContext) {
|
61
|
+
const dep = /** @type {HarmonyEvaluatedImportSpecifierDependency} */ (
|
62
|
+
dependency
|
63
|
+
);
|
64
|
+
const { moduleGraph, runtime } = templateContext;
|
65
|
+
const connection = moduleGraph.getConnection(dep);
|
66
|
+
// Skip rendering depending when dependency is conditional
|
67
|
+
if (connection && !connection.isTargetActive(runtime)) return;
|
68
|
+
|
69
|
+
const exportsInfo = moduleGraph.getExportsInfo(connection.module);
|
70
|
+
const ids = dep.getIds(moduleGraph);
|
71
|
+
const value = exportsInfo.isExportProvided(ids);
|
72
|
+
|
73
|
+
if (typeof value === "boolean") {
|
74
|
+
source.replace(dep.range[0], dep.range[1] - 1, `${value}`);
|
75
|
+
} else {
|
76
|
+
const usedName = exportsInfo.getUsedName(ids, runtime);
|
77
|
+
|
78
|
+
const code = this._getCodeForIds(
|
79
|
+
dep,
|
80
|
+
source,
|
81
|
+
templateContext,
|
82
|
+
ids.slice(0, -1)
|
83
|
+
);
|
84
|
+
source.replace(
|
85
|
+
dep.range[0],
|
86
|
+
dep.range[1] - 1,
|
87
|
+
`${
|
88
|
+
usedName ? JSON.stringify(usedName[usedName.length - 1]) : '""'
|
89
|
+
} in ${code}`
|
90
|
+
);
|
91
|
+
}
|
92
|
+
}
|
93
|
+
};
|
94
|
+
|
95
|
+
module.exports = HarmonyEvaluatedImportSpecifierDependency;
|