source: frontend/node_modules/webpack/lib/dependencies/ImportDependency.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 5.7 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const Dependency = require("../Dependency");
9const makeSerializable = require("../util/makeSerializable");
10const { ImportPhaseUtils } = require("./ImportPhase");
11const ModuleDependency = require("./ModuleDependency");
12
13/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
14/** @typedef {import("../AsyncDependenciesBlock")} AsyncDependenciesBlock */
15/** @typedef {import("../Dependency").RawReferencedExports} RawReferencedExports */
16/** @typedef {import("../Dependency").ReferencedExports} ReferencedExports */
17/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
18/** @typedef {import("../Module")} Module */
19/** @typedef {import("../Module").BuildMeta} BuildMeta */
20/** @typedef {import("../ModuleGraph")} ModuleGraph */
21/** @typedef {import("../javascript/JavascriptParser").ImportAttributes} ImportAttributes */
22/** @typedef {import("../javascript/JavascriptParser").Range} Range */
23/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
24/** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
25/** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
26/** @typedef {import("./ImportPhase").ImportPhaseType} ImportPhaseType */
27
28class ImportDependency extends ModuleDependency {
29 /**
30 * Creates an instance of ImportDependency.
31 * @param {string} request the request
32 * @param {Range} range expression range
33 * @param {RawReferencedExports | null} referencedExports list of referenced exports
34 * @param {ImportPhaseType} phase import phase
35 * @param {ImportAttributes=} attributes import attributes
36 */
37 constructor(request, range, referencedExports, phase, attributes) {
38 super(request);
39 this.range = range;
40 this.referencedExports = referencedExports;
41 this.phase = phase;
42 this.attributes = attributes;
43 }
44
45 get type() {
46 return "import()";
47 }
48
49 get category() {
50 return "esm";
51 }
52
53 /**
54 * Returns an identifier to merge equal requests.
55 * @returns {string | null} an identifier to merge equal requests
56 */
57 getResourceIdentifier() {
58 let str = super.getResourceIdentifier();
59 // We specifically use this check to avoid writing the default (`evaluation` or `0`) value and save memory
60 if (this.phase) {
61 str += `|phase${ImportPhaseUtils.stringify(this.phase)}`;
62 }
63 if (this.attributes) {
64 str += `|attributes${JSON.stringify(this.attributes)}`;
65 }
66 return str;
67 }
68
69 /**
70 * Returns list of exports referenced by this dependency
71 * @param {ModuleGraph} moduleGraph module graph
72 * @param {RuntimeSpec} runtime the runtime for which the module is analysed
73 * @returns {ReferencedExports} referenced exports
74 */
75 getReferencedExports(moduleGraph, runtime) {
76 if (!this.referencedExports) return Dependency.EXPORTS_OBJECT_REFERENCED;
77 /** @type {ReferencedExports} */
78 const refs = [];
79 for (const referencedExport of this.referencedExports) {
80 if (referencedExport[0] === "default") {
81 const selfModule =
82 /** @type {Module} */
83 (moduleGraph.getParentModule(this));
84 const importedModule =
85 /** @type {Module} */
86 (moduleGraph.getModule(this));
87 const exportsType = importedModule.getExportsType(
88 moduleGraph,
89 /** @type {BuildMeta} */
90 (selfModule.buildMeta).strictHarmonyModule
91 );
92 if (
93 exportsType === "default-only" ||
94 exportsType === "default-with-named"
95 ) {
96 return Dependency.EXPORTS_OBJECT_REFERENCED;
97 }
98 }
99 refs.push({
100 name: referencedExport,
101 canMangle: false
102 });
103 }
104 return refs;
105 }
106
107 /**
108 * Serializes this instance into the provided serializer context.
109 * @param {ObjectSerializerContext} context context
110 */
111 serialize(context) {
112 context.write(this.range);
113 context.write(this.referencedExports);
114 context.write(this.phase);
115 context.write(this.attributes);
116 super.serialize(context);
117 }
118
119 /**
120 * Restores this instance from the provided deserializer context.
121 * @param {ObjectDeserializerContext} context context
122 */
123 deserialize(context) {
124 this.range = context.read();
125 this.referencedExports = context.read();
126 this.phase = context.read();
127 this.attributes = context.read();
128 super.deserialize(context);
129 }
130}
131
132makeSerializable(ImportDependency, "webpack/lib/dependencies/ImportDependency");
133
134ImportDependency.Template = class ImportDependencyTemplate extends (
135 ModuleDependency.Template
136) {
137 /**
138 * Applies the plugin by registering its hooks on the compiler.
139 * @param {Dependency} dependency the dependency for which the template should be applied
140 * @param {ReplaceSource} source the current replace source which can be modified
141 * @param {DependencyTemplateContext} templateContext the context object
142 * @returns {void}
143 */
144 apply(
145 dependency,
146 source,
147 { runtimeTemplate, module, moduleGraph, chunkGraph, runtimeRequirements }
148 ) {
149 const dep = /** @type {ImportDependency} */ (dependency);
150 const block = /** @type {AsyncDependenciesBlock} */ (
151 moduleGraph.getParentBlock(dep)
152 );
153 let content = runtimeTemplate.moduleNamespacePromise({
154 chunkGraph,
155 block,
156 module: /** @type {Module} */ (moduleGraph.getModule(dep)),
157 request: dep.request,
158 strict: /** @type {BuildMeta} */ (module.buildMeta).strictHarmonyModule,
159 dependency: dep,
160 message: "import()",
161 runtimeRequirements
162 });
163
164 // For source phase imports, unwrap the default export
165 // import.source() should return the source directly, not a namespace
166 if (ImportPhaseUtils.isSource(dep.phase)) {
167 content = `${content}.then(${runtimeTemplate.returningFunction(
168 'm["default"]',
169 "m"
170 )})`;
171 }
172
173 source.replace(dep.range[0], dep.range[1] - 1, content);
174 }
175};
176
177module.exports = ImportDependency;
Note: See TracBrowser for help on using the repository browser.