Dependency.js 12 KB

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