UNPKG

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