Dependency.js 10 KB

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