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