ModuleInfoHeaderPlugin.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. /** @type {ExportInfo[]} */
  60. const printedExports = [];
  61. for (const exportInfo of exportsInfo.orderedExports) {
  62. if (!alreadyPrinted.has(exportInfo)) {
  63. alreadyPrinted.add(exportInfo);
  64. printedExports.push(exportInfo);
  65. } else {
  66. alreadyPrintedExports++;
  67. }
  68. }
  69. let showOtherExports = false;
  70. if (!alreadyPrinted.has(otherExportsInfo)) {
  71. alreadyPrinted.add(otherExportsInfo);
  72. showOtherExports = true;
  73. } else {
  74. alreadyPrintedExports++;
  75. }
  76. // print the exports
  77. for (const exportInfo of printedExports) {
  78. const target = exportInfo.getTarget(moduleGraph);
  79. source.add(
  80. `${Template.toComment(
  81. `${indent}export ${JSON.stringify(exportInfo.name).slice(
  82. 1,
  83. -1
  84. )} [${exportInfo.getProvidedInfo()}] [${exportInfo.getUsedInfo()}] [${exportInfo.getRenameInfo()}]${
  85. target
  86. ? ` -> ${target.module.readableIdentifier(requestShortener)}${
  87. target.export
  88. ? ` .${target.export
  89. .map((e) => JSON.stringify(e).slice(1, -1))
  90. .join(".")}`
  91. : ""
  92. }`
  93. : ""
  94. }`
  95. )}\n`
  96. );
  97. if (exportInfo.exportsInfo) {
  98. printExportsInfoToSource(
  99. source,
  100. `${indent} `,
  101. exportInfo.exportsInfo,
  102. moduleGraph,
  103. requestShortener,
  104. alreadyPrinted
  105. );
  106. }
  107. }
  108. if (alreadyPrintedExports) {
  109. source.add(
  110. `${Template.toComment(
  111. `${indent}... (${alreadyPrintedExports} already listed exports)`
  112. )}\n`
  113. );
  114. }
  115. if (showOtherExports) {
  116. const target = otherExportsInfo.getTarget(moduleGraph);
  117. if (
  118. target ||
  119. otherExportsInfo.provided !== false ||
  120. otherExportsInfo.getUsed(undefined) !== UsageState.Unused
  121. ) {
  122. const title =
  123. printedExports.length > 0 || alreadyPrintedExports > 0
  124. ? "other exports"
  125. : "exports";
  126. source.add(
  127. `${Template.toComment(
  128. `${indent}${title} [${otherExportsInfo.getProvidedInfo()}] [${otherExportsInfo.getUsedInfo()}]${
  129. target
  130. ? ` -> ${target.module.readableIdentifier(requestShortener)}`
  131. : ""
  132. }`
  133. )}\n`
  134. );
  135. }
  136. }
  137. };
  138. /** @typedef {{ header: RawSource | undefined, full: WeakMap<Source, CachedSource> }} CacheEntry */
  139. /** @type {WeakMap<RequestShortener, WeakMap<Module, CacheEntry>>} */
  140. const caches = new WeakMap();
  141. const PLUGIN_NAME = "ModuleInfoHeaderPlugin";
  142. class ModuleInfoHeaderPlugin {
  143. /**
  144. * @param {boolean=} verbose add more information like exports, runtime requirements and bailouts
  145. */
  146. constructor(verbose = true) {
  147. /** @type {boolean} */
  148. this._verbose = verbose;
  149. }
  150. /**
  151. * @param {Compiler} compiler the compiler
  152. * @returns {void}
  153. */
  154. apply(compiler) {
  155. const { _verbose: verbose } = this;
  156. compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
  157. const javascriptHooks =
  158. JavascriptModulesPlugin.getCompilationHooks(compilation);
  159. javascriptHooks.renderModulePackage.tap(
  160. PLUGIN_NAME,
  161. (
  162. moduleSource,
  163. module,
  164. { chunk, chunkGraph, moduleGraph, runtimeTemplate }
  165. ) => {
  166. const { requestShortener } = runtimeTemplate;
  167. /** @type {undefined | CacheEntry} */
  168. let cacheEntry;
  169. let cache = caches.get(requestShortener);
  170. if (cache === undefined) {
  171. caches.set(requestShortener, (cache = new WeakMap()));
  172. cache.set(
  173. module,
  174. (cacheEntry = { header: undefined, full: new WeakMap() })
  175. );
  176. } else {
  177. cacheEntry = cache.get(module);
  178. if (cacheEntry === undefined) {
  179. cache.set(
  180. module,
  181. (cacheEntry = { header: undefined, full: new WeakMap() })
  182. );
  183. } else if (!verbose) {
  184. const cachedSource = cacheEntry.full.get(moduleSource);
  185. if (cachedSource !== undefined) return cachedSource;
  186. }
  187. }
  188. const source = new ConcatSource();
  189. let header = cacheEntry.header;
  190. if (header === undefined) {
  191. header = this.generateHeader(module, requestShortener);
  192. cacheEntry.header = header;
  193. }
  194. source.add(header);
  195. if (verbose) {
  196. const exportsType = /** @type {BuildMeta} */ (module.buildMeta)
  197. .exportsType;
  198. source.add(
  199. `${Template.toComment(
  200. exportsType
  201. ? `${exportsType} exports`
  202. : "unknown exports (runtime-defined)"
  203. )}\n`
  204. );
  205. if (exportsType) {
  206. const exportsInfo = moduleGraph.getExportsInfo(module);
  207. printExportsInfoToSource(
  208. source,
  209. "",
  210. exportsInfo,
  211. moduleGraph,
  212. requestShortener
  213. );
  214. }
  215. source.add(
  216. `${Template.toComment(
  217. `runtime requirements: ${joinIterableWithComma(
  218. chunkGraph.getModuleRuntimeRequirements(module, chunk.runtime)
  219. )}`
  220. )}\n`
  221. );
  222. const optimizationBailout =
  223. moduleGraph.getOptimizationBailout(module);
  224. if (optimizationBailout) {
  225. for (const text of optimizationBailout) {
  226. const code =
  227. typeof text === "function" ? text(requestShortener) : text;
  228. source.add(`${Template.toComment(`${code}`)}\n`);
  229. }
  230. }
  231. source.add(moduleSource);
  232. return source;
  233. }
  234. source.add(moduleSource);
  235. const cachedSource = new CachedSource(source);
  236. cacheEntry.full.set(moduleSource, cachedSource);
  237. return cachedSource;
  238. }
  239. );
  240. javascriptHooks.chunkHash.tap(PLUGIN_NAME, (_chunk, hash) => {
  241. hash.update(PLUGIN_NAME);
  242. hash.update("1");
  243. });
  244. const cssHooks = CssModulesPlugin.getCompilationHooks(compilation);
  245. cssHooks.renderModulePackage.tap(
  246. PLUGIN_NAME,
  247. (moduleSource, module, { runtimeTemplate }) => {
  248. const { requestShortener } = runtimeTemplate;
  249. /** @type {undefined | CacheEntry} */
  250. let cacheEntry;
  251. let cache = caches.get(requestShortener);
  252. if (cache === undefined) {
  253. caches.set(requestShortener, (cache = new WeakMap()));
  254. cache.set(
  255. module,
  256. (cacheEntry = { header: undefined, full: new WeakMap() })
  257. );
  258. } else {
  259. cacheEntry = cache.get(module);
  260. if (cacheEntry === undefined) {
  261. cache.set(
  262. module,
  263. (cacheEntry = { header: undefined, full: new WeakMap() })
  264. );
  265. } else if (!verbose) {
  266. const cachedSource = cacheEntry.full.get(moduleSource);
  267. if (cachedSource !== undefined) return cachedSource;
  268. }
  269. }
  270. const source = new ConcatSource();
  271. let header = cacheEntry.header;
  272. if (header === undefined) {
  273. header = this.generateHeader(module, requestShortener);
  274. cacheEntry.header = header;
  275. }
  276. source.add(header);
  277. source.add(moduleSource);
  278. const cachedSource = new CachedSource(source);
  279. cacheEntry.full.set(moduleSource, cachedSource);
  280. return cachedSource;
  281. }
  282. );
  283. cssHooks.chunkHash.tap(PLUGIN_NAME, (_chunk, hash) => {
  284. hash.update(PLUGIN_NAME);
  285. hash.update("1");
  286. });
  287. });
  288. }
  289. /**
  290. * @param {Module} module the module
  291. * @param {RequestShortener} requestShortener request shortener
  292. * @returns {RawSource} the header
  293. */
  294. generateHeader(module, requestShortener) {
  295. const req = module.readableIdentifier(requestShortener);
  296. const reqStr = req.replace(/\*\//g, "*_/");
  297. const reqStrStar = "*".repeat(reqStr.length);
  298. const headerStr = `/*!****${reqStrStar}****!*\\\n !*** ${reqStr} ***!\n \\****${reqStrStar}****/\n`;
  299. return new RawSource(headerStr);
  300. }
  301. }
  302. module.exports = ModuleInfoHeaderPlugin;