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 makeSerializable = require("../util/makeSerializable");
|
---|
10 | const ModuleDependency = require("./ModuleDependency");
|
---|
11 |
|
---|
12 | /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
---|
13 | /** @typedef {import("../AsyncDependenciesBlock")} AsyncDependenciesBlock */
|
---|
14 | /** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */
|
---|
15 | /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
---|
16 | /** @typedef {import("../Module")} Module */
|
---|
17 | /** @typedef {import("../Module").BuildMeta} BuildMeta */
|
---|
18 | /** @typedef {import("../ModuleGraph")} ModuleGraph */
|
---|
19 | /** @typedef {import("../javascript/JavascriptParser").ImportAttributes} ImportAttributes */
|
---|
20 | /** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
---|
21 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
---|
22 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
---|
23 | /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
|
---|
24 |
|
---|
25 | class ImportDependency extends ModuleDependency {
|
---|
26 | /**
|
---|
27 | * @param {string} request the request
|
---|
28 | * @param {Range} range expression range
|
---|
29 | * @param {(string[][] | null)=} referencedExports list of referenced exports
|
---|
30 | * @param {ImportAttributes=} attributes import attributes
|
---|
31 | */
|
---|
32 | constructor(request, range, referencedExports, attributes) {
|
---|
33 | super(request);
|
---|
34 | this.range = range;
|
---|
35 | this.referencedExports = referencedExports;
|
---|
36 | this.assertions = attributes;
|
---|
37 | }
|
---|
38 |
|
---|
39 | get type() {
|
---|
40 | return "import()";
|
---|
41 | }
|
---|
42 |
|
---|
43 | get category() {
|
---|
44 | return "esm";
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Returns list of exports referenced by this dependency
|
---|
49 | * @param {ModuleGraph} moduleGraph module graph
|
---|
50 | * @param {RuntimeSpec} runtime the runtime for which the module is analysed
|
---|
51 | * @returns {(string[] | ReferencedExport)[]} referenced exports
|
---|
52 | */
|
---|
53 | getReferencedExports(moduleGraph, runtime) {
|
---|
54 | if (!this.referencedExports) return Dependency.EXPORTS_OBJECT_REFERENCED;
|
---|
55 | const refs = [];
|
---|
56 | for (const referencedExport of this.referencedExports) {
|
---|
57 | if (referencedExport[0] === "default") {
|
---|
58 | const selfModule =
|
---|
59 | /** @type {Module} */
|
---|
60 | (moduleGraph.getParentModule(this));
|
---|
61 | const importedModule =
|
---|
62 | /** @type {Module} */
|
---|
63 | (moduleGraph.getModule(this));
|
---|
64 | const exportsType = importedModule.getExportsType(
|
---|
65 | moduleGraph,
|
---|
66 | /** @type {BuildMeta} */
|
---|
67 | (selfModule.buildMeta).strictHarmonyModule
|
---|
68 | );
|
---|
69 | if (
|
---|
70 | exportsType === "default-only" ||
|
---|
71 | exportsType === "default-with-named"
|
---|
72 | ) {
|
---|
73 | return Dependency.EXPORTS_OBJECT_REFERENCED;
|
---|
74 | }
|
---|
75 | }
|
---|
76 | refs.push({
|
---|
77 | name: referencedExport,
|
---|
78 | canMangle: false
|
---|
79 | });
|
---|
80 | }
|
---|
81 | return refs;
|
---|
82 | }
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * @param {ObjectSerializerContext} context context
|
---|
86 | */
|
---|
87 | serialize(context) {
|
---|
88 | context.write(this.range);
|
---|
89 | context.write(this.referencedExports);
|
---|
90 | context.write(this.assertions);
|
---|
91 | super.serialize(context);
|
---|
92 | }
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * @param {ObjectDeserializerContext} context context
|
---|
96 | */
|
---|
97 | deserialize(context) {
|
---|
98 | this.range = context.read();
|
---|
99 | this.referencedExports = context.read();
|
---|
100 | this.assertions = context.read();
|
---|
101 | super.deserialize(context);
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | makeSerializable(ImportDependency, "webpack/lib/dependencies/ImportDependency");
|
---|
106 |
|
---|
107 | ImportDependency.Template = class ImportDependencyTemplate extends (
|
---|
108 | ModuleDependency.Template
|
---|
109 | ) {
|
---|
110 | /**
|
---|
111 | * @param {Dependency} dependency the dependency for which the template should be applied
|
---|
112 | * @param {ReplaceSource} source the current replace source which can be modified
|
---|
113 | * @param {DependencyTemplateContext} templateContext the context object
|
---|
114 | * @returns {void}
|
---|
115 | */
|
---|
116 | apply(
|
---|
117 | dependency,
|
---|
118 | source,
|
---|
119 | { runtimeTemplate, module, moduleGraph, chunkGraph, runtimeRequirements }
|
---|
120 | ) {
|
---|
121 | const dep = /** @type {ImportDependency} */ (dependency);
|
---|
122 | const block = /** @type {AsyncDependenciesBlock} */ (
|
---|
123 | moduleGraph.getParentBlock(dep)
|
---|
124 | );
|
---|
125 | const content = runtimeTemplate.moduleNamespacePromise({
|
---|
126 | chunkGraph,
|
---|
127 | block,
|
---|
128 | module: /** @type {Module} */ (moduleGraph.getModule(dep)),
|
---|
129 | request: dep.request,
|
---|
130 | strict: /** @type {BuildMeta} */ (module.buildMeta).strictHarmonyModule,
|
---|
131 | message: "import()",
|
---|
132 | runtimeRequirements
|
---|
133 | });
|
---|
134 |
|
---|
135 | source.replace(dep.range[0], dep.range[1] - 1, content);
|
---|
136 | }
|
---|
137 | };
|
---|
138 |
|
---|
139 | module.exports = ImportDependency;
|
---|