UNPKG

6.61 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
8/** @typedef {import("webpack-sources").Source} Source */
9/** @typedef {import("./ChunkGraph")} ChunkGraph */
10/** @typedef {import("./DependencyTemplates")} DependencyTemplates */
11/** @typedef {import("./Module")} Module */
12/** @typedef {import("./ModuleGraph")} ModuleGraph */
13/** @typedef {import("./ModuleGraphConnection")} ModuleGraphConnection */
14/** @typedef {import("./ModuleGraphConnection").ConnectionState} ConnectionState */
15/** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
16/** @typedef {import("./WebpackError")} WebpackError */
17/** @typedef {import("./util/Hash")} Hash */
18/** @typedef {import("./util/runtime").RuntimeSpec} RuntimeSpec */
19
20/**
21 * @typedef {Object} UpdateHashContext
22 * @property {ChunkGraph} chunkGraph
23 * @property {RuntimeSpec} runtime
24 */
25
26/**
27 * @typedef {Object} SourcePosition
28 * @property {number} line
29 * @property {number=} column
30 */
31
32/**
33 * @typedef {Object} RealDependencyLocation
34 * @property {SourcePosition} start
35 * @property {SourcePosition=} end
36 * @property {number=} index
37 */
38
39/**
40 * @typedef {Object} SyntheticDependencyLocation
41 * @property {string} name
42 * @property {number=} index
43 */
44
45/** @typedef {SyntheticDependencyLocation|RealDependencyLocation} DependencyLocation */
46
47/**
48 * @typedef {Object} ExportSpec
49 * @property {string} name the name of the export
50 * @property {boolean=} canMangle can the export be renamed (defaults to true)
51 * @property {boolean=} terminalBinding is the export a terminal binding that should be checked for export star conflicts
52 * @property {(string | ExportSpec)[]=} exports nested exports
53 * @property {ModuleGraphConnection=} from when reexported: from which module
54 * @property {string[] | null=} export when reexported: from which export
55 */
56
57/**
58 * @typedef {Object} ExportsSpec
59 * @property {(string | ExportSpec)[] | true | null} exports exported names, true for unknown exports or null for no exports
60 * @property {Set<string>=} excludeExports when exports = true, list of unaffected exports
61 * @property {ModuleGraphConnection=} from when reexported: from which module
62 * @property {boolean=} canMangle can the export be renamed (defaults to true)
63 * @property {boolean=} terminalBinding are the exports terminal bindings that should be checked for export star conflicts
64 * @property {Module[]=} dependencies module on which the result depends on
65 */
66
67/**
68 * @typedef {Object} ReferencedExport
69 * @property {string[]} name name of the referenced export
70 * @property {boolean=} canMangle when false, referenced export can not be mangled, defaults to true
71 */
72
73class Dependency {
74 constructor() {
75 // TODO check if this can be moved into ModuleDependency
76 /** @type {boolean} */
77 this.weak = false;
78 // TODO check if this can be moved into ModuleDependency
79 /** @type {boolean} */
80 this.optional = false;
81 /** @type {DependencyLocation} */
82 this.loc = undefined;
83 }
84
85 /**
86 * @returns {string} a display name for the type of dependency
87 */
88 get type() {
89 return "unknown";
90 }
91
92 /**
93 * @returns {string} a dependency category, typical categories are "commonjs", "amd", "esm"
94 */
95 get category() {
96 return "unknown";
97 }
98
99 /**
100 * @returns {string | null} an identifier to merge equal requests
101 */
102 getResourceIdentifier() {
103 return null;
104 }
105
106 /**
107 * Returns the referenced module and export
108 * @deprecated
109 * @param {ModuleGraph} moduleGraph module graph
110 * @returns {never} throws error
111 */
112 getReference(moduleGraph) {
113 throw new Error(
114 "Dependency.getReference was removed in favor of Dependency.getReferencedExports, ModuleGraph.getModule and ModuleGraph.getConnection().active"
115 );
116 }
117
118 /**
119 * Returns list of exports referenced by this dependency
120 * @param {ModuleGraph} moduleGraph module graph
121 * @param {RuntimeSpec} runtime the runtime for which the module is analysed
122 * @returns {(string[] | ReferencedExport)[]} referenced exports
123 */
124 getReferencedExports(moduleGraph, runtime) {
125 return Dependency.EXPORTS_OBJECT_REFERENCED;
126 }
127
128 /**
129 * @param {ModuleGraph} moduleGraph module graph
130 * @returns {function(ModuleGraphConnection, RuntimeSpec): ConnectionState} function to determine if the connection is active
131 */
132 getCondition(moduleGraph) {
133 return null;
134 }
135
136 /**
137 * Returns the exported names
138 * @param {ModuleGraph} moduleGraph module graph
139 * @returns {ExportsSpec | undefined} export names
140 */
141 getExports(moduleGraph) {
142 return undefined;
143 }
144
145 /**
146 * Returns warnings
147 * @param {ModuleGraph} moduleGraph module graph
148 * @returns {WebpackError[]} warnings
149 */
150 getWarnings(moduleGraph) {
151 return null;
152 }
153
154 /**
155 * Returns errors
156 * @param {ModuleGraph} moduleGraph module graph
157 * @returns {WebpackError[]} errors
158 */
159 getErrors(moduleGraph) {
160 return null;
161 }
162
163 /**
164 * Update the hash
165 * @param {Hash} hash hash to be updated
166 * @param {UpdateHashContext} context context
167 * @returns {void}
168 */
169 updateHash(hash, context) {
170 const { chunkGraph } = context;
171 const module = chunkGraph.moduleGraph.getModule(this);
172 if (module) {
173 hash.update(chunkGraph.getModuleId(module) + "");
174 }
175 }
176
177 /**
178 * implement this method to allow the occurrence order plugin to count correctly
179 * @returns {number} count how often the id is used in this dependency
180 */
181 getNumberOfIdOccurrences() {
182 return 1;
183 }
184
185 /**
186 * @param {ModuleGraph} moduleGraph the module graph
187 * @returns {ConnectionState} how this dependency connects the module to referencing modules
188 */
189 getModuleEvaluationSideEffectsState(moduleGraph) {
190 return true;
191 }
192
193 serialize({ write }) {
194 write(this.weak);
195 write(this.optional);
196 write(this.loc);
197 }
198
199 deserialize({ read }) {
200 this.weak = read();
201 this.optional = read();
202 this.loc = read();
203 }
204}
205
206Dependency.NO_EXPORTS_REFERENCED = [];
207Dependency.EXPORTS_OBJECT_REFERENCED = [[]];
208
209Object.defineProperty(Dependency.prototype, "module", {
210 /**
211 * @deprecated
212 * @returns {never} throws
213 */
214 get() {
215 throw new Error(
216 "module property was removed from Dependency (use compilation.moduleGraph.getModule(dependency) instead)"
217 );
218 },
219
220 /**
221 * @deprecated
222 * @returns {never} throws
223 */
224 set() {
225 throw new Error(
226 "module property was removed from Dependency (use compilation.moduleGraph.updateModule(dependency, module) instead)"
227 );
228 }
229});
230
231Object.defineProperty(Dependency.prototype, "disconnect", {
232 get() {
233 throw new Error(
234 "disconnect was removed from Dependency (Dependency no longer carries graph specific information)"
235 );
236 }
237});
238
239module.exports = Dependency;