UNPKG

9.34 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 /**
176 * @returns {string | null} an identifier to merge equal requests
177 */
178 getResourceIdentifier() {
179 return null;
180 }
181
182 /**
183 * @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
184 */
185 couldAffectReferencingModule() {
186 return TRANSITIVE;
187 }
188
189 /**
190 * Returns the referenced module and export
191 * @deprecated
192 * @param {ModuleGraph} moduleGraph module graph
193 * @returns {never} throws error
194 */
195 getReference(moduleGraph) {
196 throw new Error(
197 "Dependency.getReference was removed in favor of Dependency.getReferencedExports, ModuleGraph.getModule and ModuleGraph.getConnection().active"
198 );
199 }
200
201 /**
202 * Returns list of exports referenced by this dependency
203 * @param {ModuleGraph} moduleGraph module graph
204 * @param {RuntimeSpec} runtime the runtime for which the module is analysed
205 * @returns {(string[] | ReferencedExport)[]} referenced exports
206 */
207 getReferencedExports(moduleGraph, runtime) {
208 return Dependency.EXPORTS_OBJECT_REFERENCED;
209 }
210
211 /**
212 * @param {ModuleGraph} moduleGraph module graph
213 * @returns {null | false | function(ModuleGraphConnection, RuntimeSpec): ConnectionState} function to determine if the connection is active
214 */
215 getCondition(moduleGraph) {
216 return null;
217 }
218
219 /**
220 * Returns the exported names
221 * @param {ModuleGraph} moduleGraph module graph
222 * @returns {ExportsSpec | undefined} export names
223 */
224 getExports(moduleGraph) {
225 return undefined;
226 }
227
228 /**
229 * Returns warnings
230 * @param {ModuleGraph} moduleGraph module graph
231 * @returns {WebpackError[]} warnings
232 */
233 getWarnings(moduleGraph) {
234 return null;
235 }
236
237 /**
238 * Returns errors
239 * @param {ModuleGraph} moduleGraph module graph
240 * @returns {WebpackError[]} errors
241 */
242 getErrors(moduleGraph) {
243 return null;
244 }
245
246 /**
247 * Update the hash
248 * @param {Hash} hash hash to be updated
249 * @param {UpdateHashContext} context context
250 * @returns {void}
251 */
252 updateHash(hash, context) {}
253
254 /**
255 * implement this method to allow the occurrence order plugin to count correctly
256 * @returns {number} count how often the id is used in this dependency
257 */
258 getNumberOfIdOccurrences() {
259 return 1;
260 }
261
262 /**
263 * @param {ModuleGraph} moduleGraph the module graph
264 * @returns {ConnectionState} how this dependency connects the module to referencing modules
265 */
266 getModuleEvaluationSideEffectsState(moduleGraph) {
267 return true;
268 }
269
270 /**
271 * @param {string} context context directory
272 * @returns {Module} a module
273 */
274 createIgnoredModule(context) {
275 return getIgnoredModule();
276 }
277
278 serialize({ write }) {
279 write(this.weak);
280 write(this.optional);
281 write(this._locSL);
282 write(this._locSC);
283 write(this._locEL);
284 write(this._locEC);
285 write(this._locI);
286 write(this._locN);
287 }
288
289 deserialize({ read }) {
290 this.weak = read();
291 this.optional = read();
292 this._locSL = read();
293 this._locSC = read();
294 this._locEL = read();
295 this._locEC = read();
296 this._locI = read();
297 this._locN = read();
298 }
299}
300
301/** @type {string[][]} */
302Dependency.NO_EXPORTS_REFERENCED = [];
303/** @type {string[][]} */
304Dependency.EXPORTS_OBJECT_REFERENCED = [[]];
305
306Object.defineProperty(Dependency.prototype, "module", {
307 /**
308 * @deprecated
309 * @returns {never} throws
310 */
311 get() {
312 throw new Error(
313 "module property was removed from Dependency (use compilation.moduleGraph.getModule(dependency) instead)"
314 );
315 },
316
317 /**
318 * @deprecated
319 * @returns {never} throws
320 */
321 set() {
322 throw new Error(
323 "module property was removed from Dependency (use compilation.moduleGraph.updateModule(dependency, module) instead)"
324 );
325 }
326});
327
328Object.defineProperty(Dependency.prototype, "disconnect", {
329 get() {
330 throw new Error(
331 "disconnect was removed from Dependency (Dependency no longer carries graph specific information)"
332 );
333 }
334});
335
336Dependency.TRANSITIVE = TRANSITIVE;
337
338module.exports = Dependency;