2016-10-23 16:42:13 +00:00
|
|
|
import { join } from 'path'
|
2018-03-30 14:59:42 +00:00
|
|
|
import promisify from '../../lib/promisify'
|
|
|
|
import fs from 'fs'
|
2018-06-14 17:30:14 +00:00
|
|
|
import { IS_BUNDLED_PAGE_REGEX } from '../../lib/constants'
|
2016-10-23 16:42:13 +00:00
|
|
|
|
2018-03-30 14:59:42 +00:00
|
|
|
const unlink = promisify(fs.unlink)
|
|
|
|
|
2016-10-23 16:42:13 +00:00
|
|
|
export default class UnlinkFilePlugin {
|
|
|
|
constructor () {
|
|
|
|
this.prevAssets = {}
|
|
|
|
}
|
|
|
|
|
|
|
|
apply (compiler) {
|
|
|
|
compiler.plugin('after-emit', (compilation, callback) => {
|
|
|
|
const removed = Object.keys(this.prevAssets)
|
2018-06-14 17:30:14 +00:00
|
|
|
.filter((a) => IS_BUNDLED_PAGE_REGEX.test(a) && !compilation.assets[a])
|
2016-10-23 16:42:13 +00:00
|
|
|
|
|
|
|
this.prevAssets = compilation.assets
|
|
|
|
|
|
|
|
Promise.all(removed.map(async (f) => {
|
|
|
|
const path = join(compiler.outputPath, f)
|
|
|
|
try {
|
|
|
|
await unlink(path)
|
|
|
|
} catch (err) {
|
|
|
|
if (err.code === 'ENOENT') return
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}))
|
2018-03-27 18:11:03 +00:00
|
|
|
.then(() => callback(), callback)
|
2016-10-23 16:42:13 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|