CLIPlugin.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. class CLIPlugin {
  2. constructor(options) {
  3. this.options = options;
  4. }
  5. setupHotPlugin(compiler) {
  6. const { HotModuleReplacementPlugin } = compiler.webpack || require("webpack");
  7. const hotModuleReplacementPlugin = Boolean(
  8. compiler.options.plugins.find((plugin) => plugin instanceof HotModuleReplacementPlugin),
  9. );
  10. if (!hotModuleReplacementPlugin) {
  11. new HotModuleReplacementPlugin().apply(compiler);
  12. }
  13. }
  14. setupPrefetchPlugin(compiler) {
  15. const { PrefetchPlugin } = compiler.webpack || require("webpack");
  16. new PrefetchPlugin(null, this.options.prefetch).apply(compiler);
  17. }
  18. async setupBundleAnalyzerPlugin(compiler) {
  19. // eslint-disable-next-line node/no-extraneous-require
  20. const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
  21. const bundleAnalyzerPlugin = Boolean(
  22. compiler.options.plugins.find((plugin) => plugin instanceof BundleAnalyzerPlugin),
  23. );
  24. if (!bundleAnalyzerPlugin) {
  25. new BundleAnalyzerPlugin().apply(compiler);
  26. }
  27. }
  28. setupProgressPlugin(compiler) {
  29. const { ProgressPlugin } = compiler.webpack || require("webpack");
  30. const progressPlugin = Boolean(
  31. compiler.options.plugins.find((plugin) => plugin instanceof ProgressPlugin),
  32. );
  33. if (!progressPlugin) {
  34. new ProgressPlugin({
  35. profile: this.options.progress === "profile",
  36. }).apply(compiler);
  37. }
  38. }
  39. setupHelpfulOutput(compiler) {
  40. const pluginName = "webpack-cli";
  41. const getCompilationName = () => (compiler.name ? `'${compiler.name}'` : "");
  42. const logCompilation = (message) => {
  43. if (process.env.WEBPACK_CLI_START_FINISH_FORCE_LOG) {
  44. process.stderr.write(message);
  45. } else {
  46. this.logger.log(message);
  47. }
  48. };
  49. const { configPath } = this.options;
  50. compiler.hooks.run.tap(pluginName, () => {
  51. const name = getCompilationName();
  52. logCompilation(`Compiler${name ? ` ${name}` : ""} starting... `);
  53. if (configPath) {
  54. this.logger.log(`Compiler${name ? ` ${name}` : ""} is using config: '${configPath}'`);
  55. }
  56. });
  57. compiler.hooks.watchRun.tap(pluginName, (compiler) => {
  58. const { bail, watch } = compiler.options;
  59. if (bail && watch) {
  60. this.logger.warn(
  61. 'You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.',
  62. );
  63. }
  64. const name = getCompilationName();
  65. logCompilation(`Compiler${name ? ` ${name}` : ""} starting... `);
  66. if (configPath) {
  67. this.logger.log(`Compiler${name ? ` ${name}` : ""} is using config: '${configPath}'`);
  68. }
  69. });
  70. compiler.hooks.invalid.tap(pluginName, (filename, changeTime) => {
  71. const date = new Date(changeTime * 1000);
  72. this.logger.log(`File '${filename}' was modified`);
  73. this.logger.log(`Changed time is ${date} (timestamp is ${changeTime})`);
  74. });
  75. (compiler.webpack ? compiler.hooks.afterDone : compiler.hooks.done).tap(pluginName, () => {
  76. const name = getCompilationName();
  77. logCompilation(`Compiler${name ? ` ${name}` : ""} finished`);
  78. process.nextTick(() => {
  79. if (compiler.watchMode) {
  80. this.logger.log(`Compiler${name ? `${name}` : ""} is watching files for updates...`);
  81. }
  82. });
  83. });
  84. }
  85. apply(compiler) {
  86. this.logger = compiler.getInfrastructureLogger("webpack-cli");
  87. if (this.options.progress) {
  88. this.setupProgressPlugin(compiler);
  89. }
  90. if (this.options.hot) {
  91. this.setupHotPlugin(compiler);
  92. }
  93. if (this.options.prefetch) {
  94. this.setupPrefetchPlugin(compiler);
  95. }
  96. if (this.options.analyze) {
  97. this.setupBundleAnalyzerPlugin(compiler);
  98. }
  99. this.setupHelpfulOutput(compiler);
  100. }
  101. }
  102. module.exports = CLIPlugin;