提交学习笔记专用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

378 lines
10 KiB

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