cli-plugin.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. class CLIPlugin {
  4. logger;
  5. options;
  6. constructor(options) {
  7. this.options = options;
  8. }
  9. async setupBundleAnalyzerPlugin(compiler) {
  10. // @ts-expect-error No types right now
  11. const { BundleAnalyzerPlugin } = (await import("webpack-bundle-analyzer")).default;
  12. const bundleAnalyzerPlugin = compiler.options.plugins.some((plugin) => plugin instanceof BundleAnalyzerPlugin);
  13. if (!bundleAnalyzerPlugin) {
  14. new BundleAnalyzerPlugin().apply(compiler);
  15. }
  16. }
  17. static #progressStates = [];
  18. setupProgressPlugin(compiler) {
  19. const { ProgressPlugin } = compiler.webpack;
  20. const progressPlugin = compiler.options.plugins.some((plugin) => plugin instanceof ProgressPlugin);
  21. if (progressPlugin) {
  22. return;
  23. }
  24. const isProfile = this.options.progress === "profile";
  25. const options = {
  26. profile: isProfile,
  27. };
  28. if (this.options.isMultiCompiler && ProgressPlugin.createDefaultHandler) {
  29. const handler = ProgressPlugin.createDefaultHandler(isProfile, compiler.getInfrastructureLogger("webpack.Progress"));
  30. const idx = CLIPlugin.#progressStates.length;
  31. CLIPlugin.#progressStates[idx] = [0];
  32. options.handler = (progress, msg, ...args) => {
  33. CLIPlugin.#progressStates[idx] = [progress, msg, ...args];
  34. let sum = 0;
  35. for (const [progress] of CLIPlugin.#progressStates) {
  36. sum += progress;
  37. }
  38. handler(sum / CLIPlugin.#progressStates.length, `[${compiler.name || idx}] ${msg}`, ...args);
  39. };
  40. }
  41. new ProgressPlugin(options).apply(compiler);
  42. }
  43. setupHelpfulOutput(compiler) {
  44. const pluginName = "webpack-cli";
  45. const getCompilationName = () => (compiler.name ? ` '${compiler.name}'` : "");
  46. const logCompilation = (message) => {
  47. if (process.env.WEBPACK_CLI_START_FINISH_FORCE_LOG) {
  48. process.stderr.write(message);
  49. }
  50. else {
  51. this.logger.log(message);
  52. }
  53. };
  54. const { configPath } = this.options;
  55. compiler.hooks.run.tap(pluginName, () => {
  56. const name = getCompilationName();
  57. logCompilation(`Compiler${name} starting... `);
  58. if (configPath) {
  59. this.logger.log(`Compiler${name} is using config: ${configPath.map((path) => `'${path}'`).join(", ")}`);
  60. }
  61. });
  62. compiler.hooks.watchRun.tap(pluginName, (compiler) => {
  63. const { bail, watch } = compiler.options;
  64. if (bail && watch) {
  65. this.logger.warn('You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.');
  66. }
  67. const name = getCompilationName();
  68. logCompilation(`Compiler${name} starting... `);
  69. if (configPath) {
  70. this.logger.log(`Compiler${name} is using config: '${configPath}'`);
  71. }
  72. });
  73. compiler.hooks.invalid.tap(pluginName, (filename, changeTime) => {
  74. const date = new Date(changeTime);
  75. this.logger.log(`File '${filename}' was modified`);
  76. this.logger.log(`Changed time is ${date} (timestamp is ${changeTime})`);
  77. });
  78. compiler.hooks.afterDone.tap(pluginName, () => {
  79. const name = getCompilationName();
  80. logCompilation(`Compiler${name} finished`);
  81. process.nextTick(() => {
  82. if (compiler.watchMode) {
  83. this.logger.log(`Compiler${name} is watching files for updates...`);
  84. }
  85. });
  86. });
  87. }
  88. apply(compiler) {
  89. this.logger = compiler.getInfrastructureLogger("webpack-cli");
  90. if (this.options.progress) {
  91. this.setupProgressPlugin(compiler);
  92. }
  93. if (this.options.analyze) {
  94. this.setupBundleAnalyzerPlugin(compiler);
  95. }
  96. this.setupHelpfulOutput(compiler);
  97. }
  98. }
  99. exports.default = CLIPlugin;