source: trip-planner-front/node_modules/webpack/lib/dependencies/CommonJsSelfReferenceDependency.js@ ceaed42

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

initial commit

  • Property mode set to 100644
File size: 3.8 KB
RevLine 
[6a3a178]1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const RuntimeGlobals = require("../RuntimeGlobals");
9const { equals } = require("../util/ArrayHelpers");
10const makeSerializable = require("../util/makeSerializable");
11const propertyAccess = require("../util/propertyAccess");
12const NullDependency = require("./NullDependency");
13
14/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
15/** @typedef {import("../Dependency")} Dependency */
16/** @typedef {import("../Dependency").ExportsSpec} ExportsSpec */
17/** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */
18/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
19/** @typedef {import("../ModuleGraph")} ModuleGraph */
20/** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
21
22class CommonJsSelfReferenceDependency extends NullDependency {
23 constructor(range, base, names, call) {
24 super();
25 this.range = range;
26 this.base = base;
27 this.names = names;
28 this.call = call;
29 }
30
31 get type() {
32 return "cjs self exports reference";
33 }
34
35 get category() {
36 return "self";
37 }
38
39 /**
40 * @returns {string | null} an identifier to merge equal requests
41 */
42 getResourceIdentifier() {
43 return `self`;
44 }
45
46 /**
47 * Returns list of exports referenced by this dependency
48 * @param {ModuleGraph} moduleGraph module graph
49 * @param {RuntimeSpec} runtime the runtime for which the module is analysed
50 * @returns {(string[] | ReferencedExport)[]} referenced exports
51 */
52 getReferencedExports(moduleGraph, runtime) {
53 return [this.call ? this.names.slice(0, -1) : this.names];
54 }
55
56 serialize(context) {
57 const { write } = context;
58 write(this.range);
59 write(this.base);
60 write(this.names);
61 write(this.call);
62 super.serialize(context);
63 }
64
65 deserialize(context) {
66 const { read } = context;
67 this.range = read();
68 this.base = read();
69 this.names = read();
70 this.call = read();
71 super.deserialize(context);
72 }
73}
74
75makeSerializable(
76 CommonJsSelfReferenceDependency,
77 "webpack/lib/dependencies/CommonJsSelfReferenceDependency"
78);
79
80CommonJsSelfReferenceDependency.Template = class CommonJsSelfReferenceDependencyTemplate extends (
81 NullDependency.Template
82) {
83 /**
84 * @param {Dependency} dependency the dependency for which the template should be applied
85 * @param {ReplaceSource} source the current replace source which can be modified
86 * @param {DependencyTemplateContext} templateContext the context object
87 * @returns {void}
88 */
89 apply(
90 dependency,
91 source,
92 { module, moduleGraph, runtime, runtimeRequirements }
93 ) {
94 const dep = /** @type {CommonJsSelfReferenceDependency} */ (dependency);
95 let used;
96 if (dep.names.length === 0) {
97 used = dep.names;
98 } else {
99 used = moduleGraph.getExportsInfo(module).getUsedName(dep.names, runtime);
100 }
101 if (!used) {
102 throw new Error(
103 "Self-reference dependency has unused export name: This should not happen"
104 );
105 }
106
107 let base = undefined;
108 switch (dep.base) {
109 case "exports":
110 runtimeRequirements.add(RuntimeGlobals.exports);
111 base = module.exportsArgument;
112 break;
113 case "module.exports":
114 runtimeRequirements.add(RuntimeGlobals.module);
115 base = `${module.moduleArgument}.exports`;
116 break;
117 case "this":
118 runtimeRequirements.add(RuntimeGlobals.thisAsExports);
119 base = "this";
120 break;
121 default:
122 throw new Error(`Unsupported base ${dep.base}`);
123 }
124
125 if (base === dep.base && equals(used, dep.names)) {
126 // Nothing has to be changed
127 // We don't use a replacement for compat reasons
128 // for plugins that update `module._source` which they
129 // shouldn't do!
130 return;
131 }
132
133 source.replace(
134 dep.range[0],
135 dep.range[1] - 1,
136 `${base}${propertyAccess(used)}`
137 );
138 }
139};
140
141module.exports = CommonJsSelfReferenceDependency;
Note: See TracBrowser for help on using the repository browser.