ModuleInfoHeaderPlugin.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. 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("ModuleInfoHeaderPlugin", compilation => {
  154. const javascriptHooks =
  155. JavascriptModulesPlugin.getCompilationHooks(compilation);
  156. javascriptHooks.renderModulePackage.tap(
  157. "ModuleInfoHeaderPlugin",
  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(
  237. "ModuleInfoHeaderPlugin",
  238. (_chunk, hash) => {
  239. hash.update("ModuleInfoHeaderPlugin");
  240. hash.update("1");
  241. }
  242. );
  243. const cssHooks = CssModulesPlugin.getCompilationHooks(compilation);
  244. cssHooks.renderModulePackage.tap(
  245. "ModuleInfoHeaderPlugin",
  246. (moduleSource, module, { runtimeTemplate }) => {
  247. const { requestShortener } = runtimeTemplate;
  248. let cacheEntry;
  249. let cache = caches.get(requestShortener);
  250. if (cache === undefined) {
  251. caches.set(requestShortener, (cache = new WeakMap()));
  252. cache.set(
  253. module,
  254. (cacheEntry = { header: undefined, full: new WeakMap() })
  255. );
  256. } else {
  257. cacheEntry = cache.get(module);
  258. if (cacheEntry === undefined) {
  259. cache.set(
  260. module,
  261. (cacheEntry = { header: undefined, full: new WeakMap() })
  262. );
  263. } else if (!verbose) {
  264. const cachedSource = cacheEntry.full.get(moduleSource);
  265. if (cachedSource !== undefined) return cachedSource;
  266. }
  267. }
  268. const source = new ConcatSource();
  269. let header = cacheEntry.header;
  270. if (header === undefined) {
  271. header = this.generateHeader(module, requestShortener);
  272. cacheEntry.header = header;
  273. }
  274. source.add(header);
  275. source.add(moduleSource);
  276. const cachedSource = new CachedSource(source);
  277. cacheEntry.full.set(moduleSource, cachedSource);
  278. return cachedSource;
  279. }
  280. );
  281. cssHooks.chunkHash.tap("ModuleInfoHeaderPlugin", (_chunk, hash) => {
  282. hash.update("ModuleInfoHeaderPlugin");
  283. hash.update("1");
  284. });
  285. });
  286. }
  287. /**
  288. * @param {Module} module the module
  289. * @param {RequestShortener} requestShortener request shortener
  290. * @returns {RawSource} the header
  291. */
  292. generateHeader(module, requestShortener) {
  293. const req = module.readableIdentifier(requestShortener);
  294. const reqStr = req.replace(/\*\//g, "*_/");
  295. const reqStrStar = "*".repeat(reqStr.length);
  296. const headerStr = `/*!****${reqStrStar}****!*\\\n !*** ${reqStr} ***!\n \\****${reqStrStar}****/\n`;
  297. return new RawSource(headerStr);
  298. }
  299. }
  300. module.exports = ModuleInfoHeaderPlugin;