Dependency.js 9.5 KB

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