-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack-firstPlugin.js
38 lines (33 loc) · 1.07 KB
/
webpack-firstPlugin.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
const { compilation } = require("webpack")
/**
* 描述:这是一个plugin
* 功能:在生成打包文件之前自动生成一个关于打包出来的文件的大小信息
*/
class FileListPlugin {
constructor(options) {
this.options = options
}
apply(compiler) {
// emit is asynchronous hook, tapping into it using tapAsync, you can use tapPromise/tap(synchronous) as well
compiler.hooks.emit.tapAsync('FileListPlugin', (compilation, callback) => {
// Create a header string for the generated file:
let fileList = 'In this build:\n\n'
// Loop through all compiled assets,
// adding a new line item for each filename.
for(let filename in compilation.assets) {
fileList += '- ' + filename + '\n'
}
// Insert this list into the webpack build as a new file asset:
compilation.assets['filelist.md'] = {
source: function() {
return fileList
},
size: function() {
return fileList.length
}
}
callback()
})
}
}
module.exports = FileListPlugin