rsnext/server/build/plugins/unlink-file-plugin.js

30 lines
750 B
JavaScript
Raw Normal View History

2016-10-23 18:42:13 +02:00
import { join } from 'path'
import { unlink } from 'mz/fs'
import { IS_BUNDLED_PAGE } from '../../utils'
2016-10-23 18:42:13 +02:00
export default class UnlinkFilePlugin {
constructor () {
this.prevAssets = {}
}
apply (compiler) {
compiler.plugin('after-emit', (compilation, callback) => {
const removed = Object.keys(this.prevAssets)
.filter((a) => IS_BUNDLED_PAGE.test(a) && !compilation.assets[a])
2016-10-23 18:42:13 +02: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
}
}))
.then(() => callback(), callback)
2016-10-23 18:42:13 +02:00
})
}
}