DefaultStatsPresetPlugin.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const RequestShortener = require("../RequestShortener");
  7. /** @typedef {import("../../declarations/WebpackOptions").StatsOptions} StatsOptions */
  8. /** @typedef {import("../../declarations/WebpackOptions").StatsValue} StatsValue */
  9. /** @typedef {import("../Compilation")} Compilation */
  10. /** @typedef {import("../Compilation").CreateStatsOptionsContext} CreateStatsOptionsContext */
  11. /** @typedef {import("../Compilation").KnownNormalizedStatsOptions} KnownNormalizedStatsOptions */
  12. /** @typedef {import("../Compilation").NormalizedStatsOptions} NormalizedStatsOptions */
  13. /** @typedef {import("../Compiler")} Compiler */
  14. /** @typedef {import("./DefaultStatsFactoryPlugin").StatsError} StatsError */
  15. /**
  16. * @param {Partial<NormalizedStatsOptions>} options options
  17. * @param {StatsOptions} defaults default options
  18. */
  19. const applyDefaults = (options, defaults) => {
  20. for (const _k of Object.keys(defaults)) {
  21. const key = /** @type {keyof StatsOptions} */ (_k);
  22. if (typeof options[key] === "undefined") {
  23. options[/** @type {keyof NormalizedStatsOptions} */ (key)] =
  24. defaults[key];
  25. }
  26. }
  27. };
  28. /** @typedef {{ [Key in Exclude<StatsValue, boolean | object | "normal">]: StatsOptions }} NamedPresets */
  29. /** @type {NamedPresets} */
  30. const NAMED_PRESETS = {
  31. verbose: {
  32. hash: true,
  33. builtAt: true,
  34. relatedAssets: true,
  35. entrypoints: true,
  36. chunkGroups: true,
  37. ids: true,
  38. modules: false,
  39. chunks: true,
  40. chunkRelations: true,
  41. chunkModules: true,
  42. dependentModules: true,
  43. chunkOrigins: true,
  44. depth: true,
  45. env: true,
  46. reasons: true,
  47. usedExports: true,
  48. providedExports: true,
  49. optimizationBailout: true,
  50. errorDetails: true,
  51. errorStack: true,
  52. publicPath: true,
  53. logging: "verbose",
  54. orphanModules: true,
  55. runtimeModules: true,
  56. exclude: false,
  57. errorsSpace: Infinity,
  58. warningsSpace: Infinity,
  59. modulesSpace: Infinity,
  60. chunkModulesSpace: Infinity,
  61. assetsSpace: Infinity,
  62. reasonsSpace: Infinity,
  63. children: true
  64. },
  65. detailed: {
  66. hash: true,
  67. builtAt: true,
  68. relatedAssets: true,
  69. entrypoints: true,
  70. chunkGroups: true,
  71. ids: true,
  72. chunks: true,
  73. chunkRelations: true,
  74. chunkModules: false,
  75. chunkOrigins: true,
  76. depth: true,
  77. usedExports: true,
  78. providedExports: true,
  79. optimizationBailout: true,
  80. errorDetails: true,
  81. publicPath: true,
  82. logging: true,
  83. runtimeModules: true,
  84. exclude: false,
  85. errorsSpace: 1000,
  86. warningsSpace: 1000,
  87. modulesSpace: 1000,
  88. assetsSpace: 1000,
  89. reasonsSpace: 1000
  90. },
  91. minimal: {
  92. all: false,
  93. version: true,
  94. timings: true,
  95. modules: true,
  96. errorsSpace: 0,
  97. warningsSpace: 0,
  98. modulesSpace: 0,
  99. assets: true,
  100. assetsSpace: 0,
  101. errors: true,
  102. errorsCount: true,
  103. warnings: true,
  104. warningsCount: true,
  105. logging: "warn"
  106. },
  107. "errors-only": {
  108. all: false,
  109. errors: true,
  110. errorsCount: true,
  111. errorsSpace: Infinity,
  112. moduleTrace: true,
  113. logging: "error"
  114. },
  115. "errors-warnings": {
  116. all: false,
  117. errors: true,
  118. errorsCount: true,
  119. errorsSpace: Infinity,
  120. warnings: true,
  121. warningsCount: true,
  122. warningsSpace: Infinity,
  123. logging: "warn"
  124. },
  125. summary: {
  126. all: false,
  127. version: true,
  128. errorsCount: true,
  129. warningsCount: true
  130. },
  131. none: {
  132. all: false
  133. }
  134. };
  135. /**
  136. * @param {Partial<NormalizedStatsOptions>} all stats option
  137. * @returns {boolean} true when enabled, otherwise false
  138. */
  139. const NORMAL_ON = ({ all }) => all !== false;
  140. /**
  141. * @param {Partial<NormalizedStatsOptions>} all stats option
  142. * @returns {boolean} true when enabled, otherwise false
  143. */
  144. const NORMAL_OFF = ({ all }) => all === true;
  145. /**
  146. * @param {Partial<NormalizedStatsOptions>} all stats option
  147. * @param {CreateStatsOptionsContext} forToString stats options context
  148. * @returns {boolean} true when enabled, otherwise false
  149. */
  150. const ON_FOR_TO_STRING = ({ all }, { forToString }) =>
  151. forToString ? all !== false : all === true;
  152. /**
  153. * @param {Partial<NormalizedStatsOptions>} all stats option
  154. * @param {CreateStatsOptionsContext} forToString stats options context
  155. * @returns {boolean} true when enabled, otherwise false
  156. */
  157. const OFF_FOR_TO_STRING = ({ all }, { forToString }) =>
  158. forToString ? all === true : all !== false;
  159. /**
  160. * @param {Partial<NormalizedStatsOptions>} all stats option
  161. * @param {CreateStatsOptionsContext} forToString stats options context
  162. * @returns {boolean | "auto"} true when enabled, otherwise false
  163. */
  164. const AUTO_FOR_TO_STRING = ({ all }, { forToString }) => {
  165. if (all === false) return false;
  166. if (all === true) return true;
  167. if (forToString) return "auto";
  168. return true;
  169. };
  170. /** @typedef {keyof NormalizedStatsOptions} DefaultsKeys */
  171. /** @typedef {{ [Key in DefaultsKeys]: (options: Partial<NormalizedStatsOptions>, context: CreateStatsOptionsContext, compilation: Compilation) => NormalizedStatsOptions[Key] | RequestShortener }} Defaults */
  172. /** @type {Partial<Defaults>} */
  173. const DEFAULTS = {
  174. context: (options, context, compilation) => compilation.compiler.context,
  175. requestShortener: (options, context, compilation) =>
  176. compilation.compiler.context === options.context
  177. ? compilation.requestShortener
  178. : new RequestShortener(
  179. /** @type {string} */
  180. (options.context),
  181. compilation.compiler.root
  182. ),
  183. performance: NORMAL_ON,
  184. hash: OFF_FOR_TO_STRING,
  185. env: NORMAL_OFF,
  186. version: NORMAL_ON,
  187. timings: NORMAL_ON,
  188. builtAt: OFF_FOR_TO_STRING,
  189. assets: NORMAL_ON,
  190. entrypoints: AUTO_FOR_TO_STRING,
  191. chunkGroups: OFF_FOR_TO_STRING,
  192. chunkGroupAuxiliary: OFF_FOR_TO_STRING,
  193. chunkGroupChildren: OFF_FOR_TO_STRING,
  194. chunkGroupMaxAssets: (o, { forToString }) => (forToString ? 5 : Infinity),
  195. chunks: OFF_FOR_TO_STRING,
  196. chunkRelations: OFF_FOR_TO_STRING,
  197. chunkModules: ({ all, modules }) => {
  198. if (all === false) return false;
  199. if (all === true) return true;
  200. if (modules) return false;
  201. return true;
  202. },
  203. dependentModules: OFF_FOR_TO_STRING,
  204. chunkOrigins: OFF_FOR_TO_STRING,
  205. ids: OFF_FOR_TO_STRING,
  206. modules: ({ all, chunks, chunkModules }, { forToString }) => {
  207. if (all === false) return false;
  208. if (all === true) return true;
  209. if (forToString && chunks && chunkModules) return false;
  210. return true;
  211. },
  212. nestedModules: OFF_FOR_TO_STRING,
  213. groupModulesByType: ON_FOR_TO_STRING,
  214. groupModulesByCacheStatus: ON_FOR_TO_STRING,
  215. groupModulesByLayer: ON_FOR_TO_STRING,
  216. groupModulesByAttributes: ON_FOR_TO_STRING,
  217. groupModulesByPath: ON_FOR_TO_STRING,
  218. groupModulesByExtension: ON_FOR_TO_STRING,
  219. modulesSpace: (o, { forToString }) => (forToString ? 15 : Infinity),
  220. chunkModulesSpace: (o, { forToString }) => (forToString ? 10 : Infinity),
  221. nestedModulesSpace: (o, { forToString }) => (forToString ? 10 : Infinity),
  222. relatedAssets: OFF_FOR_TO_STRING,
  223. groupAssetsByEmitStatus: ON_FOR_TO_STRING,
  224. groupAssetsByInfo: ON_FOR_TO_STRING,
  225. groupAssetsByPath: ON_FOR_TO_STRING,
  226. groupAssetsByExtension: ON_FOR_TO_STRING,
  227. groupAssetsByChunk: ON_FOR_TO_STRING,
  228. assetsSpace: (o, { forToString }) => (forToString ? 15 : Infinity),
  229. orphanModules: OFF_FOR_TO_STRING,
  230. runtimeModules: ({ all, runtime }, { forToString }) =>
  231. runtime !== undefined
  232. ? runtime
  233. : forToString
  234. ? all === true
  235. : all !== false,
  236. cachedModules: ({ all, cached }, { forToString }) =>
  237. cached !== undefined ? cached : forToString ? all === true : all !== false,
  238. moduleAssets: OFF_FOR_TO_STRING,
  239. depth: OFF_FOR_TO_STRING,
  240. cachedAssets: OFF_FOR_TO_STRING,
  241. reasons: OFF_FOR_TO_STRING,
  242. reasonsSpace: (o, { forToString }) => (forToString ? 15 : Infinity),
  243. groupReasonsByOrigin: ON_FOR_TO_STRING,
  244. usedExports: OFF_FOR_TO_STRING,
  245. providedExports: OFF_FOR_TO_STRING,
  246. optimizationBailout: OFF_FOR_TO_STRING,
  247. children: OFF_FOR_TO_STRING,
  248. source: NORMAL_OFF,
  249. moduleTrace: NORMAL_ON,
  250. errors: NORMAL_ON,
  251. errorsCount: NORMAL_ON,
  252. errorDetails: AUTO_FOR_TO_STRING,
  253. errorStack: OFF_FOR_TO_STRING,
  254. warnings: NORMAL_ON,
  255. warningsCount: NORMAL_ON,
  256. publicPath: OFF_FOR_TO_STRING,
  257. logging: ({ all }, { forToString }) =>
  258. forToString && all !== false ? "info" : false,
  259. loggingDebug: () => [],
  260. loggingTrace: OFF_FOR_TO_STRING,
  261. excludeModules: () => [],
  262. excludeAssets: () => [],
  263. modulesSort: () => "depth",
  264. chunkModulesSort: () => "name",
  265. nestedModulesSort: () => false,
  266. chunksSort: () => false,
  267. assetsSort: () => "!size",
  268. outputPath: OFF_FOR_TO_STRING,
  269. colors: () => false
  270. };
  271. /**
  272. * @template {string} T
  273. * @param {string | ({ test: (value: T) => boolean }) | ((value: T, ...args: EXPECTED_ANY[]) => boolean) | boolean} item item to normalize
  274. * @returns {(value: T, ...args: EXPECTED_ANY[]) => boolean} normalize fn
  275. */
  276. const normalizeFilter = item => {
  277. if (typeof item === "string") {
  278. const regExp = new RegExp(
  279. `[\\\\/]${item.replace(/[-[\]{}()*+?.\\^$|]/g, "\\$&")}([\\\\/]|$|!|\\?)`
  280. );
  281. return ident => regExp.test(/** @type {T} */ (ident));
  282. }
  283. if (item && typeof item === "object" && typeof item.test === "function") {
  284. return ident => item.test(ident);
  285. }
  286. if (typeof item === "boolean") {
  287. return () => item;
  288. }
  289. return /** @type {(value: T, ...args: EXPECTED_ANY[]) => boolean} */ (item);
  290. };
  291. /** @typedef {keyof (KnownNormalizedStatsOptions | StatsOptions)} NormalizerKeys */
  292. /** @typedef {{ [Key in NormalizerKeys]: (value: StatsOptions[Key]) => KnownNormalizedStatsOptions[Key] }} Normalizers */
  293. /** @type {Partial<Normalizers>} */
  294. const NORMALIZER = {
  295. excludeModules: value => {
  296. if (!Array.isArray(value)) {
  297. value = value
  298. ? /** @type {KnownNormalizedStatsOptions["excludeModules"]} */ ([value])
  299. : [];
  300. }
  301. return value.map(normalizeFilter);
  302. },
  303. excludeAssets: value => {
  304. if (!Array.isArray(value)) {
  305. value = value ? [value] : [];
  306. }
  307. return value.map(normalizeFilter);
  308. },
  309. warningsFilter: value => {
  310. if (!Array.isArray(value)) {
  311. value = value ? [value] : [];
  312. }
  313. /**
  314. * @callback WarningFilterFn
  315. * @param {StatsError} warning warning
  316. * @param {string} warningString warning string
  317. * @returns {boolean} result
  318. */
  319. return value.map(
  320. /**
  321. * @param {StatsOptions["warningsFilter"]} filter a warning filter
  322. * @returns {WarningFilterFn} result
  323. */
  324. filter => {
  325. if (typeof filter === "string") {
  326. return (warning, warningString) => warningString.includes(filter);
  327. }
  328. if (filter instanceof RegExp) {
  329. return (warning, warningString) => filter.test(warningString);
  330. }
  331. if (typeof filter === "function") {
  332. return filter;
  333. }
  334. throw new Error(
  335. `Can only filter warnings with Strings or RegExps. (Given: ${filter})`
  336. );
  337. }
  338. );
  339. },
  340. logging: value => {
  341. if (value === true) value = "log";
  342. return /** @type {KnownNormalizedStatsOptions["logging"]} */ (value);
  343. },
  344. loggingDebug: value => {
  345. if (!Array.isArray(value)) {
  346. value = value
  347. ? /** @type {KnownNormalizedStatsOptions["loggingDebug"]} */ ([value])
  348. : [];
  349. }
  350. return value.map(normalizeFilter);
  351. }
  352. };
  353. class DefaultStatsPresetPlugin {
  354. /**
  355. * Apply the plugin
  356. * @param {Compiler} compiler the compiler instance
  357. * @returns {void}
  358. */
  359. apply(compiler) {
  360. compiler.hooks.compilation.tap("DefaultStatsPresetPlugin", compilation => {
  361. for (const key of Object.keys(NAMED_PRESETS)) {
  362. const defaults = NAMED_PRESETS[/** @type {keyof NamedPresets} */ (key)];
  363. compilation.hooks.statsPreset
  364. .for(key)
  365. .tap("DefaultStatsPresetPlugin", (options, context) => {
  366. applyDefaults(options, defaults);
  367. });
  368. }
  369. compilation.hooks.statsNormalize.tap(
  370. "DefaultStatsPresetPlugin",
  371. (options, context) => {
  372. for (const key of Object.keys(DEFAULTS)) {
  373. if (options[key] === undefined)
  374. options[key] =
  375. /** @type {Defaults[DefaultsKeys]} */
  376. (DEFAULTS[/** @type {DefaultsKeys} */ (key)])(
  377. options,
  378. context,
  379. compilation
  380. );
  381. }
  382. for (const key of Object.keys(NORMALIZER)) {
  383. options[key] =
  384. /** @type {TODO} */
  385. (NORMALIZER[/** @type {NormalizerKeys} */ (key)])(options[key]);
  386. }
  387. }
  388. );
  389. });
  390. }
  391. }
  392. module.exports = DefaultStatsPresetPlugin;