forked from luoxue-victor/workflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webapck-plugin-copy.js
31 lines (28 loc) · 969 Bytes
/
webapck-plugin-copy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const fs = require('fs-extra')
const globby = require('globby')
class CopyDirWebpackPlugin {
constructor(options) {
this.options = options
}
apply(compiler) {
const opt = this.options
compiler.plugin('done', (stats) => {
if (process.env.NODE_ENV === 'production') {
(async () => {
const toFilesPath = await globby([`${opt.to}/**`, '!.git/**'])
toFilesPath.forEach(filePath => fs.removeSync(filePath))
const fromFilesPath = await globby([`${opt.from}/**`])
fromFilesPath.forEach(fromPath => {
const cachePath = fromPath
fromPath = fromPath.replace('dist', opt.to)
const dirpaths = fromPath.substring(0, fromPath.lastIndexOf('/'))
fs.mkdirpSync(dirpaths)
fs.copySync(cachePath, fromPath)
})
console.log(` 完成copy ${opt.from} to ${opt.to}`)
})()
}
})
}
}
module.exports = CopyDirWebpackPlugin