提交学习笔记专用
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.

313 lines
8.8 KiB

  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { CachedSource, ConcatSource, RawSource } = require("webpack-sources");
  7. const { UsageState } = require("./ExportsInfo");
  8. const Template = require("./Template");
  9. const CssModulesPlugin = require("./css/CssModulesPlugin");
  10. const JavascriptModulesPlugin = require("./javascript/JavascriptModulesPlugin");
  11. /** @typedef {import("webpack-sources").Source} Source */
  12. /** @typedef {import("./Compiler")} Compiler */
  13. /** @typedef {import("./ExportsInfo")} ExportsInfo */
  14. /** @typedef {import("./ExportsInfo").ExportInfo} ExportInfo */
  15. /** @typedef {import("./Module")} Module */
  16. /** @typedef {import("./Module").BuildMeta} BuildMeta */
  17. /** @typedef {import("./ModuleGraph")} ModuleGraph */
  18. /** @typedef {import("./RequestShortener")} RequestShortener */
  19. /**
  20. * @template T
  21. * @param {Iterable<T>} iterable iterable
  22. * @returns {string} joined with comma
  23. */
  24. const joinIterableWithComma = (iterable) => {
  25. // This is more performant than Array.from().join(", ")
  26. // as it doesn't create an array
  27. let str = "";
  28. let first = true;
  29. for (const item of iterable) {
  30. if (first) {
  31. first = false;
  32. } else {
  33. str += ", ";
  34. }
  35. str += item;
  36. }
  37. return str;
  38. };
  39. /**
  40. * @param {ConcatSource} source output
  41. * @param {string} indent spacing
  42. * @param {ExportsInfo} exportsInfo data
  43. * @param {ModuleGraph} moduleGraph moduleGraph
  44. * @param {RequestShortener} requestShortener requestShortener
  45. * @param {Set<ExportInfo>} alreadyPrinted deduplication set
  46. * @returns {void}
  47. */
  48. const printExportsInfoToSource = (
  49. source,
  50. indent,
  51. exportsInfo,
  52. moduleGraph,
  53. requestShortener,
  54. alreadyPrinted = new Set()
  55. ) => {
  56. const otherExportsInfo = exportsInfo.otherExportsInfo;
  57. let alreadyPrintedExports = 0;
  58. // determine exports to print
  59. const printedExports = [];
  60. for (const exportInfo of exportsInfo.orderedExports) {
  61. if (!alreadyPrinted.has(exportInfo)) {
  62. alreadyPrinted.add(exportInfo);
  63. printedExports.push(exportInfo);
  64. } else {
  65. alreadyPrintedExports++;
  66. }
  67. }
  68. let showOtherExports = false;
  69. if (!alreadyPrinted.has(otherExportsInfo)) {
  70. alreadyPrinted.add(otherExportsInfo);
  71. showOtherExports = true;
  72. } else {
  73. alreadyPrintedExports++;
  74. }
  75. // print the exports
  76. for (const exportInfo of printedExports) {
  77. const target = exportInfo.getTarget(moduleGraph);
  78. source.add(
  79. `${Template.toComment(
  80. `${indent}export ${JSON.stringify(exportInfo.name).slice(
  81. 1,
  82. -1
  83. )} [${exportInfo.getProvidedInfo()}] [${exportInfo.getUsedInfo()}] [${exportInfo.getRenameInfo()}]${
  84. target
  85. ? ` -> ${target.module.readableIdentifier(requestShortener)}${
  86. target.export
  87. ? ` .${target.export
  88. .map((e) => JSON.stringify(e).slice(1, -1))
  89. .join(".")}`
  90. : ""
  91. }`
  92. : ""
  93. }`
  94. )}\n`
  95. );
  96. if (exportInfo.exportsInfo) {
  97. printExportsInfoToSource(
  98. source,
  99. `${indent} `,
  100. exportInfo.exportsInfo,
  101. moduleGraph,
  102. requestShortener,
  103. alreadyPrinted
  104. );
  105. }
  106. }
  107. if (alreadyPrintedExports) {
  108. source.add(
  109. `${Template.toComment(
  110. `${indent}... (${alreadyPrintedExports} already listed exports)`
  111. )}\n`
  112. );
  113. }
  114. if (showOtherExports) {
  115. const target = otherExportsInfo.getTarget(moduleGraph);
  116. if (
  117. target ||
  118. otherExportsInfo.provided !== false ||
  119. otherExportsInfo.getUsed(undefined) !== UsageState.Unused
  120. ) {
  121. const title =
  122. printedExports.length > 0 || alreadyPrintedExports > 0
  123. ? "other exports"
  124. : "exports";
  125. source.add(
  126. `${Template.toComment(
  127. `${indent}${title} [${otherExportsInfo.getProvidedInfo()}] [${otherExportsInfo.getUsedInfo()}]${
  128. target
  129. ? ` -> ${target.module.readableIdentifier(requestShortener)}`
  130. : ""
  131. }`
  132. )}\n`
  133. );
  134. }
  135. }
  136. };
  137. /** @type {WeakMap<RequestShortener, WeakMap<Module, { header: RawSource | undefined, full: WeakMap<Source, CachedSource> }>>} */
  138. const caches = new WeakMap();
  139. const PLUGIN_NAME = "ModuleInfoHeaderPlugin";
  140. class ModuleInfoHeaderPlugin {
  141. /**
  142. * @param {boolean=} verbose add more information like exports, runtime requirements and bailouts
  143. */
  144. constructor(verbose = true) {
  145. this._verbose = verbose;
  146. }
  147. /**
  148. * @param {Compiler} compiler the compiler
  149. * @returns {void}
  150. */
  151. apply(compiler) {
  152. const { _verbose: verbose } = this;
  153. compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
  154. const javascriptHooks =
  155. JavascriptModulesPlugin.getCompilationHooks(compilation);
  156. javascriptHooks.renderModulePackage.tap(
  157. PLUGIN_NAME,
  158. (
  159. moduleSource,
  160. module,
  161. { chunk, chunkGraph, moduleGraph, runtimeTemplate }
  162. ) => {
  163. const { requestShortener } = runtimeTemplate;
  164. let cacheEntry;
  165. let cache = caches.get(requestShortener);
  166. if (cache === undefined) {
  167. caches.set(requestShortener, (cache = new WeakMap()));
  168. cache.set(
  169. module,
  170. (cacheEntry = { header: undefined, full: new WeakMap() })
  171. );
  172. } else {
  173. cacheEntry = cache.get(module);
  174. if (cacheEntry === undefined) {
  175. cache.set(
  176. module,
  177. (cacheEntry = { header: undefined, full: new WeakMap() })
  178. );
  179. } else if (!verbose) {
  180. const cachedSource = cacheEntry.full.get(moduleSource);
  181. if (cachedSource !== undefined) return cachedSource;
  182. }
  183. }
  184. const source = new ConcatSource();
  185. let header = cacheEntry.header;
  186. if (header === undefined) {
  187. header = this.generateHeader(module, requestShortener);
  188. cacheEntry.header = header;
  189. }
  190. source.add(header);
  191. if (verbose) {
  192. const exportsType = /** @type {BuildMeta} */ (module.buildMeta)
  193. .exportsType;
  194. source.add(
  195. `${Template.toComment(
  196. exportsType
  197. ? `${exportsType} exports`
  198. : "unknown exports (runtime-defined)"
  199. )}\n`
  200. );
  201. if (exportsType) {
  202. const exportsInfo = moduleGraph.getExportsInfo(module);
  203. printExportsInfoToSource(
  204. source,
  205. "",
  206. exportsInfo,
  207. moduleGraph,
  208. requestShortener
  209. );
  210. }
  211. source.add(
  212. `${Template.toComment(
  213. `runtime requirements: ${joinIterableWithComma(
  214. chunkGraph.getModuleRuntimeRequirements(module, chunk.runtime)
  215. )}`
  216. )}\n`
  217. );
  218. const optimizationBailout =
  219. moduleGraph.getOptimizationBailout(module);
  220. if (optimizationBailout) {
  221. for (const text of optimizationBailout) {
  222. const code =
  223. typeof text === "function" ? text(requestShortener) : text;
  224. source.add(`${Template.toComment(`${code}`)}\n`);
  225. }
  226. }
  227. source.add(moduleSource);
  228. return source;
  229. }
  230. source.add(moduleSource);
  231. const cachedSource = new CachedSource(source);
  232. cacheEntry.full.set(moduleSource, cachedSource);
  233. return cachedSource;
  234. }
  235. );
  236. javascriptHooks.chunkHash.tap(PLUGIN_NAME, (_chunk, hash) => {
  237. hash.update(PLUGIN_NAME);
  238. hash.update("1");
  239. });
  240. const cssHooks = CssModulesPlugin.getCompilationHooks(compilation);
  241. cssHooks.renderModulePackage.tap(
  242. PLUGIN_NAME,
  243. (moduleSource, module, { runtimeTemplate }) => {
  244. const { requestShortener } = runtimeTemplate;
  245. let cacheEntry;
  246. let cache = caches.get(requestShortener);
  247. if (cache === undefined) {
  248. caches.set(requestShortener, (cache = new WeakMap()));
  249. cache.set(
  250. module,
  251. (cacheEntry = { header: undefined, full: new WeakMap() })
  252. );
  253. } else {
  254. cacheEntry = cache.get(module);
  255. if (cacheEntry === undefined) {
  256. cache.set(
  257. module,
  258. (cacheEntry = { header: undefined, full: new WeakMap() })
  259. );
  260. } else if (!verbose) {
  261. const cachedSource = cacheEntry.full.get(moduleSource);
  262. if (cachedSource !== undefined) return cachedSource;
  263. }
  264. }
  265. const source = new ConcatSource();
  266. let header = cacheEntry.header;
  267. if (header === undefined) {
  268. header = this.generateHeader(module, requestShortener);
  269. cacheEntry.header = header;
  270. }
  271. source.add(header);
  272. source.add(moduleSource);
  273. const cachedSource = new CachedSource(source);
  274. cacheEntry.full.set(moduleSource, cachedSource);
  275. return cachedSource;
  276. }
  277. );
  278. cssHooks.chunkHash.tap(PLUGIN_NAME, (_chunk, hash) => {
  279. hash.update(PLUGIN_NAME);
  280. hash.update("1");
  281. });
  282. });
  283. }
  284. /**
  285. * @param {Module} module the module
  286. * @param {RequestShortener} requestShortener request shortener
  287. * @returns {RawSource} the header
  288. */
  289. generateHeader(module, requestShortener) {
  290. const req = module.readableIdentifier(requestShortener);
  291. const reqStr = req.replace(/\*\//g, "*_/");
  292. const reqStrStar = "*".repeat(reqStr.length);
  293. const headerStr = `/*!****${reqStrStar}****!*\\\n !*** ${reqStr} ***!\n \\****${reqStrStar}****/\n`;
  294. return new RawSource(headerStr);
  295. }
  296. }
  297. module.exports = ModuleInfoHeaderPlugin;