UNPKG

8.9 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const memoize = require("./util/memoize");
9
10/** @typedef {import("webpack-sources").Source} Source */
11/** @typedef {import("./ChunkGraph")} ChunkGraph */
12/** @typedef {import("./DependenciesBlock")} DependenciesBlock */
13/** @typedef {import("./DependencyTemplates")} DependencyTemplates */
14/** @typedef {import("./Module")} Module */
15/** @typedef {import("./ModuleGraph")} ModuleGraph */
16/** @typedef {import("./ModuleGraphConnection")} ModuleGraphConnection */
17/** @typedef {import("./ModuleGraphConnection").ConnectionState} ConnectionState */
18/** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
19/** @typedef {import("./WebpackError")} WebpackError */
20/** @typedef {import("./util/Hash")} Hash */
21/** @typedef {import("./util/runtime").RuntimeSpec} RuntimeSpec */
22
23/**
24 * @typedef {Object} UpdateHashContext
25 * @property {ChunkGraph} chunkGraph
26 * @property {RuntimeSpec} runtime
27 * @property {RuntimeTemplate=} runtimeTemplate
28 */
29
30/**
31 * @typedef {Object} SourcePosition
32 * @property {number} line
33 * @property {number=} column
34 */
35
36/**
37 * @typedef {Object} RealDependencyLocation
38 * @property {SourcePosition} start
39 * @property {SourcePosition=} end
40 * @property {number=} index
41 */
42
43/**
44 * @typedef {Object} SyntheticDependencyLocation
45 * @property {string} name
46 * @property {number=} index
47 */
48
49/** @typedef {SyntheticDependencyLocation|RealDependencyLocation} DependencyLocation */
50
51/**
52 * @typedef {Object} ExportSpec
53 * @property {string} name the name of the export
54 * @property {boolean=} canMangle can the export be renamed (defaults to true)
55 * @property {boolean=} terminalBinding is the export a terminal binding that should be checked for export star conflicts
56 * @property {(string | ExportSpec)[]=} exports nested exports
57 * @property {ModuleGraphConnection=} from when reexported: from which module
58 * @property {string[] | null=} export when reexported: from which export
59 * @property {number=} priority when reexported: with which priority
60 * @property {boolean=} hidden export is not visible, because another export blends over it
61 */
62
63/**
64 * @typedef {Object} ExportsSpec
65 * @property {(string | ExportSpec)[] | true | null} exports exported names, true for unknown exports or null for no exports
66 * @property {Set<string>=} excludeExports when exports = true, list of unaffected exports
67 * @property {Set<string>=} hideExports list of maybe prior exposed, but now hidden exports
68 * @property {ModuleGraphConnection=} from when reexported: from which module
69 * @property {number=} priority when reexported: with which priority
70 * @property {boolean=} canMangle can the export be renamed (defaults to true)
71 * @property {boolean=} terminalBinding are the exports terminal bindings that should be checked for export star conflicts
72 * @property {Module[]=} dependencies module on which the result depends on
73 */
74
75/**
76 * @typedef {Object} ReferencedExport
77 * @property {string[]} name name of the referenced export
78 * @property {boolean=} canMangle when false, referenced export can not be mangled, defaults to true
79 */
80
81const getIgnoredModule = memoize(() => {
82 const RawModule = require("./RawModule");
83 return new RawModule("/* (ignored) */", `ignored`, `(ignored)`);
84});
85
86class Dependency {
87 constructor() {
88 /** @type {Module} */
89 this._parentModule = undefined;
90 /** @type {DependenciesBlock} */
91 this._parentDependenciesBlock = undefined;
92 // TODO check if this can be moved into ModuleDependency
93 /** @type {boolean} */
94 this.weak = false;
95 // TODO check if this can be moved into ModuleDependency
96 /** @type {boolean} */
97 this.optional = false;
98 this._locSL = 0;
99 this._locSC = 0;
100 this._locEL = 0;
101 this._locEC = 0;
102 this._locI = undefined;
103 this._locN = undefined;
104 this._loc = undefined;
105 }
106
107 /**
108 * @returns {string} a display name for the type of dependency
109 */
110 get type() {
111 return "unknown";
112 }
113
114 /**
115 * @returns {string} a dependency category, typical categories are "commonjs", "amd", "esm"
116 */
117 get category() {
118 return "unknown";
119 }
120
121 /**
122 * @returns {DependencyLocation} location
123 */
124 get loc() {
125 if (this._loc !== undefined) return this._loc;
126 /** @type {SyntheticDependencyLocation & RealDependencyLocation} */
127 const loc = {};
128 if (this._locSL > 0) {
129 loc.start = { line: this._locSL, column: this._locSC };
130 }
131 if (this._locEL > 0) {
132 loc.end = { line: this._locEL, column: this._locEC };
133 }
134 if (this._locN !== undefined) {
135 loc.name = this._locN;
136 }
137 if (this._locI !== undefined) {
138 loc.index = this._locI;
139 }
140 return (this._loc = loc);
141 }
142
143 set loc(loc) {
144 if ("start" in loc && typeof loc.start === "object") {
145 this._locSL = loc.start.line || 0;
146 this._locSC = loc.start.column || 0;
147 } else {
148 this._locSL = 0;
149 this._locSC = 0;
150 }
151 if ("end" in loc && typeof loc.end === "object") {
152 this._locEL = loc.end.line || 0;
153 this._locEC = loc.end.column || 0;
154 } else {
155 this._locEL = 0;
156 this._locEC = 0;
157 }
158 if ("index" in loc) {
159 this._locI = loc.index;
160 } else {
161 this._locI = undefined;
162 }
163 if ("name" in loc) {
164 this._locN = loc.name;
165 } else {
166 this._locN = undefined;
167 }
168 this._loc = loc;
169 }
170
171 /**
172 * @returns {string | null} an identifier to merge equal requests
173 */
174 getResourceIdentifier() {
175 return null;
176 }
177
178 /**
179 * Returns the referenced module and export
180 * @deprecated
181 * @param {ModuleGraph} moduleGraph module graph
182 * @returns {never} throws error
183 */
184 getReference(moduleGraph) {
185 throw new Error(
186 "Dependency.getReference was removed in favor of Dependency.getReferencedExports, ModuleGraph.getModule and ModuleGraph.getConnection().active"
187 );
188 }
189
190 /**
191 * Returns list of exports referenced by this dependency
192 * @param {ModuleGraph} moduleGraph module graph
193 * @param {RuntimeSpec} runtime the runtime for which the module is analysed
194 * @returns {(string[] | ReferencedExport)[]} referenced exports
195 */
196 getReferencedExports(moduleGraph, runtime) {
197 return Dependency.EXPORTS_OBJECT_REFERENCED;
198 }
199
200 /**
201 * @param {ModuleGraph} moduleGraph module graph
202 * @returns {null | false | function(ModuleGraphConnection, RuntimeSpec): ConnectionState} function to determine if the connection is active
203 */
204 getCondition(moduleGraph) {
205 return null;
206 }
207
208 /**
209 * Returns the exported names
210 * @param {ModuleGraph} moduleGraph module graph
211 * @returns {ExportsSpec | undefined} export names
212 */
213 getExports(moduleGraph) {
214 return undefined;
215 }
216
217 /**
218 * Returns warnings
219 * @param {ModuleGraph} moduleGraph module graph
220 * @returns {WebpackError[]} warnings
221 */
222 getWarnings(moduleGraph) {
223 return null;
224 }
225
226 /**
227 * Returns errors
228 * @param {ModuleGraph} moduleGraph module graph
229 * @returns {WebpackError[]} errors
230 */
231 getErrors(moduleGraph) {
232 return null;
233 }
234
235 /**
236 * Update the hash
237 * @param {Hash} hash hash to be updated
238 * @param {UpdateHashContext} context context
239 * @returns {void}
240 */
241 updateHash(hash, context) {}
242
243 /**
244 * implement this method to allow the occurrence order plugin to count correctly
245 * @returns {number} count how often the id is used in this dependency
246 */
247 getNumberOfIdOccurrences() {
248 return 1;
249 }
250
251 /**
252 * @param {ModuleGraph} moduleGraph the module graph
253 * @returns {ConnectionState} how this dependency connects the module to referencing modules
254 */
255 getModuleEvaluationSideEffectsState(moduleGraph) {
256 return true;
257 }
258
259 /**
260 * @param {string} context context directory
261 * @returns {Module} a module
262 */
263 createIgnoredModule(context) {
264 return getIgnoredModule();
265 }
266
267 serialize({ write }) {
268 write(this.weak);
269 write(this.optional);
270 write(this._locSL);
271 write(this._locSC);
272 write(this._locEL);
273 write(this._locEC);
274 write(this._locI);
275 write(this._locN);
276 }
277
278 deserialize({ read }) {
279 this.weak = read();
280 this.optional = read();
281 this._locSL = read();
282 this._locSC = read();
283 this._locEL = read();
284 this._locEC = read();
285 this._locI = read();
286 this._locN = read();
287 }
288}
289
290/** @type {string[][]} */
291Dependency.NO_EXPORTS_REFERENCED = [];
292/** @type {string[][]} */
293Dependency.EXPORTS_OBJECT_REFERENCED = [[]];
294
295Object.defineProperty(Dependency.prototype, "module", {
296 /**
297 * @deprecated
298 * @returns {never} throws
299 */
300 get() {
301 throw new Error(
302 "module property was removed from Dependency (use compilation.moduleGraph.getModule(dependency) instead)"
303 );
304 },
305
306 /**
307 * @deprecated
308 * @returns {never} throws
309 */
310 set() {
311 throw new Error(
312 "module property was removed from Dependency (use compilation.moduleGraph.updateModule(dependency, module) instead)"
313 );
314 }
315});
316
317Object.defineProperty(Dependency.prototype, "disconnect", {
318 get() {
319 throw new Error(
320 "disconnect was removed from Dependency (Dependency no longer carries graph specific information)"
321 );
322 }
323});
324
325module.exports = Dependency;