Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,31 @@ const UselessFile = require('useless-files-webpack-plugin')
plugins: [
new UselessFile({
root: './src', // 项目目录
out?: './fileList.json', // 输出文件列表
out?: (files) => deal(files), // 或者回调处理
clean?: false // 删除文件,
exclude?: path // 排除文件列表, 格式为文件路径数组
output?: './uselessFile.json', // 输出文件列表
output?: (files) => deal(files), // 或者回调处理
clean?: false // 删除文件:true 执行会删除无用的文件; false不处理,默认为false
exclude?: path // 数组
exclude?: path // 字符串,文件路径,文件内容须为数组
})
]

```
### Example 1
```
new UselessFile({
root: './src',
output: './uselessFile.json',
clean: false,
exclude: ['src/view/unusedFile']
})
```
### Example 2
```
new UselessFile({
root: './src',
output: (files) => deal(files),
clean: false,
exclude: path.join(__dirname, '../exclude.json') // json文件内容为数组
})
```

21 changes: 18 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,23 @@ class CleanUnusedFilesPlugin {
}

dealExclude (path, unusedList) {
const file = fs.readFileSync(path, 'utf-8')
const files = JSON.parse(file) || []
let files = []
if(Array.isArray(path)) {
// 兼容传入的是数组路径
files = path
} else if(typeof path == 'string') {
try{
const file = fs.readFileSync(path, 'utf-8')
files = JSON.parse(file) || []
} catch(error) {
console.log(error)
}
}

if(process.platform == 'win32') {
// windows 需要兼容路径 D:\\workspace\\src\\App.vue
unusedList = unusedList.map(item => item.replace(/\\/g, '/'))
}
const result = unusedList.filter(unused => {
return !files.some(item => ~unused.indexOf(item))
})
Expand All @@ -68,7 +83,7 @@ class CleanUnusedFilesPlugin {
const allFiles = await this.getAllFiles(pattern)
let unUsed = allFiles
.filter(item => !~allChunks.indexOf(item))
if (exclude && typeof exclude === 'string') {
if (exclude) {
unUsed = this.dealExclude(exclude, unUsed)
}
if (typeof output === 'string') {
Expand Down