1、需求

实现一个输出文件夹大小的plugin;

2、配置插件

2.1 引入插件

image.png

2.2 实例化插件

image.png

3、插件源码

  1. const { statSync } = require('fs')
  2. const { resolve } = require('path')
  3. class BundleSizePlugin {
  4. constructor(options) {
  5. this.options = options;
  6. }
  7. apply(compiler) {
  8. const { limit } = this.options
  9. compiler.hooks.done.tap('BundleSizePlugin', (stats) => {
  10. const { path } = stats.compilation.outputOptions
  11. const bundlePath = resolve(path)
  12. const { size } = statSync(bundlePath)
  13. const bundleSize = size
  14. if (bundleSize < limit) {
  15. console.log('输出success-------bundleSize:', bundleSize, "\n limit:", limit, '小于限制大小')
  16. } else {
  17. console.error('输出error-------bundleSize:', bundleSize, "\n limit:", limit, '超出了限制大小')
  18. }
  19. })
  20. }
  21. }
  22. module.exports = BundleSizePlugin

4、实现效果

image.png