1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Tobias Koppers @sokra
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | const Dependency = require("../Dependency");
|
---|
9 | const Template = require("../Template");
|
---|
10 | const {
|
---|
11 | getDependencyUsedByExportsCondition
|
---|
12 | } = require("../optimize/InnerGraph");
|
---|
13 | const { getTrimmedIdsAndRange } = require("../util/chainedImports");
|
---|
14 | const makeSerializable = require("../util/makeSerializable");
|
---|
15 | const propertyAccess = require("../util/propertyAccess");
|
---|
16 | const HarmonyImportDependency = require("./HarmonyImportDependency");
|
---|
17 |
|
---|
18 | /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
---|
19 | /** @typedef {import("../ChunkGraph")} ChunkGraph */
|
---|
20 | /** @typedef {import("../Dependency").ExportsSpec} ExportsSpec */
|
---|
21 | /** @typedef {import("../Dependency").GetConditionFn} GetConditionFn */
|
---|
22 | /** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */
|
---|
23 | /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
|
---|
24 | /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
---|
25 | /** @typedef {import("../Module")} Module */
|
---|
26 | /** @typedef {import("../Module").BuildMeta} BuildMeta */
|
---|
27 | /** @typedef {import("../ModuleGraph")} ModuleGraph */
|
---|
28 | /** @typedef {import("../ModuleGraphConnection")} ModuleGraphConnection */
|
---|
29 | /** @typedef {import("../ModuleGraphConnection").ConnectionState} ConnectionState */
|
---|
30 | /** @typedef {import("../WebpackError")} WebpackError */
|
---|
31 | /** @typedef {import("../javascript/JavascriptParser").DestructuringAssignmentProperty} DestructuringAssignmentProperty */
|
---|
32 | /** @typedef {import("../javascript/JavascriptParser").ImportAttributes} ImportAttributes */
|
---|
33 | /** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
---|
34 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
---|
35 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
---|
36 | /** @typedef {import("../util/Hash")} Hash */
|
---|
37 | /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
|
---|
38 |
|
---|
39 | const idsSymbol = Symbol("HarmonyImportSpecifierDependency.ids");
|
---|
40 |
|
---|
41 | const { ExportPresenceModes } = HarmonyImportDependency;
|
---|
42 |
|
---|
43 | class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
|
---|
44 | /**
|
---|
45 | * @param {string} request request
|
---|
46 | * @param {number} sourceOrder source order
|
---|
47 | * @param {string[]} ids ids
|
---|
48 | * @param {string} name name
|
---|
49 | * @param {Range} range range
|
---|
50 | * @param {TODO} exportPresenceMode export presence mode
|
---|
51 | * @param {ImportAttributes | undefined} attributes import attributes
|
---|
52 | * @param {Range[] | undefined} idRanges ranges for members of ids; the two arrays are right-aligned
|
---|
53 | */
|
---|
54 | constructor(
|
---|
55 | request,
|
---|
56 | sourceOrder,
|
---|
57 | ids,
|
---|
58 | name,
|
---|
59 | range,
|
---|
60 | exportPresenceMode,
|
---|
61 | attributes,
|
---|
62 | idRanges // TODO webpack 6 make this non-optional. It must always be set to properly trim ids.
|
---|
63 | ) {
|
---|
64 | super(request, sourceOrder, attributes);
|
---|
65 | this.ids = ids;
|
---|
66 | this.name = name;
|
---|
67 | this.range = range;
|
---|
68 | this.idRanges = idRanges;
|
---|
69 | this.exportPresenceMode = exportPresenceMode;
|
---|
70 | this.namespaceObjectAsContext = false;
|
---|
71 | this.call = undefined;
|
---|
72 | this.directImport = undefined;
|
---|
73 | this.shorthand = undefined;
|
---|
74 | this.asiSafe = undefined;
|
---|
75 | /** @type {Set<string> | boolean | undefined} */
|
---|
76 | this.usedByExports = undefined;
|
---|
77 | /** @type {Set<DestructuringAssignmentProperty> | undefined} */
|
---|
78 | this.referencedPropertiesInDestructuring = undefined;
|
---|
79 | }
|
---|
80 |
|
---|
81 | // TODO webpack 6 remove
|
---|
82 | get id() {
|
---|
83 | throw new Error("id was renamed to ids and type changed to string[]");
|
---|
84 | }
|
---|
85 |
|
---|
86 | // TODO webpack 6 remove
|
---|
87 | getId() {
|
---|
88 | throw new Error("id was renamed to ids and type changed to string[]");
|
---|
89 | }
|
---|
90 |
|
---|
91 | // TODO webpack 6 remove
|
---|
92 | setId() {
|
---|
93 | throw new Error("id was renamed to ids and type changed to string[]");
|
---|
94 | }
|
---|
95 |
|
---|
96 | get type() {
|
---|
97 | return "harmony import specifier";
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * @param {ModuleGraph} moduleGraph the module graph
|
---|
102 | * @returns {string[]} the imported ids
|
---|
103 | */
|
---|
104 | getIds(moduleGraph) {
|
---|
105 | const meta = moduleGraph.getMetaIfExisting(this);
|
---|
106 | if (meta === undefined) return this.ids;
|
---|
107 | const ids = meta[/** @type {keyof object} */ (idsSymbol)];
|
---|
108 | return ids !== undefined ? ids : this.ids;
|
---|
109 | }
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * @param {ModuleGraph} moduleGraph the module graph
|
---|
113 | * @param {string[]} ids the imported ids
|
---|
114 | * @returns {void}
|
---|
115 | */
|
---|
116 | setIds(moduleGraph, ids) {
|
---|
117 | /** @type {TODO} */
|
---|
118 | (moduleGraph.getMeta(this))[idsSymbol] = ids;
|
---|
119 | }
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * @param {ModuleGraph} moduleGraph module graph
|
---|
123 | * @returns {null | false | GetConditionFn} function to determine if the connection is active
|
---|
124 | */
|
---|
125 | getCondition(moduleGraph) {
|
---|
126 | return getDependencyUsedByExportsCondition(
|
---|
127 | this,
|
---|
128 | this.usedByExports,
|
---|
129 | moduleGraph
|
---|
130 | );
|
---|
131 | }
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * @param {ModuleGraph} moduleGraph the module graph
|
---|
135 | * @returns {ConnectionState} how this dependency connects the module to referencing modules
|
---|
136 | */
|
---|
137 | getModuleEvaluationSideEffectsState(moduleGraph) {
|
---|
138 | return false;
|
---|
139 | }
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Returns list of exports referenced by this dependency
|
---|
143 | * @param {ModuleGraph} moduleGraph module graph
|
---|
144 | * @param {RuntimeSpec} runtime the runtime for which the module is analysed
|
---|
145 | * @returns {(string[] | ReferencedExport)[]} referenced exports
|
---|
146 | */
|
---|
147 | getReferencedExports(moduleGraph, runtime) {
|
---|
148 | let ids = this.getIds(moduleGraph);
|
---|
149 | if (ids.length === 0) return this._getReferencedExportsInDestructuring();
|
---|
150 | let namespaceObjectAsContext = this.namespaceObjectAsContext;
|
---|
151 | if (ids[0] === "default") {
|
---|
152 | const selfModule =
|
---|
153 | /** @type {Module} */
|
---|
154 | (moduleGraph.getParentModule(this));
|
---|
155 | const importedModule =
|
---|
156 | /** @type {Module} */
|
---|
157 | (moduleGraph.getModule(this));
|
---|
158 | switch (
|
---|
159 | importedModule.getExportsType(
|
---|
160 | moduleGraph,
|
---|
161 | /** @type {BuildMeta} */
|
---|
162 | (selfModule.buildMeta).strictHarmonyModule
|
---|
163 | )
|
---|
164 | ) {
|
---|
165 | case "default-only":
|
---|
166 | case "default-with-named":
|
---|
167 | if (ids.length === 1)
|
---|
168 | return this._getReferencedExportsInDestructuring();
|
---|
169 | ids = ids.slice(1);
|
---|
170 | namespaceObjectAsContext = true;
|
---|
171 | break;
|
---|
172 | case "dynamic":
|
---|
173 | return Dependency.EXPORTS_OBJECT_REFERENCED;
|
---|
174 | }
|
---|
175 | }
|
---|
176 |
|
---|
177 | if (
|
---|
178 | this.call &&
|
---|
179 | !this.directImport &&
|
---|
180 | (namespaceObjectAsContext || ids.length > 1)
|
---|
181 | ) {
|
---|
182 | if (ids.length === 1) return Dependency.EXPORTS_OBJECT_REFERENCED;
|
---|
183 | ids = ids.slice(0, -1);
|
---|
184 | }
|
---|
185 |
|
---|
186 | return this._getReferencedExportsInDestructuring(ids);
|
---|
187 | }
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * @param {string[]=} ids ids
|
---|
191 | * @returns {string[][]} referenced exports
|
---|
192 | */
|
---|
193 | _getReferencedExportsInDestructuring(ids) {
|
---|
194 | if (this.referencedPropertiesInDestructuring) {
|
---|
195 | /** @type {string[][]} */
|
---|
196 | const refs = [];
|
---|
197 | for (const { id } of this.referencedPropertiesInDestructuring) {
|
---|
198 | refs.push(ids ? ids.concat([id]) : [id]);
|
---|
199 | }
|
---|
200 | return refs;
|
---|
201 | }
|
---|
202 | return ids ? [ids] : Dependency.EXPORTS_OBJECT_REFERENCED;
|
---|
203 | }
|
---|
204 |
|
---|
205 | /**
|
---|
206 | * @param {ModuleGraph} moduleGraph module graph
|
---|
207 | * @returns {number} effective mode
|
---|
208 | */
|
---|
209 | _getEffectiveExportPresenceLevel(moduleGraph) {
|
---|
210 | if (this.exportPresenceMode !== ExportPresenceModes.AUTO)
|
---|
211 | return this.exportPresenceMode;
|
---|
212 | const buildMeta =
|
---|
213 | /** @type {BuildMeta} */
|
---|
214 | (
|
---|
215 | /** @type {Module} */
|
---|
216 | (moduleGraph.getParentModule(this)).buildMeta
|
---|
217 | );
|
---|
218 | return buildMeta.strictHarmonyModule
|
---|
219 | ? ExportPresenceModes.ERROR
|
---|
220 | : ExportPresenceModes.WARN;
|
---|
221 | }
|
---|
222 |
|
---|
223 | /**
|
---|
224 | * Returns warnings
|
---|
225 | * @param {ModuleGraph} moduleGraph module graph
|
---|
226 | * @returns {WebpackError[] | null | undefined} warnings
|
---|
227 | */
|
---|
228 | getWarnings(moduleGraph) {
|
---|
229 | const exportsPresence = this._getEffectiveExportPresenceLevel(moduleGraph);
|
---|
230 | if (exportsPresence === ExportPresenceModes.WARN) {
|
---|
231 | return this._getErrors(moduleGraph);
|
---|
232 | }
|
---|
233 | return null;
|
---|
234 | }
|
---|
235 |
|
---|
236 | /**
|
---|
237 | * Returns errors
|
---|
238 | * @param {ModuleGraph} moduleGraph module graph
|
---|
239 | * @returns {WebpackError[] | null | undefined} errors
|
---|
240 | */
|
---|
241 | getErrors(moduleGraph) {
|
---|
242 | const exportsPresence = this._getEffectiveExportPresenceLevel(moduleGraph);
|
---|
243 | if (exportsPresence === ExportPresenceModes.ERROR) {
|
---|
244 | return this._getErrors(moduleGraph);
|
---|
245 | }
|
---|
246 | return null;
|
---|
247 | }
|
---|
248 |
|
---|
249 | /**
|
---|
250 | * @param {ModuleGraph} moduleGraph module graph
|
---|
251 | * @returns {WebpackError[] | undefined} errors
|
---|
252 | */
|
---|
253 | _getErrors(moduleGraph) {
|
---|
254 | const ids = this.getIds(moduleGraph);
|
---|
255 | return this.getLinkingErrors(
|
---|
256 | moduleGraph,
|
---|
257 | ids,
|
---|
258 | `(imported as '${this.name}')`
|
---|
259 | );
|
---|
260 | }
|
---|
261 |
|
---|
262 | /**
|
---|
263 | * implement this method to allow the occurrence order plugin to count correctly
|
---|
264 | * @returns {number} count how often the id is used in this dependency
|
---|
265 | */
|
---|
266 | getNumberOfIdOccurrences() {
|
---|
267 | return 0;
|
---|
268 | }
|
---|
269 |
|
---|
270 | /**
|
---|
271 | * @param {ObjectSerializerContext} context context
|
---|
272 | */
|
---|
273 | serialize(context) {
|
---|
274 | const { write } = context;
|
---|
275 | write(this.ids);
|
---|
276 | write(this.name);
|
---|
277 | write(this.range);
|
---|
278 | write(this.idRanges);
|
---|
279 | write(this.exportPresenceMode);
|
---|
280 | write(this.namespaceObjectAsContext);
|
---|
281 | write(this.call);
|
---|
282 | write(this.directImport);
|
---|
283 | write(this.shorthand);
|
---|
284 | write(this.asiSafe);
|
---|
285 | write(this.usedByExports);
|
---|
286 | write(this.referencedPropertiesInDestructuring);
|
---|
287 | super.serialize(context);
|
---|
288 | }
|
---|
289 |
|
---|
290 | /**
|
---|
291 | * @param {ObjectDeserializerContext} context context
|
---|
292 | */
|
---|
293 | deserialize(context) {
|
---|
294 | const { read } = context;
|
---|
295 | this.ids = read();
|
---|
296 | this.name = read();
|
---|
297 | this.range = read();
|
---|
298 | this.idRanges = read();
|
---|
299 | this.exportPresenceMode = read();
|
---|
300 | this.namespaceObjectAsContext = read();
|
---|
301 | this.call = read();
|
---|
302 | this.directImport = read();
|
---|
303 | this.shorthand = read();
|
---|
304 | this.asiSafe = read();
|
---|
305 | this.usedByExports = read();
|
---|
306 | this.referencedPropertiesInDestructuring = read();
|
---|
307 | super.deserialize(context);
|
---|
308 | }
|
---|
309 | }
|
---|
310 |
|
---|
311 | makeSerializable(
|
---|
312 | HarmonyImportSpecifierDependency,
|
---|
313 | "webpack/lib/dependencies/HarmonyImportSpecifierDependency"
|
---|
314 | );
|
---|
315 |
|
---|
316 | HarmonyImportSpecifierDependency.Template = class HarmonyImportSpecifierDependencyTemplate extends (
|
---|
317 | HarmonyImportDependency.Template
|
---|
318 | ) {
|
---|
319 | /**
|
---|
320 | * @param {Dependency} dependency the dependency for which the template should be applied
|
---|
321 | * @param {ReplaceSource} source the current replace source which can be modified
|
---|
322 | * @param {DependencyTemplateContext} templateContext the context object
|
---|
323 | * @returns {void}
|
---|
324 | */
|
---|
325 | apply(dependency, source, templateContext) {
|
---|
326 | const dep = /** @type {HarmonyImportSpecifierDependency} */ (dependency);
|
---|
327 | const { moduleGraph, runtime } = templateContext;
|
---|
328 | const connection = moduleGraph.getConnection(dep);
|
---|
329 | // Skip rendering depending when dependency is conditional
|
---|
330 | if (connection && !connection.isTargetActive(runtime)) return;
|
---|
331 |
|
---|
332 | const ids = dep.getIds(moduleGraph);
|
---|
333 | const {
|
---|
334 | trimmedRange: [trimmedRangeStart, trimmedRangeEnd],
|
---|
335 | trimmedIds
|
---|
336 | } = getTrimmedIdsAndRange(ids, dep.range, dep.idRanges, moduleGraph, dep);
|
---|
337 |
|
---|
338 | const exportExpr = this._getCodeForIds(
|
---|
339 | dep,
|
---|
340 | source,
|
---|
341 | templateContext,
|
---|
342 | trimmedIds
|
---|
343 | );
|
---|
344 | if (dep.shorthand) {
|
---|
345 | source.insert(trimmedRangeEnd, `: ${exportExpr}`);
|
---|
346 | } else {
|
---|
347 | source.replace(trimmedRangeStart, trimmedRangeEnd - 1, exportExpr);
|
---|
348 | }
|
---|
349 |
|
---|
350 | if (dep.referencedPropertiesInDestructuring) {
|
---|
351 | let prefixedIds = ids;
|
---|
352 |
|
---|
353 | if (ids[0] === "default") {
|
---|
354 | const selfModule =
|
---|
355 | /** @type {Module} */
|
---|
356 | (moduleGraph.getParentModule(dep));
|
---|
357 | const importedModule =
|
---|
358 | /** @type {Module} */
|
---|
359 | (moduleGraph.getModule(dep));
|
---|
360 | const exportsType = importedModule.getExportsType(
|
---|
361 | moduleGraph,
|
---|
362 | /** @type {BuildMeta} */
|
---|
363 | (selfModule.buildMeta).strictHarmonyModule
|
---|
364 | );
|
---|
365 | if (
|
---|
366 | (exportsType === "default-only" ||
|
---|
367 | exportsType === "default-with-named") &&
|
---|
368 | ids.length >= 1
|
---|
369 | ) {
|
---|
370 | prefixedIds = ids.slice(1);
|
---|
371 | }
|
---|
372 | }
|
---|
373 |
|
---|
374 | for (const {
|
---|
375 | id,
|
---|
376 | shorthand,
|
---|
377 | range
|
---|
378 | } of dep.referencedPropertiesInDestructuring) {
|
---|
379 | const concatedIds = prefixedIds.concat([id]);
|
---|
380 | const module = /** @type {Module} */ (moduleGraph.getModule(dep));
|
---|
381 | const used = moduleGraph
|
---|
382 | .getExportsInfo(module)
|
---|
383 | .getUsedName(concatedIds, runtime);
|
---|
384 | if (!used) return;
|
---|
385 | const newName = used[used.length - 1];
|
---|
386 | const name = concatedIds[concatedIds.length - 1];
|
---|
387 | if (newName === name) continue;
|
---|
388 |
|
---|
389 | const comment = `${Template.toNormalComment(name)} `;
|
---|
390 | const key = comment + JSON.stringify(newName);
|
---|
391 | source.replace(
|
---|
392 | /** @type {Range} */
|
---|
393 | (range)[0],
|
---|
394 | /** @type {Range} */
|
---|
395 | (range)[1] - 1,
|
---|
396 | shorthand ? `${key}: ${name}` : `${key}`
|
---|
397 | );
|
---|
398 | }
|
---|
399 | }
|
---|
400 | }
|
---|
401 |
|
---|
402 | /**
|
---|
403 | * @param {HarmonyImportSpecifierDependency} dep dependency
|
---|
404 | * @param {ReplaceSource} source source
|
---|
405 | * @param {DependencyTemplateContext} templateContext context
|
---|
406 | * @param {string[]} ids ids
|
---|
407 | * @returns {string} generated code
|
---|
408 | */
|
---|
409 | _getCodeForIds(dep, source, templateContext, ids) {
|
---|
410 | const { moduleGraph, module, runtime, concatenationScope } =
|
---|
411 | templateContext;
|
---|
412 | const connection = moduleGraph.getConnection(dep);
|
---|
413 | let exportExpr;
|
---|
414 | if (
|
---|
415 | connection &&
|
---|
416 | concatenationScope &&
|
---|
417 | concatenationScope.isModuleInScope(connection.module)
|
---|
418 | ) {
|
---|
419 | if (ids.length === 0) {
|
---|
420 | exportExpr = concatenationScope.createModuleReference(
|
---|
421 | connection.module,
|
---|
422 | {
|
---|
423 | asiSafe: dep.asiSafe
|
---|
424 | }
|
---|
425 | );
|
---|
426 | } else if (dep.namespaceObjectAsContext && ids.length === 1) {
|
---|
427 | exportExpr =
|
---|
428 | concatenationScope.createModuleReference(connection.module, {
|
---|
429 | asiSafe: dep.asiSafe
|
---|
430 | }) + propertyAccess(ids);
|
---|
431 | } else {
|
---|
432 | exportExpr = concatenationScope.createModuleReference(
|
---|
433 | connection.module,
|
---|
434 | {
|
---|
435 | ids,
|
---|
436 | call: dep.call,
|
---|
437 | directImport: dep.directImport,
|
---|
438 | asiSafe: dep.asiSafe
|
---|
439 | }
|
---|
440 | );
|
---|
441 | }
|
---|
442 | } else {
|
---|
443 | super.apply(dep, source, templateContext);
|
---|
444 |
|
---|
445 | const { runtimeTemplate, initFragments, runtimeRequirements } =
|
---|
446 | templateContext;
|
---|
447 |
|
---|
448 | exportExpr = runtimeTemplate.exportFromImport({
|
---|
449 | moduleGraph,
|
---|
450 | module: /** @type {Module} */ (moduleGraph.getModule(dep)),
|
---|
451 | request: dep.request,
|
---|
452 | exportName: ids,
|
---|
453 | originModule: module,
|
---|
454 | asiSafe: dep.shorthand ? true : dep.asiSafe,
|
---|
455 | isCall: dep.call,
|
---|
456 | callContext: !dep.directImport,
|
---|
457 | defaultInterop: true,
|
---|
458 | importVar: dep.getImportVar(moduleGraph),
|
---|
459 | initFragments,
|
---|
460 | runtime,
|
---|
461 | runtimeRequirements
|
---|
462 | });
|
---|
463 | }
|
---|
464 | return exportExpr;
|
---|
465 | }
|
---|
466 | };
|
---|
467 |
|
---|
468 | module.exports = HarmonyImportSpecifierDependency;
|
---|