Dependency.js 10 KB

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