Skip to content

Commit

Permalink
Merge pull request #2 from JackXuyi/feature/plugin
Browse files Browse the repository at this point in the history
Feature/plugin
  • Loading branch information
JackXuyi committed Nov 16, 2020
2 parents dee1528 + 21b4b45 commit bcc86ba
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,5 @@ module.exports = {
## 说明

此方案的原理是通过 loader 收集项目中使用的资源信息,然后再通过文件递归的方式检测是否使用,所以整个项目需要编译一遍,耗时可能较长

- 部分资源是在 loader 中直接调用 emitFile 生成的文件,不经过 webpack 的其它流程,所以无法在插件中获取源文件路径
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zhijianren/develop-plugin",
"version": "0.0.2-beta.5",
"version": "0.0.3",
"description": "开发过程中项目优化",
"main": "./lib/index.js",
"scripts": {
Expand All @@ -25,9 +25,13 @@
"LICENSE",
".gitignore",
"tsconfig.json",
"src",
"lib"
],
"keywords": [
"webpack",
"clean",
"assets"
],
"homepage": "https://github.com/JackXuyi/develop-plugin#readme",
"devDependencies": {
"@types/node": "^14.14.7",
Expand Down
50 changes: 38 additions & 12 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,31 @@ export interface IOptions {
const createPluginClass = (useAssets: Set<string>) => {
return class {
public options: IOptions
public assets: Set<string>
public assetsMap: { [key: string]: true } = {}
public files: Set<string>

constructor(options: IOptions) {
this.options = options
this.assets = useAssets
this.files = new Set()

this.getAssets()
}

public apply(compiler: any) {
compiler.plugin('shouldEmit', () => {
this.outputAssets()
const { outputAssets = false } = this.options
return outputAssets
})

compiler.plugin('done', () => {
// 遍历使用的文件
this.assets.forEach((file: string) => {
this.assetsMap[file] = true
})
this.outputAssets()
})
}

public getAssets() {
Expand All @@ -33,18 +47,22 @@ const createPluginClass = (useAssets: Set<string>) => {
const list = getFileList(path.resolve(pathname))
files.push(...list)
})
return files.filter((file) => test.test(file))
files.forEach((file) => {
if (test.test(file)) {
this.files.add(file)
}
})
}

public outputAssets() {
const { output, isDel } = this.options
if (output) {
const projectAssets = this.getAssets()
const useAssetsMap: { [key: string]: true } = {}
useAssets.forEach((key) => (useAssetsMap[key] = true), {})
const projectNoUseAssets = projectAssets.filter(
(file) => !useAssetsMap[file],
)
const projectNoUseAssets: string[] = []
this.files.forEach((file) => {
if (!this.assetsMap[file]) {
projectNoUseAssets.push(file)
}
})
if (isDel) {
projectNoUseAssets.forEach((filename) => {
if (delFile(filename)) {
Expand All @@ -53,10 +71,18 @@ const createPluginClass = (useAssets: Set<string>) => {
})
}
if (typeof output === 'boolean') {
console.log(
chalk.red('Project do not use images list'),
projectNoUseAssets,
)
if (projectNoUseAssets.length) {
console.log(
chalk.yellow(
`${chalk.red(
projectNoUseAssets.length,
)} files do not use, list is `,
),
projectNoUseAssets,
)
} else {
console.log(chalk.green('All assets is used'))
}
} else if (typeof output === 'function') {
output(projectNoUseAssets)
}
Expand Down

0 comments on commit bcc86ba

Please sign in to comment.