forked from aikar/webpack-auto-clean-build-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
57 lines (51 loc) · 1.28 KB
/
index.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
* Copyright (c) (2017) - Aikar
*
* http://aikar.co
*
* @license MIT
*
*/
var fs = require("fs");
var path = require("path");
function WebpackAutoCleanBuildPlugin (path) {}
WebpackAutoCleanBuildPlugin.prototype = {
previousFiles: {},
varructor: WebpackAutoCleanBuildPlugin,
apply: function (compiler) {
var self = this;
compiler.plugin('after-emit', function (compilation, callback) {
var options = compiler.options;
var stats = compilation.getStats().toJson({
hash: true,
publicPath: true,
assets: true,
chunks: false,
modules: false,
source: false,
errorDetails: false,
timings: false
});
var assetsByChunkName = stats.assetsByChunkName;
var keys = Object.keys(assetsByChunkName);
for (var key in keys) {
if (!keys.hasOwnProperty(key)) continue;
key = keys[key];
var fileName = assetsByChunkName[key];
if (Array.isArray(fileName)) {
fileName = assetsByChunkName[key][0];
}
var file = path.resolve(options.output.path, fileName);
var prevFile = self.previousFiles[key];
if (prevFile && prevFile != file) {
fs.unlink(prevFile, (error) => {
console.log(error)
});
}
self.previousFiles[key] = file;
}
callback();
});
}
};
module.exports = WebpackAutoCleanBuildPlugin;