UNPKG

9.68 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 TRANSITIVE = Symbol("transitive");
82
83const getIgnoredModule = memoize(() => {
84 const RawModule = require("./RawModule");
85 return new RawModule("/* (ignored) */", `ignored`, `(ignored)`);
86});
87
88class Dependency {
89 constructor() {
90 /** @type {Module} */
91 this._parentModule = undefined;
92 /** @type {DependenciesBlock} */
93 this._parentDependenciesBlock = undefined;
94 /** @type {number} */
95 this._parentDependenciesBlockIndex = -1;
96 // TODO check if this can be moved into ModuleDependency
97 /** @type {boolean} */
98 this.weak = false;
99 // TODO check if this can be moved into ModuleDependency
100 /** @type {boolean} */
101 this.optional = false;
102 this._locSL = 0;
103 this._locSC = 0;
104 this._locEL = 0;
105 this._locEC = 0;
106 this._locI = undefined;
107 this._locN = undefined;
108 this._loc = undefined;
109 }
110
111 /**
112 * @returns {string} a display name for the type of dependency
113 */
114 get type() {
115 return "unknown";
116 }
117
118 /**
119 * @returns {string} a dependency category, typical categories are "commonjs", "amd", "esm"
120 */
121 get category() {
122 return "unknown";
123 }
124
125 /**
126 * @returns {DependencyLocation} location
127 */
128 get loc() {
129 if (this._loc !== undefined) return this._loc;
130 /** @type {SyntheticDependencyLocation & RealDependencyLocation} */
131 const loc = {};
132 if (this._locSL > 0) {
133 loc.start = { line: this._locSL, column: this._locSC };
134 }
135 if (this._locEL > 0) {
136 loc.end = { line: this._locEL, column: this._locEC };
137 }
138 if (this._locN !== undefined) {
139 loc.name = this._locN;
140 }
141 if (this._locI !== undefined) {
142 loc.index = this._locI;
143 }
144 return (this._loc = loc);
145 }
146
147 set loc(loc) {
148 if ("start" in loc && typeof loc.start === "object") {
149 this._locSL = loc.start.line || 0;
150 this._locSC = loc.start.column || 0;
151 } else {
152 this._locSL = 0;
153 this._locSC = 0;
154 }
155 if ("end" in loc && typeof loc.end === "object") {
156 this._locEL = loc.end.line || 0;
157 this._locEC = loc.end.column || 0;
158 } else {
159 this._locEL = 0;
160 this._locEC = 0;
161 }
162 if ("index" in loc) {
163 this._locI = loc.index;
164 } else {
165 this._locI = undefined;
166 }
167 if ("name" in loc) {
168 this._locN = loc.name;
169 } else {
170 this._locN = undefined;
171 }
172 this._loc = loc;
173 }
174
175 setLoc(startLine, startColumn, endLine, endColumn) {
176 this._locSL = startLine;
177 this._locSC = startColumn;
178 this._locEL = endLine;
179 this._locEC = endColumn;
180 this._locI = undefined;
181 this._locN = undefined;
182 this._loc = undefined;
183 }
184
185 /**
186 * @returns {string | undefined} a request context
187 */
188 getContext() {
189 return undefined;
190 }
191
192 /**
193 * @returns {string | null} an identifier to merge equal requests
194 */
195 getResourceIdentifier() {
196 return null;
197 }
198
199 /**
200 * @returns {boolean | TRANSITIVE} true, when changes to the referenced module could affect the referencing module; TRANSITIVE, when changes to the referenced module could affect referencing modules of the referencing module
201 */
202 couldAffectReferencingModule() {
203 return TRANSITIVE;
204 }
205
206 /**
207 * Returns the referenced module and export
208 * @deprecated
209 * @param {ModuleGraph} moduleGraph module graph
210 * @returns {never} throws error
211 */
212 getReference(moduleGraph) {
213 throw new Error(
214 "Dependency.getReference was removed in favor of Dependency.getReferencedExports, ModuleGraph.getModule and ModuleGraph.getConnection().active"
215 );
216 }
217
218 /**
219 * Returns list of exports referenced by this dependency
220 * @param {ModuleGraph} moduleGraph module graph
221 * @param {RuntimeSpec} runtime the runtime for which the module is analysed
222 * @returns {(string[] | ReferencedExport)[]} referenced exports
223 */
224 getReferencedExports(moduleGraph, runtime) {
225 return Dependency.EXPORTS_OBJECT_REFERENCED;
226 }
227
228 /**
229 * @param {ModuleGraph} moduleGraph module graph
230 * @returns {null | false | function(ModuleGraphConnection, RuntimeSpec): ConnectionState} function to determine if the connection is active
231 */
232 getCondition(moduleGraph) {
233 return null;
234 }
235
236 /**
237 * Returns the exported names
238 * @param {ModuleGraph} moduleGraph module graph
239 * @returns {ExportsSpec | undefined} export names
240 */
241 getExports(moduleGraph) {
242 return undefined;
243 }
244
245 /**
246 * Returns warnings
247 * @param {ModuleGraph} moduleGraph module graph
248 * @returns {WebpackError[]} warnings
249 */
250 getWarnings(moduleGraph) {
251 return null;
252 }
253
254 /**
255 * Returns errors
256 * @param {ModuleGraph} moduleGraph module graph
257 * @returns {WebpackError[]} errors
258 */
259 getErrors(moduleGraph) {
260 return null;
261 }
262
263 /**
264 * Update the hash
265 * @param {Hash} hash hash to be updated
266 * @param {UpdateHashContext} context context
267 * @returns {void}
268 */
269 updateHash(hash, context) {}
270
271 /**
272 * implement this method to allow the occurrence order plugin to count correctly
273 * @returns {number} count how often the id is used in this dependency
274 */
275 getNumberOfIdOccurrences() {
276 return 1;
277 }
278
279 /**
280 * @param {ModuleGraph} moduleGraph the module graph
281 * @returns {ConnectionState} how this dependency connects the module to referencing modules
282 */
283 getModuleEvaluationSideEffectsState(moduleGraph) {
284 return true;
285 }
286
287 /**
288 * @param {string} context context directory
289 * @returns {Module} a module
290 */
291 createIgnoredModule(context) {
292 return getIgnoredModule();
293 }
294
295 serialize({ write }) {
296 write(this.weak);
297 write(this.optional);
298 write(this._locSL);
299 write(this._locSC);
300 write(this._locEL);
301 write(this._locEC);
302 write(this._locI);
303 write(this._locN);
304 }
305
306 deserialize({ read }) {
307 this.weak = read();
308 this.optional = read();
309 this._locSL = read();
310 this._locSC = read();
311 this._locEL = read();
312 this._locEC = read();
313 this._locI = read();
314 this._locN = read();
315 }
316}
317
318/** @type {string[][]} */
319Dependency.NO_EXPORTS_REFERENCED = [];
320/** @type {string[][]} */
321Dependency.EXPORTS_OBJECT_REFERENCED = [[]];
322
323Object.defineProperty(Dependency.prototype, "module", {
324 /**
325 * @deprecated
326 * @returns {never} throws
327 */
328 get() {
329 throw new Error(
330 "module property was removed from Dependency (use compilation.moduleGraph.getModule(dependency) instead)"
331 );
332 },
333
334 /**
335 * @deprecated
336 * @returns {never} throws
337 */
338 set() {
339 throw new Error(
340 "module property was removed from Dependency (use compilation.moduleGraph.updateModule(dependency, module) instead)"
341 );
342 }
343});
344
345Object.defineProperty(Dependency.prototype, "disconnect", {
346 get() {
347 throw new Error(
348 "disconnect was removed from Dependency (Dependency no longer carries graph specific information)"
349 );
350 }
351});
352
353Dependency.TRANSITIVE = TRANSITIVE;
354
355module.exports = Dependency;