cli-plugin.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. "use strict";
  2. var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
  3. if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
  4. if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
  5. return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
  6. };
  7. var _a, _CLIPlugin_progressStates;
  8. Object.defineProperty(exports, "__esModule", { value: true });
  9. exports.CLIPlugin = void 0;
  10. class CLIPlugin {
  11. constructor(options) {
  12. this.options = options;
  13. }
  14. async setupBundleAnalyzerPlugin(compiler) {
  15. // eslint-disable-next-line node/no-extraneous-require,@typescript-eslint/no-var-requires
  16. const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
  17. const bundleAnalyzerPlugin = Boolean(compiler.options.plugins.find((plugin) => plugin instanceof BundleAnalyzerPlugin));
  18. if (!bundleAnalyzerPlugin) {
  19. new BundleAnalyzerPlugin().apply(compiler);
  20. }
  21. }
  22. setupProgressPlugin(compiler) {
  23. const { ProgressPlugin } = compiler.webpack || require("webpack");
  24. const progressPlugin = Boolean(compiler.options.plugins.find((plugin) => plugin instanceof ProgressPlugin));
  25. if (progressPlugin) {
  26. return;
  27. }
  28. const isProfile = this.options.progress === "profile";
  29. const options = {
  30. profile: isProfile,
  31. };
  32. if (this.options.isMultiCompiler && ProgressPlugin.createDefaultHandler) {
  33. const handler = ProgressPlugin.createDefaultHandler(isProfile, compiler.getInfrastructureLogger("webpack.Progress"));
  34. const idx = __classPrivateFieldGet(CLIPlugin, _a, "f", _CLIPlugin_progressStates).length;
  35. __classPrivateFieldGet(CLIPlugin, _a, "f", _CLIPlugin_progressStates)[idx] = [0];
  36. options.handler = (p, msg, ...args) => {
  37. __classPrivateFieldGet(CLIPlugin, _a, "f", _CLIPlugin_progressStates)[idx] = [p, msg, ...args];
  38. let sum = 0;
  39. for (const [p] of __classPrivateFieldGet(CLIPlugin, _a, "f", _CLIPlugin_progressStates)) {
  40. sum += p;
  41. }
  42. handler(sum / __classPrivateFieldGet(CLIPlugin, _a, "f", _CLIPlugin_progressStates).length, `[${compiler.name ? compiler.name : idx}] ${msg}`, ...args);
  43. };
  44. }
  45. new ProgressPlugin(options).apply(compiler);
  46. }
  47. setupHelpfulOutput(compiler) {
  48. const pluginName = "webpack-cli";
  49. const getCompilationName = () => (compiler.name ? `'${compiler.name}'` : "");
  50. const logCompilation = (message) => {
  51. if (process.env.WEBPACK_CLI_START_FINISH_FORCE_LOG) {
  52. process.stderr.write(message);
  53. }
  54. else {
  55. this.logger.log(message);
  56. }
  57. };
  58. const { configPath } = this.options;
  59. compiler.hooks.run.tap(pluginName, () => {
  60. const name = getCompilationName();
  61. logCompilation(`Compiler${name ? ` ${name}` : ""} starting... `);
  62. if (configPath) {
  63. this.logger.log(`Compiler${name ? ` ${name}` : ""} is using config: ${configPath
  64. .map((path) => `'${path}'`)
  65. .join(", ")}`);
  66. }
  67. });
  68. compiler.hooks.watchRun.tap(pluginName, (compiler) => {
  69. const { bail, watch } = compiler.options;
  70. if (bail && watch) {
  71. this.logger.warn('You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.');
  72. }
  73. const name = getCompilationName();
  74. logCompilation(`Compiler${name ? ` ${name}` : ""} starting... `);
  75. if (configPath) {
  76. this.logger.log(`Compiler${name ? ` ${name}` : ""} is using config: '${configPath}'`);
  77. }
  78. });
  79. compiler.hooks.invalid.tap(pluginName, (filename, changeTime) => {
  80. const date = new Date(changeTime);
  81. this.logger.log(`File '${filename}' was modified`);
  82. this.logger.log(`Changed time is ${date} (timestamp is ${changeTime})`);
  83. });
  84. (compiler.webpack ? compiler.hooks.afterDone : compiler.hooks.done).tap(pluginName, () => {
  85. const name = getCompilationName();
  86. logCompilation(`Compiler${name ? ` ${name}` : ""} finished`);
  87. process.nextTick(() => {
  88. if (compiler.watchMode) {
  89. this.logger.log(`Compiler${name ? `${name}` : ""} is watching files for updates...`);
  90. }
  91. });
  92. });
  93. }
  94. apply(compiler) {
  95. this.logger = compiler.getInfrastructureLogger("webpack-cli");
  96. if (this.options.progress) {
  97. this.setupProgressPlugin(compiler);
  98. }
  99. if (this.options.analyze) {
  100. this.setupBundleAnalyzerPlugin(compiler);
  101. }
  102. this.setupHelpfulOutput(compiler);
  103. }
  104. }
  105. exports.CLIPlugin = CLIPlugin;
  106. _a = CLIPlugin;
  107. _CLIPlugin_progressStates = { value: [] };
  108. module.exports = CLIPlugin;