webpack 5.69.0 → 5.71.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 +10 -4
- package/lib/Chunk.js +1 -1
- package/lib/ChunkGroup.js +1 -1
- package/lib/CleanPlugin.js +64 -18
- package/lib/Compilation.js +51 -25
- package/lib/Compiler.js +16 -3
- package/lib/ConstPlugin.js +2 -2
- package/lib/ContextModule.js +54 -22
- package/lib/ContextModuleFactory.js +13 -12
- package/lib/DelegatedModuleFactoryPlugin.js +1 -1
- package/lib/Dependency.js +7 -0
- package/lib/EntryOptionPlugin.js +1 -0
- package/lib/ErrorHelpers.js +2 -2
- package/lib/ExternalModuleFactoryPlugin.js +4 -4
- package/lib/FileSystemInfo.js +8 -0
- package/lib/Generator.js +1 -0
- package/lib/LoaderOptionsPlugin.js +1 -1
- package/lib/Module.js +2 -0
- package/lib/ModuleFilenameHelpers.js +3 -3
- package/lib/ModuleHashingError.js +29 -0
- package/lib/NodeStuffPlugin.js +10 -0
- package/lib/NormalModule.js +23 -18
- package/lib/NormalModuleFactory.js +17 -10
- package/lib/ProgressPlugin.js +3 -4
- package/lib/RuntimePlugin.js +18 -0
- package/lib/RuntimeTemplate.js +1 -0
- package/lib/WebpackOptionsApply.js +2 -0
- package/lib/asset/AssetGenerator.js +119 -31
- package/lib/cache/ResolverCachePlugin.js +15 -6
- package/lib/config/browserslistTargetHandler.js +3 -5
- package/lib/config/defaults.js +9 -1
- package/lib/config/normalization.js +1 -0
- package/lib/dependencies/CommonJsExportsParserPlugin.js +1 -2
- package/lib/dependencies/ContextDependencyHelpers.js +3 -3
- package/lib/dependencies/ContextElementDependency.js +33 -1
- package/lib/dependencies/HarmonyAcceptImportDependency.js +5 -3
- package/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js +95 -0
- package/lib/dependencies/HarmonyExportInitFragment.js +4 -1
- package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +127 -43
- package/lib/dependencies/HarmonyImportSpecifierDependency.js +22 -8
- 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/LoaderPlugin.js +2 -0
- package/lib/dependencies/RequireContextDependency.js +0 -16
- package/lib/esm/ModuleChunkLoadingPlugin.js +3 -1
- package/lib/esm/ModuleChunkLoadingRuntimeModule.js +24 -8
- 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/node/ReadFileChunkLoadingRuntimeModule.js +22 -7
- package/lib/node/RequireChunkLoadingRuntimeModule.js +22 -7
- package/lib/runtime/BaseUriRuntimeModule.js +31 -0
- package/lib/schemes/HttpUriPlugin.js +44 -3
- package/lib/stats/DefaultStatsFactoryPlugin.js +1 -1
- package/lib/util/internalSerializables.js +4 -0
- package/lib/web/JsonpChunkLoadingRuntimeModule.js +17 -6
- package/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js +30 -20
- package/module.d.ts +15 -0
- package/package.json +2 -2
- package/schemas/WebpackOptions.check.js +1 -1
- package/schemas/WebpackOptions.json +17 -1
- package/schemas/plugins/schemes/HttpUriPlugin.check.js +1 -1
- package/schemas/plugins/schemes/HttpUriPlugin.json +4 -0
- package/types.d.ts +179 -83
@@ -113,6 +113,7 @@ const decodeDataUriContent = (encoding, content) => {
|
|
113
113
|
|
114
114
|
const JS_TYPES = new Set(["javascript"]);
|
115
115
|
const JS_AND_ASSET_TYPES = new Set(["javascript", "asset"]);
|
116
|
+
const DEFAULT_ENCODING = "base64";
|
116
117
|
|
117
118
|
class AssetGenerator extends Generator {
|
118
119
|
/**
|
@@ -131,6 +132,65 @@ class AssetGenerator extends Generator {
|
|
131
132
|
this.emit = emit;
|
132
133
|
}
|
133
134
|
|
135
|
+
/**
|
136
|
+
* @param {NormalModule} module module
|
137
|
+
* @param {RuntimeTemplate} runtimeTemplate runtime template
|
138
|
+
* @returns {string} source file name
|
139
|
+
*/
|
140
|
+
getSourceFileName(module, runtimeTemplate) {
|
141
|
+
return makePathsRelative(
|
142
|
+
runtimeTemplate.compilation.compiler.context,
|
143
|
+
module.matchResource || module.resource,
|
144
|
+
runtimeTemplate.compilation.compiler.root
|
145
|
+
).replace(/^\.\//, "");
|
146
|
+
}
|
147
|
+
|
148
|
+
/**
|
149
|
+
* @param {NormalModule} module module
|
150
|
+
* @returns {string} mime type
|
151
|
+
*/
|
152
|
+
getMimeType(module) {
|
153
|
+
if (typeof this.dataUrlOptions === "function") {
|
154
|
+
throw new Error(
|
155
|
+
"This method must not be called when dataUrlOptions is a function"
|
156
|
+
);
|
157
|
+
}
|
158
|
+
|
159
|
+
let mimeType = this.dataUrlOptions.mimetype;
|
160
|
+
if (mimeType === undefined) {
|
161
|
+
const ext = path.extname(module.nameForCondition());
|
162
|
+
if (
|
163
|
+
module.resourceResolveData &&
|
164
|
+
module.resourceResolveData.mimetype !== undefined
|
165
|
+
) {
|
166
|
+
mimeType =
|
167
|
+
module.resourceResolveData.mimetype +
|
168
|
+
module.resourceResolveData.parameters;
|
169
|
+
} else if (ext) {
|
170
|
+
mimeType = mimeTypes.lookup(ext);
|
171
|
+
|
172
|
+
if (typeof mimeType !== "string") {
|
173
|
+
throw new Error(
|
174
|
+
"DataUrl can't be generated automatically, " +
|
175
|
+
`because there is no mimetype for "${ext}" in mimetype database. ` +
|
176
|
+
'Either pass a mimetype via "generator.mimetype" or ' +
|
177
|
+
'use type: "asset/resource" to create a resource file instead of a DataUrl'
|
178
|
+
);
|
179
|
+
}
|
180
|
+
}
|
181
|
+
}
|
182
|
+
|
183
|
+
if (typeof mimeType !== "string") {
|
184
|
+
throw new Error(
|
185
|
+
"DataUrl can't be generated automatically. " +
|
186
|
+
'Either pass a mimetype via "generator.mimetype" or ' +
|
187
|
+
'use type: "asset/resource" to create a resource file instead of a DataUrl'
|
188
|
+
);
|
189
|
+
}
|
190
|
+
|
191
|
+
return mimeType;
|
192
|
+
}
|
193
|
+
|
134
194
|
/**
|
135
195
|
* @param {NormalModule} module module for which the code should be generated
|
136
196
|
* @param {GenerateContext} generateContext context for generate
|
@@ -170,31 +230,9 @@ class AssetGenerator extends Generator {
|
|
170
230
|
}
|
171
231
|
}
|
172
232
|
if (encoding === undefined) {
|
173
|
-
encoding =
|
174
|
-
}
|
175
|
-
let ext;
|
176
|
-
let mimeType = this.dataUrlOptions.mimetype;
|
177
|
-
if (mimeType === undefined) {
|
178
|
-
ext = path.extname(module.nameForCondition());
|
179
|
-
if (
|
180
|
-
module.resourceResolveData &&
|
181
|
-
module.resourceResolveData.mimetype !== undefined
|
182
|
-
) {
|
183
|
-
mimeType =
|
184
|
-
module.resourceResolveData.mimetype +
|
185
|
-
module.resourceResolveData.parameters;
|
186
|
-
} else if (ext) {
|
187
|
-
mimeType = mimeTypes.lookup(ext);
|
188
|
-
}
|
189
|
-
}
|
190
|
-
if (typeof mimeType !== "string") {
|
191
|
-
throw new Error(
|
192
|
-
"DataUrl can't be generated automatically, " +
|
193
|
-
`because there is no mimetype for "${ext}" in mimetype database. ` +
|
194
|
-
'Either pass a mimetype via "generator.mimetype" or ' +
|
195
|
-
'use type: "asset/resource" to create a resource file instead of a DataUrl'
|
196
|
-
);
|
233
|
+
encoding = DEFAULT_ENCODING;
|
197
234
|
}
|
235
|
+
const mimeType = this.getMimeType(module);
|
198
236
|
|
199
237
|
let encodedContent;
|
200
238
|
|
@@ -238,11 +276,10 @@ class AssetGenerator extends Generator {
|
|
238
276
|
runtimeTemplate.outputOptions.hashDigestLength
|
239
277
|
);
|
240
278
|
module.buildInfo.fullContentHash = fullHash;
|
241
|
-
const sourceFilename =
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
).replace(/^\.\//, "");
|
279
|
+
const sourceFilename = this.getSourceFileName(
|
280
|
+
module,
|
281
|
+
runtimeTemplate
|
282
|
+
);
|
246
283
|
let { path: filename, info: assetInfo } =
|
247
284
|
runtimeTemplate.compilation.getAssetPathWithInfo(
|
248
285
|
assetModuleFilename,
|
@@ -368,8 +405,59 @@ class AssetGenerator extends Generator {
|
|
368
405
|
* @param {Hash} hash hash that will be modified
|
369
406
|
* @param {UpdateHashContext} updateHashContext context for updating hash
|
370
407
|
*/
|
371
|
-
updateHash(hash, { module }) {
|
372
|
-
|
408
|
+
updateHash(hash, { module, runtime, runtimeTemplate, chunkGraph }) {
|
409
|
+
if (module.buildInfo.dataUrl) {
|
410
|
+
hash.update("data-url");
|
411
|
+
// this.dataUrlOptions as function should be pure and only depend on input source and filename
|
412
|
+
// therefore it doesn't need to be hashed
|
413
|
+
if (typeof this.dataUrlOptions === "function") {
|
414
|
+
const ident = /** @type {{ ident?: string }} */ (this.dataUrlOptions)
|
415
|
+
.ident;
|
416
|
+
if (ident) hash.update(ident);
|
417
|
+
} else {
|
418
|
+
if (
|
419
|
+
this.dataUrlOptions.encoding &&
|
420
|
+
this.dataUrlOptions.encoding !== DEFAULT_ENCODING
|
421
|
+
) {
|
422
|
+
hash.update(this.dataUrlOptions.encoding);
|
423
|
+
}
|
424
|
+
if (this.dataUrlOptions.mimetype)
|
425
|
+
hash.update(this.dataUrlOptions.mimetype);
|
426
|
+
// computed mimetype depends only on module filename which is already part of the hash
|
427
|
+
}
|
428
|
+
} else {
|
429
|
+
hash.update("resource");
|
430
|
+
|
431
|
+
const pathData = {
|
432
|
+
module,
|
433
|
+
runtime,
|
434
|
+
filename: this.getSourceFileName(module, runtimeTemplate),
|
435
|
+
chunkGraph,
|
436
|
+
contentHash: runtimeTemplate.contentHashReplacement
|
437
|
+
};
|
438
|
+
|
439
|
+
if (typeof this.publicPath === "function") {
|
440
|
+
hash.update("path");
|
441
|
+
const assetInfo = {};
|
442
|
+
hash.update(this.publicPath(pathData, assetInfo));
|
443
|
+
hash.update(JSON.stringify(assetInfo));
|
444
|
+
} else if (this.publicPath) {
|
445
|
+
hash.update("path");
|
446
|
+
hash.update(this.publicPath);
|
447
|
+
} else {
|
448
|
+
hash.update("no-path");
|
449
|
+
}
|
450
|
+
|
451
|
+
const assetModuleFilename =
|
452
|
+
this.filename || runtimeTemplate.outputOptions.assetModuleFilename;
|
453
|
+
const { path: filename, info } =
|
454
|
+
runtimeTemplate.compilation.getAssetPathWithInfo(
|
455
|
+
assetModuleFilename,
|
456
|
+
pathData
|
457
|
+
);
|
458
|
+
hash.update(filename);
|
459
|
+
hash.update(JSON.stringify(info));
|
460
|
+
}
|
373
461
|
}
|
374
462
|
}
|
375
463
|
|
@@ -129,8 +129,10 @@ class ResolverCachePlugin {
|
|
129
129
|
contextDependencies: new LazySet()
|
130
130
|
};
|
131
131
|
let yieldResult;
|
132
|
+
let withYield = false;
|
132
133
|
if (typeof newResolveContext.yield === "function") {
|
133
134
|
yieldResult = [];
|
135
|
+
withYield = true;
|
134
136
|
newResolveContext.yield = obj => yieldResult.push(obj);
|
135
137
|
}
|
136
138
|
const propagate = key => {
|
@@ -160,7 +162,10 @@ class ResolverCachePlugin {
|
|
160
162
|
snapshotOptions,
|
161
163
|
(err, snapshot) => {
|
162
164
|
if (err) return callback(err);
|
163
|
-
const resolveResult =
|
165
|
+
const resolveResult = withYield ? yieldResult : result;
|
166
|
+
// since we intercept resolve hook
|
167
|
+
// we still can get result in callback
|
168
|
+
if (withYield && result) yieldResult.push(result);
|
164
169
|
if (!snapshot) {
|
165
170
|
if (resolveResult) return callback(null, resolveResult);
|
166
171
|
return callback();
|
@@ -242,11 +247,15 @@ class ResolverCachePlugin {
|
|
242
247
|
yields = undefined;
|
243
248
|
callbacks = false;
|
244
249
|
} else {
|
245
|
-
|
246
|
-
const cb
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
+
if (err) {
|
251
|
+
for (const cb of callbacks) cb(err);
|
252
|
+
} else {
|
253
|
+
for (let i = 0; i < callbacks.length; i++) {
|
254
|
+
const cb = callbacks[i];
|
255
|
+
const yield_ = yields[i];
|
256
|
+
if (result) for (const r of result) yield_(r);
|
257
|
+
cb(null, null);
|
258
|
+
}
|
250
259
|
}
|
251
260
|
activeRequestsWithYield.delete(identifier);
|
252
261
|
yields = undefined;
|
@@ -121,8 +121,7 @@ const resolve = browsers => {
|
|
121
121
|
// baidu: Not supported
|
122
122
|
// and_uc: Not supported
|
123
123
|
// kaios: Not supported
|
124
|
-
|
125
|
-
node: [13, 14]
|
124
|
+
node: [12, 17]
|
126
125
|
});
|
127
126
|
|
128
127
|
return {
|
@@ -248,8 +247,7 @@ const resolve = browsers => {
|
|
248
247
|
// baidu: Not supported
|
249
248
|
// and_uc: Not supported
|
250
249
|
// kaios: Not supported
|
251
|
-
|
252
|
-
node: [13, 14]
|
250
|
+
node: [12, 17]
|
253
251
|
}),
|
254
252
|
dynamicImport: es6DynamicImport,
|
255
253
|
dynamicImportInWorker: es6DynamicImport && !anyNode,
|
@@ -272,7 +270,7 @@ const resolve = browsers => {
|
|
272
270
|
// baidu: Unknown support
|
273
271
|
// and_uc: Unknown support
|
274
272
|
// kaios: Unknown support
|
275
|
-
node:
|
273
|
+
node: 12
|
276
274
|
}),
|
277
275
|
optionalChaining: rawChecker({
|
278
276
|
chrome: 80,
|
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 {
|
@@ -488,6 +488,7 @@ const getNormalizedEntryStatic = entry => {
|
|
488
488
|
filename: value.filename,
|
489
489
|
layer: value.layer,
|
490
490
|
runtime: value.runtime,
|
491
|
+
baseUri: value.baseUri,
|
491
492
|
publicPath: value.publicPath,
|
492
493
|
chunkLoading: value.chunkLoading,
|
493
494
|
asyncChunks: value.asyncChunks,
|
@@ -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,
|
@@ -37,7 +37,7 @@ const splitContextFromPrefix = prefix => {
|
|
37
37
|
};
|
38
38
|
};
|
39
39
|
|
40
|
-
/** @typedef {Partial<Omit<ContextDependencyOptions, "resource"
|
40
|
+
/** @typedef {Partial<Omit<ContextDependencyOptions, "resource">>} PartialContextDependencyOptions */
|
41
41
|
|
42
42
|
/** @typedef {{ new(options: ContextDependencyOptions, range: [number, number], valueRange: [number, number]): ContextDependency }} ContextDependencyConstructor */
|
43
43
|
|
@@ -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
|
}
|
@@ -7,6 +7,7 @@
|
|
7
7
|
|
8
8
|
const makeSerializable = require("../util/makeSerializable");
|
9
9
|
const HarmonyImportDependency = require("./HarmonyImportDependency");
|
10
|
+
const NullDependency = require("./NullDependency");
|
10
11
|
|
11
12
|
/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
12
13
|
/** @typedef {import("../Dependency")} Dependency */
|
@@ -28,8 +29,9 @@ makeSerializable(
|
|
28
29
|
"webpack/lib/dependencies/HarmonyAcceptImportDependency"
|
29
30
|
);
|
30
31
|
|
31
|
-
HarmonyAcceptImportDependency.Template =
|
32
|
-
HarmonyImportDependency.Template
|
33
|
-
|
32
|
+
HarmonyAcceptImportDependency.Template =
|
33
|
+
/** @type {typeof HarmonyImportDependency.Template} */ (
|
34
|
+
NullDependency.Template
|
35
|
+
);
|
34
36
|
|
35
37
|
module.exports = HarmonyAcceptImportDependency;
|
@@ -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;
|
@@ -145,7 +145,10 @@ class HarmonyExportInitFragment extends InitFragment {
|
|
145
145
|
? `/* unused harmony export ${first(this.unusedExports)} */\n`
|
146
146
|
: "";
|
147
147
|
const definitions = [];
|
148
|
-
|
148
|
+
const orderedExportMap = Array.from(this.exportMap).sort(([a], [b]) =>
|
149
|
+
a < b ? -1 : 1
|
150
|
+
);
|
151
|
+
for (const [key, value] of orderedExportMap) {
|
149
152
|
definitions.push(
|
150
153
|
`\n/* harmony export */ ${JSON.stringify(
|
151
154
|
key
|