-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileListPlugin.js
54 lines (51 loc) · 1.85 KB
/
FileListPlugin.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
const fs = require('fs');
function FileListPlugin() {
// 使用配置(options)设置插件实例
}
FileListPlugin.prototype.apply = compiler => {
compiler.plugin('emit', (compilation, callback) => {
// console.log(compiler.hooks);
// console.log('compilation', compilation);
// console.log('compilation', compilation.chunks);
const stats = compilation.getStats().toJson();
fs.writeFile('./compilationStats.json', JSON.stringify(stats));
const allFiles = stats.assets.map(asset => asset.name);
const entryFiles = Object.keys(stats.entrypoints)
.map(entryName => stats.entrypoints[entryName].assets)
.reduce(
(accumulater, currentValue) => currentValue.concat(accumulater),
[],
);
const asyncFiles = allFiles.filter(
file => entryFiles.indexOf(file) < 0,
);
const entryJs = [];
const entryCss = [];
entryFiles.forEach(fileName => {
const fileType = fileName.substring(fileName.lastIndexOf('.'));
if (fileType === '.js') {
entryJs.push(fileName);
} else if (fileType === '.css') {
entryCss.push(fileName);
}
});
const clientFileList = JSON.stringify({
entryJs,
entryCss,
asyncFiles,
publicPath: stats.publicPath,
});
// console.log(compilation.assets['index.html'].source());
// 把它作为一个新的文件资源插入到 webpack 构建中:
compilation.assets['clientFileList.json'] = {
source() {
return clientFileList;
},
size() {
return clientFileList.length;
},
};
callback();
});
};
module.exports = FileListPlugin;