-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileListPlugin.js
39 lines (32 loc) · 1.03 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
class FileListPlugin {
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 = '{\n'
// Loop through all compiled assets,
// adding a new line item for each filename.
let i = 0
for (let filename in compilation.assets) {
let chunk = compilation.chunks[i]
if (chunk && chunk.name) {
filelist += `\t"${chunk.name}":"${filename}"` + ',\n'
}
i++
}
filelist = filelist.replace(/,\s*$/, '\n')
filelist += '}'
// Insert this list into the webpack build as a new file asset:
compilation.assets['_filelist.json'] = {
source: function() {
return filelist
},
size: function() {
return filelist.length
},
}
callback()
})
}
}
module.exports = FileListPlugin