ModuleInfoHeaderPlugin.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { ConcatSource, RawSource, CachedSource } = 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("./ModuleTemplate")} ModuleTemplate */
  19. /** @typedef {import("./RequestShortener")} RequestShortener */
  20. /**
  21. * @template T
  22. * @param {Iterable<T>} iterable iterable
  23. * @returns {string} joined with comma
  24. */
  25. const joinIterableWithComma = iterable => {
  26. // This is more performant than Array.from().join(", ")
  27. // as it doesn't create an array
  28. let str = "";
  29. let first = true;
  30. for (const item of iterable) {
  31. if (first) {
  32. first = false;
  33. } else {
  34. str += ", ";
  35. }
  36. str += item;
  37. }
  38. return str;
  39. };
  40. /**
  41. * @param {ConcatSource} source output
  42. * @param {string} indent spacing
  43. * @param {ExportsInfo} exportsInfo data
  44. * @param {ModuleGraph} moduleGraph moduleGraph
  45. * @param {RequestShortener} requestShortener requestShortener
  46. * @param {Set<ExportInfo>} alreadyPrinted deduplication set
  47. * @returns {void}
  48. */
  49. const printExportsInfoToSource = (
  50. source,
  51. indent,
  52. exportsInfo,
  53. moduleGraph,
  54. requestShortener,
  55. alreadyPrinted = new Set()
  56. ) => {
  57. const otherExportsInfo = exportsInfo.otherExportsInfo;
  58. let alreadyPrintedExports = 0;
  59. // determine exports to print
  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. /** @type {WeakMap<RequestShortener, WeakMap<Module, { header: RawSource | undefined, full: WeakMap<Source, CachedSource> }>>} */
  139. const caches = new WeakMap();
  140. const PLUGIN_NAME = "ModuleInfoHeaderPlugin";
  141. class ModuleInfoHeaderPlugin {
  142. /**
  143. * @param {boolean=} verbose add more information like exports, runtime requirements and bailouts
  144. */
  145. constructor(verbose = true) {
  146. this._verbose = verbose;
  147. }
  148. /**
  149. * @param {Compiler} compiler the compiler
  150. * @returns {void}
  151. */
  152. apply(compiler) {
  153. const { _verbose: verbose } = this;
  154. compiler.hooks.compilation.tap(PLUGIN_NAME, compilation => {
  155. const javascriptHooks =
  156. JavascriptModulesPlugin.getCompilationHooks(compilation);
  157. javascriptHooks.renderModulePackage.tap(
  158. PLUGIN_NAME,
  159. (
  160. moduleSource,
  161. module,
  162. { chunk, chunkGraph, moduleGraph, runtimeTemplate }
  163. ) => {
  164. const { requestShortener } = runtimeTemplate;
  165. let cacheEntry;
  166. let cache = caches.get(requestShortener);
  167. if (cache === undefined) {
  168. caches.set(requestShortener, (cache = new WeakMap()));
  169. cache.set(
  170. module,
  171. (cacheEntry = { header: undefined, full: new WeakMap() })
  172. );
  173. } else {
  174. cacheEntry = cache.get(module);
  175. if (cacheEntry === undefined) {
  176. cache.set(
  177. module,
  178. (cacheEntry = { header: undefined, full: new WeakMap() })
  179. );
  180. } else if (!verbose) {
  181. const cachedSource = cacheEntry.full.get(moduleSource);
  182. if (cachedSource !== undefined) return cachedSource;
  183. }
  184. }
  185. const source = new ConcatSource();
  186. let header = cacheEntry.header;
  187. if (header === undefined) {
  188. header = this.generateHeader(module, requestShortener);
  189. cacheEntry.header = header;
  190. }
  191. source.add(header);
  192. if (verbose) {
  193. const exportsType = /** @type {BuildMeta} */ (module.buildMeta)
  194. .exportsType;
  195. source.add(
  196. `${Template.toComment(
  197. exportsType
  198. ? `${exportsType} exports`
  199. : "unknown exports (runtime-defined)"
  200. )}\n`
  201. );
  202. if (exportsType) {
  203. const exportsInfo = moduleGraph.getExportsInfo(module);
  204. printExportsInfoToSource(
  205. source,
  206. "",
  207. exportsInfo,
  208. moduleGraph,
  209. requestShortener
  210. );
  211. }
  212. source.add(
  213. `${Template.toComment(
  214. `runtime requirements: ${joinIterableWithComma(
  215. chunkGraph.getModuleRuntimeRequirements(module, chunk.runtime)
  216. )}`
  217. )}\n`
  218. );
  219. const optimizationBailout =
  220. moduleGraph.getOptimizationBailout(module);
  221. if (optimizationBailout) {
  222. for (const text of optimizationBailout) {
  223. const code =
  224. typeof text === "function" ? text(requestShortener) : text;
  225. source.add(`${Template.toComment(`${code}`)}\n`);
  226. }
  227. }
  228. source.add(moduleSource);
  229. return source;
  230. }
  231. source.add(moduleSource);
  232. const cachedSource = new CachedSource(source);
  233. cacheEntry.full.set(moduleSource, cachedSource);
  234. return cachedSource;
  235. }
  236. );
  237. javascriptHooks.chunkHash.tap(PLUGIN_NAME, (_chunk, hash) => {
  238. hash.update(PLUGIN_NAME);
  239. hash.update("1");
  240. });
  241. const cssHooks = CssModulesPlugin.getCompilationHooks(compilation);
  242. cssHooks.renderModulePackage.tap(
  243. PLUGIN_NAME,
  244. (moduleSource, module, { runtimeTemplate }) => {
  245. const { requestShortener } = runtimeTemplate;
  246. let cacheEntry;
  247. let cache = caches.get(requestShortener);
  248. if (cache === undefined) {
  249. caches.set(requestShortener, (cache = new WeakMap()));
  250. cache.set(
  251. module,
  252. (cacheEntry = { header: undefined, full: new WeakMap() })
  253. );
  254. } else {
  255. cacheEntry = cache.get(module);
  256. if (cacheEntry === undefined) {
  257. cache.set(
  258. module,
  259. (cacheEntry = { header: undefined, full: new WeakMap() })
  260. );
  261. } else if (!verbose) {
  262. const cachedSource = cacheEntry.full.get(moduleSource);
  263. if (cachedSource !== undefined) return cachedSource;
  264. }
  265. }
  266. const source = new ConcatSource();
  267. let header = cacheEntry.header;
  268. if (header === undefined) {
  269. header = this.generateHeader(module, requestShortener);
  270. cacheEntry.header = header;
  271. }
  272. source.add(header);
  273. source.add(moduleSource);
  274. const cachedSource = new CachedSource(source);
  275. cacheEntry.full.set(moduleSource, cachedSource);
  276. return cachedSource;
  277. }
  278. );
  279. cssHooks.chunkHash.tap(PLUGIN_NAME, (_chunk, hash) => {
  280. hash.update(PLUGIN_NAME);
  281. hash.update("1");
  282. });
  283. });
  284. }
  285. /**
  286. * @param {Module} module the module
  287. * @param {RequestShortener} requestShortener request shortener
  288. * @returns {RawSource} the header
  289. */
  290. generateHeader(module, requestShortener) {
  291. const req = module.readableIdentifier(requestShortener);
  292. const reqStr = req.replace(/\*\//g, "*_/");
  293. const reqStrStar = "*".repeat(reqStr.length);
  294. const headerStr = `/*!****${reqStrStar}****!*\\\n !*** ${reqStr} ***!\n \\****${reqStrStar}****/\n`;
  295. return new RawSource(headerStr);
  296. }
  297. }
  298. module.exports = ModuleInfoHeaderPlugin;