source: frontend/node_modules/webpack/lib/dependencies/HarmonyAcceptDependency.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: 7.6 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 Template = require("../Template");
9const AwaitDependenciesInitFragment = require("../async-modules/AwaitDependenciesInitFragment");
10const makeSerializable = require("../util/makeSerializable");
11const HarmonyImportDependency = require("./HarmonyImportDependency");
12const { ImportPhaseUtils } = require("./ImportPhase");
13const NullDependency = require("./NullDependency");
14
15/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
16/** @typedef {import("../Dependency")} Dependency */
17/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
18/** @typedef {import("../javascript/JavascriptParser").Range} Range */
19/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
20/** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
21/** @typedef {import("./HarmonyAcceptImportDependency")} HarmonyAcceptImportDependency */
22/** @typedef {import("../Module")} Module */
23/** @typedef {import("../Module").ModuleId} ModuleId */
24
25class HarmonyAcceptDependency extends NullDependency {
26 /**
27 * Creates an instance of HarmonyAcceptDependency.
28 * @param {Range} range expression range
29 * @param {HarmonyAcceptImportDependency[]} dependencies import dependencies
30 * @param {boolean} hasCallback true, if the range wraps an existing callback
31 */
32 constructor(range, dependencies, hasCallback) {
33 super();
34 this.range = range;
35 this.dependencies = dependencies;
36 this.hasCallback = hasCallback;
37 }
38
39 get type() {
40 return "accepted harmony modules";
41 }
42
43 /**
44 * Serializes this instance into the provided serializer context.
45 * @param {ObjectSerializerContext} context context
46 */
47 serialize(context) {
48 const { write } = context;
49 write(this.range);
50 write(this.dependencies);
51 write(this.hasCallback);
52 super.serialize(context);
53 }
54
55 /**
56 * Restores this instance from the provided deserializer context.
57 * @param {ObjectDeserializerContext} context context
58 */
59 deserialize(context) {
60 const { read } = context;
61 this.range = read();
62 this.dependencies = read();
63 this.hasCallback = read();
64 super.deserialize(context);
65 }
66}
67
68makeSerializable(
69 HarmonyAcceptDependency,
70 "webpack/lib/dependencies/HarmonyAcceptDependency"
71);
72
73HarmonyAcceptDependency.Template = class HarmonyAcceptDependencyTemplate extends (
74 NullDependency.Template
75) {
76 /**
77 * Applies the plugin by registering its hooks on the compiler.
78 * @param {Dependency} dependency the dependency for which the template should be applied
79 * @param {ReplaceSource} source the current replace source which can be modified
80 * @param {DependencyTemplateContext} templateContext the context object
81 * @returns {void}
82 */
83 apply(dependency, source, templateContext) {
84 const dep = /** @type {HarmonyAcceptDependency} */ (dependency);
85 const {
86 module,
87 runtime,
88 runtimeRequirements,
89 runtimeTemplate,
90 moduleGraph,
91 chunkGraph
92 } = templateContext;
93
94 /**
95 * Gets dependency module id.
96 * @param {Dependency} dependency the dependency to get module id for
97 * @returns {ModuleId | null} the module id or null if not found
98 */
99 const getDependencyModuleId = (dependency) =>
100 chunkGraph.getModuleId(
101 /** @type {Module} */ (moduleGraph.getModule(dependency))
102 );
103
104 /**
105 * Checks whether this harmony accept dependency is related harmony import dependency.
106 * @param {Dependency} a the first dependency
107 * @param {Dependency} b the second dependency
108 * @returns {boolean} true if the dependencies are related
109 */
110 const isRelatedHarmonyImportDependency = (a, b) =>
111 a !== b &&
112 b instanceof HarmonyImportDependency &&
113 getDependencyModuleId(a) === getDependencyModuleId(b);
114
115 /**
116 * HarmonyAcceptImportDependency lacks a lot of information, such as the defer property.
117 * One HarmonyAcceptImportDependency may need to generate multiple ImportStatements.
118 * Therefore, we find its original HarmonyImportDependency for code generation.
119 * @param {HarmonyAcceptImportDependency} dependency the dependency to get harmony import dependencies for
120 * @returns {HarmonyImportDependency[]} array of related harmony import dependencies
121 */
122 const getHarmonyImportDependencies = (dependency) => {
123 /** @type {HarmonyImportDependency[]} */
124 const result = [];
125 /** @type {HarmonyImportDependency | null} */
126 let deferDependency = null;
127 /** @type {HarmonyImportDependency | null} */
128 let noDeferredDependency = null;
129
130 for (const d of module.dependencies) {
131 if (deferDependency && noDeferredDependency) break;
132 if (isRelatedHarmonyImportDependency(dependency, d)) {
133 if (
134 ImportPhaseUtils.isDefer(
135 /** @type {HarmonyImportDependency} */ (d).phase
136 )
137 ) {
138 deferDependency = /** @type {HarmonyImportDependency} */ (d);
139 } else {
140 noDeferredDependency = /** @type {HarmonyImportDependency} */ (d);
141 }
142 }
143 }
144 if (deferDependency) result.push(deferDependency);
145 if (noDeferredDependency) result.push(noDeferredDependency);
146 if (result.length === 0) {
147 // fallback to the original dependency
148 result.push(dependency);
149 }
150 return result;
151 };
152
153 /** @type {HarmonyImportDependency[]} */
154 const syncDeps = [];
155
156 /** @type {HarmonyAcceptImportDependency[]} */
157 const asyncDeps = [];
158
159 for (const dependency of dep.dependencies) {
160 const connection = moduleGraph.getConnection(dependency);
161
162 if (connection && moduleGraph.isAsync(connection.module)) {
163 asyncDeps.push(dependency);
164 } else {
165 syncDeps.push(...getHarmonyImportDependencies(dependency));
166 }
167 }
168
169 let content = syncDeps
170 .map((dependency) => {
171 const referencedModule = moduleGraph.getModule(dependency);
172 return {
173 dependency,
174 runtimeCondition: referencedModule
175 ? HarmonyImportDependency.Template.getImportEmittedRuntime(
176 module,
177 referencedModule
178 )
179 : false
180 };
181 })
182 .filter(({ runtimeCondition }) => runtimeCondition !== false)
183 .map(({ dependency, runtimeCondition }) => {
184 const condition = runtimeTemplate.runtimeConditionExpression({
185 chunkGraph,
186 runtime,
187 runtimeCondition,
188 runtimeRequirements
189 });
190 const s = dependency.getImportStatement(true, templateContext);
191 const code = s[0] + s[1];
192 if (condition !== "true") {
193 return `if (${condition}) {\n${Template.indent(code)}\n}\n`;
194 }
195 return code;
196 })
197 .join("");
198
199 const promises = new Map(
200 asyncDeps.map((dependency) => [
201 dependency.getImportVar(moduleGraph),
202 dependency.getModuleExports(templateContext)
203 ])
204 );
205
206 let optAsync = "";
207 if (promises.size !== 0) {
208 optAsync = "async ";
209 content += new AwaitDependenciesInitFragment(promises).getContent({
210 ...templateContext,
211 type: "javascript"
212 });
213 }
214
215 if (dep.hasCallback) {
216 if (runtimeTemplate.supportsArrowFunction()) {
217 source.insert(
218 dep.range[0],
219 `${optAsync}__WEBPACK_OUTDATED_DEPENDENCIES__ => { ${content} return (`
220 );
221 source.insert(dep.range[1], ")(__WEBPACK_OUTDATED_DEPENDENCIES__); }");
222 } else {
223 source.insert(
224 dep.range[0],
225 `${optAsync}function(__WEBPACK_OUTDATED_DEPENDENCIES__) { ${content} return (`
226 );
227 source.insert(
228 dep.range[1],
229 ")(__WEBPACK_OUTDATED_DEPENDENCIES__); }.bind(this)"
230 );
231 }
232 return;
233 }
234
235 const arrow = runtimeTemplate.supportsArrowFunction();
236 source.insert(
237 dep.range[1] - 0.5,
238 `, ${arrow ? `${optAsync}() =>` : `${optAsync}function()`} { ${content} }`
239 );
240 }
241};
242
243module.exports = HarmonyAcceptDependency;
Note: See TracBrowser for help on using the repository browser.