CLIPlugin.js 4.2 KB

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