Dependency.js 10 KB

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