Dependency.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. () => new RawModule("/* (ignored) */", "ignored", "(ignored)")
  76. );
  77. class Dependency {
  78. constructor() {
  79. /** @type {Module | undefined} */
  80. this._parentModule = undefined;
  81. /** @type {DependenciesBlock | undefined} */
  82. this._parentDependenciesBlock = undefined;
  83. /** @type {number} */
  84. this._parentDependenciesBlockIndex = -1;
  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. this._locSL = 0;
  92. this._locSC = 0;
  93. this._locEL = 0;
  94. this._locEC = 0;
  95. this._locI = undefined;
  96. this._locN = undefined;
  97. this._loc = undefined;
  98. }
  99. /**
  100. * @returns {string} a display name for the type of dependency
  101. */
  102. get type() {
  103. return "unknown";
  104. }
  105. /**
  106. * @returns {string} a dependency category, typical categories are "commonjs", "amd", "esm"
  107. */
  108. get category() {
  109. return "unknown";
  110. }
  111. /**
  112. * @returns {DependencyLocation} location
  113. */
  114. get loc() {
  115. if (this._loc !== undefined) return this._loc;
  116. /** @type {SyntheticDependencyLocation & RealDependencyLocation} */
  117. const loc = {
  118. start: { line: 0, column: 0 },
  119. end: { line: 0, column: 0 },
  120. name: "",
  121. index: -1
  122. };
  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. loc.name = this._locN;
  130. loc.index = this._locI;
  131. return (this._loc = loc);
  132. }
  133. set loc(loc) {
  134. if ("start" in loc && typeof loc.start === "object") {
  135. this._locSL = loc.start.line || 0;
  136. this._locSC = loc.start.column || 0;
  137. } else {
  138. this._locSL = 0;
  139. this._locSC = 0;
  140. }
  141. if ("end" in loc && typeof loc.end === "object") {
  142. this._locEL = loc.end.line || 0;
  143. this._locEC = loc.end.column || 0;
  144. } else {
  145. this._locEL = 0;
  146. this._locEC = 0;
  147. }
  148. this._locI = "index" in loc ? loc.index : undefined;
  149. this._locN = "name" in loc ? loc.name : undefined;
  150. this._loc = loc;
  151. }
  152. /**
  153. * @param {number} startLine start line
  154. * @param {number} startColumn start column
  155. * @param {number} endLine end line
  156. * @param {number} endColumn end column
  157. */
  158. setLoc(startLine, startColumn, endLine, endColumn) {
  159. this._locSL = startLine;
  160. this._locSC = startColumn;
  161. this._locEL = endLine;
  162. this._locEC = endColumn;
  163. this._locI = undefined;
  164. this._locN = undefined;
  165. this._loc = undefined;
  166. }
  167. /**
  168. * @returns {string | undefined} a request context
  169. */
  170. getContext() {
  171. return undefined;
  172. }
  173. /**
  174. * @returns {string | null} an identifier to merge equal requests
  175. */
  176. getResourceIdentifier() {
  177. return null;
  178. }
  179. /**
  180. * @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
  181. */
  182. couldAffectReferencingModule() {
  183. return TRANSITIVE;
  184. }
  185. /**
  186. * Returns the referenced module and export
  187. * @deprecated
  188. * @param {ModuleGraph} moduleGraph module graph
  189. * @returns {never} throws error
  190. */
  191. getReference(moduleGraph) {
  192. throw new Error(
  193. "Dependency.getReference was removed in favor of Dependency.getReferencedExports, ModuleGraph.getModule and ModuleGraph.getConnection().active"
  194. );
  195. }
  196. /**
  197. * Returns list of exports referenced by this dependency
  198. * @param {ModuleGraph} moduleGraph module graph
  199. * @param {RuntimeSpec} runtime the runtime for which the module is analysed
  200. * @returns {(string[] | ReferencedExport)[]} referenced exports
  201. */
  202. getReferencedExports(moduleGraph, runtime) {
  203. return Dependency.EXPORTS_OBJECT_REFERENCED;
  204. }
  205. /**
  206. * @param {ModuleGraph} moduleGraph module graph
  207. * @returns {null | false | GetConditionFn} function to determine if the connection is active
  208. */
  209. getCondition(moduleGraph) {
  210. return null;
  211. }
  212. /**
  213. * Returns the exported names
  214. * @param {ModuleGraph} moduleGraph module graph
  215. * @returns {ExportsSpec | undefined} export names
  216. */
  217. getExports(moduleGraph) {
  218. return undefined;
  219. }
  220. /**
  221. * Returns warnings
  222. * @param {ModuleGraph} moduleGraph module graph
  223. * @returns {WebpackError[] | null | undefined} warnings
  224. */
  225. getWarnings(moduleGraph) {
  226. return null;
  227. }
  228. /**
  229. * Returns errors
  230. * @param {ModuleGraph} moduleGraph module graph
  231. * @returns {WebpackError[] | null | undefined} errors
  232. */
  233. getErrors(moduleGraph) {
  234. return null;
  235. }
  236. /**
  237. * Update the hash
  238. * @param {Hash} hash hash to be updated
  239. * @param {UpdateHashContext} context context
  240. * @returns {void}
  241. */
  242. updateHash(hash, context) {}
  243. /**
  244. * implement this method to allow the occurrence order plugin to count correctly
  245. * @returns {number} count how often the id is used in this dependency
  246. */
  247. getNumberOfIdOccurrences() {
  248. return 1;
  249. }
  250. /**
  251. * @param {ModuleGraph} moduleGraph the module graph
  252. * @returns {ConnectionState} how this dependency connects the module to referencing modules
  253. */
  254. getModuleEvaluationSideEffectsState(moduleGraph) {
  255. return true;
  256. }
  257. /**
  258. * @param {string} context context directory
  259. * @returns {Module | null} a module
  260. */
  261. createIgnoredModule(context) {
  262. return getIgnoredModule();
  263. }
  264. /**
  265. * @param {ObjectSerializerContext} context context
  266. */
  267. serialize({ write }) {
  268. write(this.weak);
  269. write(this.optional);
  270. write(this._locSL);
  271. write(this._locSC);
  272. write(this._locEL);
  273. write(this._locEC);
  274. write(this._locI);
  275. write(this._locN);
  276. }
  277. /**
  278. * @param {ObjectDeserializerContext} context context
  279. */
  280. deserialize({ read }) {
  281. this.weak = read();
  282. this.optional = read();
  283. this._locSL = read();
  284. this._locSC = read();
  285. this._locEL = read();
  286. this._locEC = read();
  287. this._locI = read();
  288. this._locN = read();
  289. }
  290. }
  291. /** @type {string[][]} */
  292. Dependency.NO_EXPORTS_REFERENCED = [];
  293. /** @type {string[][]} */
  294. Dependency.EXPORTS_OBJECT_REFERENCED = [[]];
  295. // TODO remove in webpack 6
  296. Object.defineProperty(Dependency.prototype, "module", {
  297. /**
  298. * @deprecated
  299. * @returns {EXPECTED_ANY} throws
  300. */
  301. get() {
  302. throw new Error(
  303. "module property was removed from Dependency (use compilation.moduleGraph.getModule(dependency) instead)"
  304. );
  305. },
  306. /**
  307. * @deprecated
  308. * @returns {never} throws
  309. */
  310. set() {
  311. throw new Error(
  312. "module property was removed from Dependency (use compilation.moduleGraph.updateModule(dependency, module) instead)"
  313. );
  314. }
  315. });
  316. // TODO remove in webpack 6
  317. Object.defineProperty(Dependency.prototype, "disconnect", {
  318. /**
  319. * @deprecated
  320. * @returns {EXPECTED_ANY} throws
  321. */
  322. get() {
  323. throw new Error(
  324. "disconnect was removed from Dependency (Dependency no longer carries graph specific information)"
  325. );
  326. }
  327. });
  328. Dependency.TRANSITIVE = TRANSITIVE;
  329. module.exports = Dependency;