Dependency.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 {function(ModuleGraphConnection, 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. if (this._locSL > 0) {
  119. loc.start = { line: this._locSL, column: this._locSC };
  120. }
  121. if (this._locEL > 0) {
  122. loc.end = { line: this._locEL, column: this._locEC };
  123. }
  124. if (this._locN !== undefined) {
  125. loc.name = this._locN;
  126. }
  127. if (this._locI !== undefined) {
  128. loc.index = this._locI;
  129. }
  130. return (this._loc = loc);
  131. }
  132. set loc(loc) {
  133. if ("start" in loc && typeof loc.start === "object") {
  134. this._locSL = loc.start.line || 0;
  135. this._locSC = loc.start.column || 0;
  136. } else {
  137. this._locSL = 0;
  138. this._locSC = 0;
  139. }
  140. if ("end" in loc && typeof loc.end === "object") {
  141. this._locEL = loc.end.line || 0;
  142. this._locEC = loc.end.column || 0;
  143. } else {
  144. this._locEL = 0;
  145. this._locEC = 0;
  146. }
  147. this._locI = "index" in loc ? loc.index : undefined;
  148. this._locN = "name" in loc ? loc.name : undefined;
  149. this._loc = loc;
  150. }
  151. /**
  152. * @param {number} startLine start line
  153. * @param {number} startColumn start column
  154. * @param {number} endLine end line
  155. * @param {number} endColumn end column
  156. */
  157. setLoc(startLine, startColumn, endLine, endColumn) {
  158. this._locSL = startLine;
  159. this._locSC = startColumn;
  160. this._locEL = endLine;
  161. this._locEC = endColumn;
  162. this._locI = undefined;
  163. this._locN = undefined;
  164. this._loc = undefined;
  165. }
  166. /**
  167. * @returns {string | undefined} a request context
  168. */
  169. getContext() {
  170. return undefined;
  171. }
  172. /**
  173. * @returns {string | null} an identifier to merge equal requests
  174. */
  175. getResourceIdentifier() {
  176. return null;
  177. }
  178. /**
  179. * @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
  180. */
  181. couldAffectReferencingModule() {
  182. return TRANSITIVE;
  183. }
  184. /**
  185. * Returns the referenced module and export
  186. * @deprecated
  187. * @param {ModuleGraph} moduleGraph module graph
  188. * @returns {never} throws error
  189. */
  190. getReference(moduleGraph) {
  191. throw new Error(
  192. "Dependency.getReference was removed in favor of Dependency.getReferencedExports, ModuleGraph.getModule and ModuleGraph.getConnection().active"
  193. );
  194. }
  195. /**
  196. * Returns list of exports referenced by this dependency
  197. * @param {ModuleGraph} moduleGraph module graph
  198. * @param {RuntimeSpec} runtime the runtime for which the module is analysed
  199. * @returns {(string[] | ReferencedExport)[]} referenced exports
  200. */
  201. getReferencedExports(moduleGraph, runtime) {
  202. return Dependency.EXPORTS_OBJECT_REFERENCED;
  203. }
  204. /**
  205. * @param {ModuleGraph} moduleGraph module graph
  206. * @returns {null | false | GetConditionFn} function to determine if the connection is active
  207. */
  208. getCondition(moduleGraph) {
  209. return null;
  210. }
  211. /**
  212. * Returns the exported names
  213. * @param {ModuleGraph} moduleGraph module graph
  214. * @returns {ExportsSpec | undefined} export names
  215. */
  216. getExports(moduleGraph) {
  217. return undefined;
  218. }
  219. /**
  220. * Returns warnings
  221. * @param {ModuleGraph} moduleGraph module graph
  222. * @returns {WebpackError[] | null | undefined} warnings
  223. */
  224. getWarnings(moduleGraph) {
  225. return null;
  226. }
  227. /**
  228. * Returns errors
  229. * @param {ModuleGraph} moduleGraph module graph
  230. * @returns {WebpackError[] | null | undefined} errors
  231. */
  232. getErrors(moduleGraph) {
  233. return null;
  234. }
  235. /**
  236. * Update the hash
  237. * @param {Hash} hash hash to be updated
  238. * @param {UpdateHashContext} context context
  239. * @returns {void}
  240. */
  241. updateHash(hash, context) {}
  242. /**
  243. * implement this method to allow the occurrence order plugin to count correctly
  244. * @returns {number} count how often the id is used in this dependency
  245. */
  246. getNumberOfIdOccurrences() {
  247. return 1;
  248. }
  249. /**
  250. * @param {ModuleGraph} moduleGraph the module graph
  251. * @returns {ConnectionState} how this dependency connects the module to referencing modules
  252. */
  253. getModuleEvaluationSideEffectsState(moduleGraph) {
  254. return true;
  255. }
  256. /**
  257. * @param {string} context context directory
  258. * @returns {Module | null} a module
  259. */
  260. createIgnoredModule(context) {
  261. return getIgnoredModule();
  262. }
  263. /**
  264. * @param {ObjectSerializerContext} context context
  265. */
  266. serialize({ write }) {
  267. write(this.weak);
  268. write(this.optional);
  269. write(this._locSL);
  270. write(this._locSC);
  271. write(this._locEL);
  272. write(this._locEC);
  273. write(this._locI);
  274. write(this._locN);
  275. }
  276. /**
  277. * @param {ObjectDeserializerContext} context context
  278. */
  279. deserialize({ read }) {
  280. this.weak = read();
  281. this.optional = read();
  282. this._locSL = read();
  283. this._locSC = read();
  284. this._locEL = read();
  285. this._locEC = read();
  286. this._locI = read();
  287. this._locN = read();
  288. }
  289. }
  290. /** @type {string[][]} */
  291. Dependency.NO_EXPORTS_REFERENCED = [];
  292. /** @type {string[][]} */
  293. Dependency.EXPORTS_OBJECT_REFERENCED = [[]];
  294. // eslint-disable-next-line no-warning-comments
  295. // @ts-ignore https://github.com/microsoft/TypeScript/issues/42919
  296. Object.defineProperty(Dependency.prototype, "module", {
  297. /**
  298. * @deprecated
  299. * @returns {never} 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. // eslint-disable-next-line no-warning-comments
  317. // @ts-ignore https://github.com/microsoft/TypeScript/issues/42919
  318. Object.defineProperty(Dependency.prototype, "disconnect", {
  319. get() {
  320. throw new Error(
  321. "disconnect was removed from Dependency (Dependency no longer carries graph specific information)"
  322. );
  323. }
  324. });
  325. Dependency.TRANSITIVE = TRANSITIVE;
  326. module.exports = Dependency;