HarmonyImportDependency.js
10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const ConditionalInitFragment = require("../ConditionalInitFragment");
const Dependency = require("../Dependency");
const HarmonyLinkingError = require("../HarmonyLinkingError");
const InitFragment = require("../InitFragment");
const Template = require("../Template");
const AwaitDependenciesInitFragment = require("../async-modules/AwaitDependenciesInitFragment");
const { filterRuntime, mergeRuntime } = require("../util/runtime");
const ModuleDependency = require("./ModuleDependency");
/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
/** @typedef {import("webpack-sources").Source} Source */
/** @typedef {import("../ChunkGraph")} ChunkGraph */
/** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */
/** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
/** @typedef {import("../Module")} Module */
/** @typedef {import("../ModuleGraph")} ModuleGraph */
/** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
/** @typedef {import("../WebpackError")} WebpackError */
/** @typedef {import("../util/Hash")} Hash */
/** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
const ExportPresenceModes = {
NONE: /** @type {0} */ (0),
WARN: /** @type {1} */ (1),
AUTO: /** @type {2} */ (2),
ERROR: /** @type {3} */ (3),
fromUserOption(str) {
switch (str) {
case "error":
return ExportPresenceModes.ERROR;
case "warn":
return ExportPresenceModes.WARN;
case "auto":
return ExportPresenceModes.AUTO;
case false:
return ExportPresenceModes.NONE;
default:
throw new Error(`Invalid export presence value ${str}`);
}
}
};
class HarmonyImportDependency extends ModuleDependency {
/**
*
* @param {string} request request string
* @param {number} sourceOrder source order
* @param {Record<string, any>=} assertions import assertions
*/
constructor(request, sourceOrder, assertions) {
super(request);
this.sourceOrder = sourceOrder;
this.assertions = assertions;
}
get category() {
return "esm";
}
/**
* Returns list of exports referenced by this dependency
* @param {ModuleGraph} moduleGraph module graph
* @param {RuntimeSpec} runtime the runtime for which the module is analysed
* @returns {(string[] | ReferencedExport)[]} referenced exports
*/
getReferencedExports(moduleGraph, runtime) {
return Dependency.NO_EXPORTS_REFERENCED;
}
/**
* @param {ModuleGraph} moduleGraph the module graph
* @returns {string} name of the variable for the import
*/
getImportVar(moduleGraph) {
const module = moduleGraph.getParentModule(this);
const meta = moduleGraph.getMeta(module);
let importVarMap = meta.importVarMap;
if (!importVarMap) meta.importVarMap = importVarMap = new Map();
let importVar = importVarMap.get(moduleGraph.getModule(this));
if (importVar) return importVar;
importVar = `${Template.toIdentifier(
`${this.userRequest}`
)}__WEBPACK_IMPORTED_MODULE_${importVarMap.size}__`;
importVarMap.set(moduleGraph.getModule(this), importVar);
return importVar;
}
/**
* @param {boolean} update create new variables or update existing one
* @param {DependencyTemplateContext} templateContext the template context
* @returns {[string, string]} the import statement and the compat statement
*/
getImportStatement(
update,
{ runtimeTemplate, module, moduleGraph, chunkGraph, runtimeRequirements }
) {
return runtimeTemplate.importStatement({
update,
module: moduleGraph.getModule(this),
chunkGraph,
importVar: this.getImportVar(moduleGraph),
request: this.request,
originModule: module,
runtimeRequirements
});
}
/**
* @param {ModuleGraph} moduleGraph module graph
* @param {string[]} ids imported ids
* @param {string} additionalMessage extra info included in the error message
* @returns {WebpackError[] | undefined} errors
*/
getLinkingErrors(moduleGraph, ids, additionalMessage) {
const importedModule = moduleGraph.getModule(this);
// ignore errors for missing or failed modules
if (!importedModule || importedModule.getNumberOfErrors() > 0) {
return;
}
const parentModule = moduleGraph.getParentModule(this);
const exportsType = importedModule.getExportsType(
moduleGraph,
parentModule.buildMeta.strictHarmonyModule
);
if (exportsType === "namespace" || exportsType === "default-with-named") {
if (ids.length === 0) {
return;
}
if (
(exportsType !== "default-with-named" || ids[0] !== "default") &&
moduleGraph.isExportProvided(importedModule, ids) === false
) {
// We are sure that it's not provided
// Try to provide detailed info in the error message
let pos = 0;
let exportsInfo = moduleGraph.getExportsInfo(importedModule);
while (pos < ids.length && exportsInfo) {
const id = ids[pos++];
const exportInfo = exportsInfo.getReadOnlyExportInfo(id);
if (exportInfo.provided === false) {
// We are sure that it's not provided
const providedExports = exportsInfo.getProvidedExports();
const moreInfo = !Array.isArray(providedExports)
? " (possible exports unknown)"
: providedExports.length === 0
? " (module has no exports)"
: ` (possible exports: ${providedExports.join(", ")})`;
return [
new HarmonyLinkingError(
`export ${ids
.slice(0, pos)
.map(id => `'${id}'`)
.join(".")} ${additionalMessage} was not found in '${
this.userRequest
}'${moreInfo}`
)
];
}
exportsInfo = exportInfo.getNestedExportsInfo();
}
// General error message
return [
new HarmonyLinkingError(
`export ${ids
.map(id => `'${id}'`)
.join(".")} ${additionalMessage} was not found in '${
this.userRequest
}'`
)
];
}
}
switch (exportsType) {
case "default-only":
// It's has only a default export
if (ids.length > 0 && ids[0] !== "default") {
// In strict harmony modules we only support the default export
return [
new HarmonyLinkingError(
`Can't import the named export ${ids
.map(id => `'${id}'`)
.join(
"."
)} ${additionalMessage} from default-exporting module (only default export is available)`
)
];
}
break;
case "default-with-named":
// It has a default export and named properties redirect
// In some cases we still want to warn here
if (
ids.length > 0 &&
ids[0] !== "default" &&
importedModule.buildMeta.defaultObject === "redirect-warn"
) {
// For these modules only the default export is supported
return [
new HarmonyLinkingError(
`Should not import the named export ${ids
.map(id => `'${id}'`)
.join(
"."
)} ${additionalMessage} from default-exporting module (only default export is available soon)`
)
];
}
break;
}
}
serialize(context) {
const { write } = context;
write(this.sourceOrder);
write(this.assertions);
super.serialize(context);
}
deserialize(context) {
const { read } = context;
this.sourceOrder = read();
this.assertions = read();
super.deserialize(context);
}
}
module.exports = HarmonyImportDependency;
/** @type {WeakMap<Module, WeakMap<Module, RuntimeSpec | boolean>>} */
const importEmittedMap = new WeakMap();
HarmonyImportDependency.Template = class HarmonyImportDependencyTemplate extends (
ModuleDependency.Template
) {
/**
* @param {Dependency} dependency the dependency for which the template should be applied
* @param {ReplaceSource} source the current replace source which can be modified
* @param {DependencyTemplateContext} templateContext the context object
* @returns {void}
*/
apply(dependency, source, templateContext) {
const dep = /** @type {HarmonyImportDependency} */ (dependency);
const { module, chunkGraph, moduleGraph, runtime } = templateContext;
const connection = moduleGraph.getConnection(dep);
if (connection && !connection.isTargetActive(runtime)) return;
const referencedModule = connection && connection.module;
if (
connection &&
connection.weak &&
referencedModule &&
chunkGraph.getModuleId(referencedModule) === null
) {
// in weak references, module might not be in any chunk
// but that's ok, we don't need that logic in this case
return;
}
const moduleKey = referencedModule
? referencedModule.identifier()
: dep.request;
const key = `harmony import ${moduleKey}`;
const runtimeCondition = dep.weak
? false
: connection
? filterRuntime(runtime, r => connection.isTargetActive(r))
: true;
if (module && referencedModule) {
let emittedModules = importEmittedMap.get(module);
if (emittedModules === undefined) {
emittedModules = new WeakMap();
importEmittedMap.set(module, emittedModules);
}
let mergedRuntimeCondition = runtimeCondition;
const oldRuntimeCondition = emittedModules.get(referencedModule) || false;
if (oldRuntimeCondition !== false && mergedRuntimeCondition !== true) {
if (mergedRuntimeCondition === false || oldRuntimeCondition === true) {
mergedRuntimeCondition = oldRuntimeCondition;
} else {
mergedRuntimeCondition = mergeRuntime(
oldRuntimeCondition,
mergedRuntimeCondition
);
}
}
emittedModules.set(referencedModule, mergedRuntimeCondition);
}
const importStatement = dep.getImportStatement(false, templateContext);
if (
referencedModule &&
templateContext.moduleGraph.isAsync(referencedModule)
) {
templateContext.initFragments.push(
new ConditionalInitFragment(
importStatement[0],
InitFragment.STAGE_HARMONY_IMPORTS,
dep.sourceOrder,
key,
runtimeCondition
)
);
templateContext.initFragments.push(
new AwaitDependenciesInitFragment(
new Set([dep.getImportVar(templateContext.moduleGraph)])
)
);
templateContext.initFragments.push(
new ConditionalInitFragment(
importStatement[1],
InitFragment.STAGE_ASYNC_HARMONY_IMPORTS,
dep.sourceOrder,
key + " compat",
runtimeCondition
)
);
} else {
templateContext.initFragments.push(
new ConditionalInitFragment(
importStatement[0] + importStatement[1],
InitFragment.STAGE_HARMONY_IMPORTS,
dep.sourceOrder,
key,
runtimeCondition
)
);
}
}
/**
*
* @param {Module} module the module
* @param {Module} referencedModule the referenced module
* @returns {RuntimeSpec | boolean} runtimeCondition in which this import has been emitted
*/
static getImportEmittedRuntime(module, referencedModule) {
const emittedModules = importEmittedMap.get(module);
if (emittedModules === undefined) return false;
return emittedModules.get(referencedModule) || false;
}
};
module.exports.ExportPresenceModes = ExportPresenceModes;