source: trip-planner-front/node_modules/webpack/lib/dependencies/CommonJsExportRequireDependency.js@ 188ee53

Last change on this file since 188ee53 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 9.8 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 { UsageState } = require("../ExportsInfo");
10const Template = require("../Template");
11const { equals } = require("../util/ArrayHelpers");
12const makeSerializable = require("../util/makeSerializable");
13const propertyAccess = require("../util/propertyAccess");
14const { handleDependencyBase } = require("./CommonJsDependencyHelpers");
15const ModuleDependency = require("./ModuleDependency");
16const processExportInfo = require("./processExportInfo");
17
18/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
19/** @typedef {import("../Dependency").ExportsSpec} ExportsSpec */
20/** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */
21/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
22/** @typedef {import("../Module")} Module */
23/** @typedef {import("../ModuleGraph")} ModuleGraph */
24/** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
25
26const idsSymbol = Symbol("CommonJsExportRequireDependency.ids");
27
28const EMPTY_OBJECT = {};
29
30class CommonJsExportRequireDependency extends ModuleDependency {
31 constructor(range, valueRange, base, names, request, ids, resultUsed) {
32 super(request);
33 this.range = range;
34 this.valueRange = valueRange;
35 this.base = base;
36 this.names = names;
37 this.ids = ids;
38 this.resultUsed = resultUsed;
39 this.asiSafe = undefined;
40 }
41
42 get type() {
43 return "cjs export require";
44 }
45
46 /**
47 * @param {ModuleGraph} moduleGraph the module graph
48 * @returns {string[]} the imported id
49 */
50 getIds(moduleGraph) {
51 return moduleGraph.getMeta(this)[idsSymbol] || this.ids;
52 }
53
54 /**
55 * @param {ModuleGraph} moduleGraph the module graph
56 * @param {string[]} ids the imported ids
57 * @returns {void}
58 */
59 setIds(moduleGraph, ids) {
60 moduleGraph.getMeta(this)[idsSymbol] = ids;
61 }
62
63 /**
64 * Returns list of exports referenced by this dependency
65 * @param {ModuleGraph} moduleGraph module graph
66 * @param {RuntimeSpec} runtime the runtime for which the module is analysed
67 * @returns {(string[] | ReferencedExport)[]} referenced exports
68 */
69 getReferencedExports(moduleGraph, runtime) {
70 const ids = this.getIds(moduleGraph);
71 const getFullResult = () => {
72 if (ids.length === 0) {
73 return Dependency.EXPORTS_OBJECT_REFERENCED;
74 } else {
75 return [
76 {
77 name: ids,
78 canMangle: false
79 }
80 ];
81 }
82 };
83 if (this.resultUsed) return getFullResult();
84 let exportsInfo = moduleGraph.getExportsInfo(
85 moduleGraph.getParentModule(this)
86 );
87 for (const name of this.names) {
88 const exportInfo = exportsInfo.getReadOnlyExportInfo(name);
89 const used = exportInfo.getUsed(runtime);
90 if (used === UsageState.Unused) return Dependency.NO_EXPORTS_REFERENCED;
91 if (used !== UsageState.OnlyPropertiesUsed) return getFullResult();
92 exportsInfo = exportInfo.exportsInfo;
93 if (!exportsInfo) return getFullResult();
94 }
95 if (exportsInfo.otherExportsInfo.getUsed(runtime) !== UsageState.Unused) {
96 return getFullResult();
97 }
98 /** @type {string[][]} */
99 const referencedExports = [];
100 for (const exportInfo of exportsInfo.orderedExports) {
101 processExportInfo(
102 runtime,
103 referencedExports,
104 ids.concat(exportInfo.name),
105 exportInfo,
106 false
107 );
108 }
109 return referencedExports.map(name => ({
110 name,
111 canMangle: false
112 }));
113 }
114
115 /**
116 * Returns the exported names
117 * @param {ModuleGraph} moduleGraph module graph
118 * @returns {ExportsSpec | undefined} export names
119 */
120 getExports(moduleGraph) {
121 const ids = this.getIds(moduleGraph);
122 if (this.names.length === 1) {
123 const name = this.names[0];
124 const from = moduleGraph.getConnection(this);
125 if (!from) return;
126 return {
127 exports: [
128 {
129 name,
130 from,
131 export: ids.length === 0 ? null : ids,
132 // we can't mangle names that are in an empty object
133 // because one could access the prototype property
134 // when export isn't set yet
135 canMangle: !(name in EMPTY_OBJECT) && false
136 }
137 ],
138 dependencies: [from.module]
139 };
140 } else if (this.names.length > 0) {
141 const name = this.names[0];
142 return {
143 exports: [
144 {
145 name,
146 // we can't mangle names that are in an empty object
147 // because one could access the prototype property
148 // when export isn't set yet
149 canMangle: !(name in EMPTY_OBJECT) && false
150 }
151 ],
152 dependencies: undefined
153 };
154 } else {
155 const from = moduleGraph.getConnection(this);
156 if (!from) return;
157 const reexportInfo = this.getStarReexports(
158 moduleGraph,
159 undefined,
160 from.module
161 );
162 if (reexportInfo) {
163 return {
164 exports: Array.from(reexportInfo.exports, name => {
165 return {
166 name,
167 from,
168 export: ids.concat(name),
169 canMangle: !(name in EMPTY_OBJECT) && false
170 };
171 }),
172 // TODO handle deep reexports
173 dependencies: [from.module]
174 };
175 } else {
176 return {
177 exports: true,
178 from: ids.length === 0 ? from : undefined,
179 canMangle: false,
180 dependencies: [from.module]
181 };
182 }
183 }
184 }
185
186 /**
187 * @param {ModuleGraph} moduleGraph the module graph
188 * @param {RuntimeSpec} runtime the runtime
189 * @param {Module} importedModule the imported module (optional)
190 * @returns {{exports?: Set<string>, checked?: Set<string>}} information
191 */
192 getStarReexports(
193 moduleGraph,
194 runtime,
195 importedModule = moduleGraph.getModule(this)
196 ) {
197 let importedExportsInfo = moduleGraph.getExportsInfo(importedModule);
198 const ids = this.getIds(moduleGraph);
199 if (ids.length > 0)
200 importedExportsInfo = importedExportsInfo.getNestedExportsInfo(ids);
201 let exportsInfo = moduleGraph.getExportsInfo(
202 moduleGraph.getParentModule(this)
203 );
204 if (this.names.length > 0)
205 exportsInfo = exportsInfo.getNestedExportsInfo(this.names);
206
207 const noExtraExports =
208 importedExportsInfo &&
209 importedExportsInfo.otherExportsInfo.provided === false;
210 const noExtraImports =
211 exportsInfo &&
212 exportsInfo.otherExportsInfo.getUsed(runtime) === UsageState.Unused;
213
214 if (!noExtraExports && !noExtraImports) {
215 return;
216 }
217
218 const isNamespaceImport =
219 importedModule.getExportsType(moduleGraph, false) === "namespace";
220
221 /** @type {Set<string>} */
222 const exports = new Set();
223 /** @type {Set<string>} */
224 const checked = new Set();
225
226 if (noExtraImports) {
227 for (const exportInfo of exportsInfo.orderedExports) {
228 const name = exportInfo.name;
229 if (exportInfo.getUsed(runtime) === UsageState.Unused) continue;
230 if (name === "__esModule" && isNamespaceImport) {
231 exports.add(name);
232 } else if (importedExportsInfo) {
233 const importedExportInfo =
234 importedExportsInfo.getReadOnlyExportInfo(name);
235 if (importedExportInfo.provided === false) continue;
236 exports.add(name);
237 if (importedExportInfo.provided === true) continue;
238 checked.add(name);
239 } else {
240 exports.add(name);
241 checked.add(name);
242 }
243 }
244 } else if (noExtraExports) {
245 for (const importedExportInfo of importedExportsInfo.orderedExports) {
246 const name = importedExportInfo.name;
247 if (importedExportInfo.provided === false) continue;
248 if (exportsInfo) {
249 const exportInfo = exportsInfo.getReadOnlyExportInfo(name);
250 if (exportInfo.getUsed(runtime) === UsageState.Unused) continue;
251 }
252 exports.add(name);
253 if (importedExportInfo.provided === true) continue;
254 checked.add(name);
255 }
256 if (isNamespaceImport) {
257 exports.add("__esModule");
258 checked.delete("__esModule");
259 }
260 }
261
262 return { exports, checked };
263 }
264
265 serialize(context) {
266 const { write } = context;
267 write(this.asiSafe);
268 write(this.range);
269 write(this.valueRange);
270 write(this.base);
271 write(this.names);
272 write(this.ids);
273 write(this.resultUsed);
274 super.serialize(context);
275 }
276
277 deserialize(context) {
278 const { read } = context;
279 this.asiSafe = read();
280 this.range = read();
281 this.valueRange = read();
282 this.base = read();
283 this.names = read();
284 this.ids = read();
285 this.resultUsed = read();
286 super.deserialize(context);
287 }
288}
289
290makeSerializable(
291 CommonJsExportRequireDependency,
292 "webpack/lib/dependencies/CommonJsExportRequireDependency"
293);
294
295CommonJsExportRequireDependency.Template = class CommonJsExportRequireDependencyTemplate extends (
296 ModuleDependency.Template
297) {
298 /**
299 * @param {Dependency} dependency the dependency for which the template should be applied
300 * @param {ReplaceSource} source the current replace source which can be modified
301 * @param {DependencyTemplateContext} templateContext the context object
302 * @returns {void}
303 */
304 apply(
305 dependency,
306 source,
307 {
308 module,
309 runtimeTemplate,
310 chunkGraph,
311 moduleGraph,
312 runtimeRequirements,
313 runtime
314 }
315 ) {
316 const dep = /** @type {CommonJsExportRequireDependency} */ (dependency);
317 const used = moduleGraph
318 .getExportsInfo(module)
319 .getUsedName(dep.names, runtime);
320
321 const [type, base] = handleDependencyBase(
322 dep.base,
323 module,
324 runtimeRequirements
325 );
326
327 const importedModule = moduleGraph.getModule(dep);
328 let requireExpr = runtimeTemplate.moduleExports({
329 module: importedModule,
330 chunkGraph,
331 request: dep.request,
332 weak: dep.weak,
333 runtimeRequirements
334 });
335 const ids = dep.getIds(moduleGraph);
336 const usedImported = moduleGraph
337 .getExportsInfo(importedModule)
338 .getUsedName(ids, runtime);
339 if (usedImported) {
340 const comment = equals(usedImported, ids)
341 ? ""
342 : Template.toNormalComment(propertyAccess(ids)) + " ";
343 requireExpr += `${comment}${propertyAccess(usedImported)}`;
344 }
345
346 switch (type) {
347 case "expression":
348 source.replace(
349 dep.range[0],
350 dep.range[1] - 1,
351 used
352 ? `${base}${propertyAccess(used)} = ${requireExpr}`
353 : `/* unused reexport */ ${requireExpr}`
354 );
355 return;
356 case "Object.defineProperty":
357 throw new Error("TODO");
358 default:
359 throw new Error("Unexpected type");
360 }
361 }
362};
363
364module.exports = CommonJsExportRequireDependency;
Note: See TracBrowser for help on using the repository browser.