xiaofeihe

webpack plugin清理项目无用文件

背景

对于经过数次迭代,经过无数人接手的项目,项目中无用的功能,无用的文件肯定在项目中遍布。对于这样一个项目我们做些整理、升级也是很有必要的。清理这些无用文件我们就可以使用webpack plugin,因为webpack打包是根据依赖树,使用到的文件就会在依赖树对象中,未使用到的则不会出现。

插件

这里介绍两个插件:

useless-files-webpack-plugin

unused-files-webpack-plugin

使用也很简单:

1
npm i --save-dev useless-files-webpack-plugin
1
2
3
4
5
6
7
8
9
10
11
const UselessFile = require('useless-files-webpack-plugin')

plugins: [
new UselessFile({
root: './src', // 项目目录
out?: './fileList.json', // 输出文件列表
out?: (files) => deal(files), // 或者回调处理
clean?: false // 删除文件,
exclude?: path // 排除文件列表, 格式为文件路径数组
})
]

注意

会清理一些使用到的文件,可以按src、components等目录逐步清理,清理的时候运行项目进行一定的测试。