-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
75 lines (57 loc) · 1.79 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
function ChunkListWebpackPlugin(options) {
this.opts = {};
this.opts.output = options.output || 'chunks-list.json';
this.opts.groupByExtension = options.groupByExtension || false;
this.opts.extensions = options.extensions || false;
this.opts.externalChunks = options.externalChunks || [];
}
ChunkListWebpackPlugin.prototype.apply = function(compiler) {
var OPTS = this.opts;
compiler.plugin('emit', function(compilation, callback) {
var chunksList = [].concat(OPTS.externalChunks);
compilation.chunks.forEach(function(chunk) {
chunk.files.forEach(function(filename) {
var ext = getFileExtension(filename);
if (shouldIncludeFile(OPTS.extensions, ext)) {
chunksList.push(filename);
}
});
});
var result = prepareResult(chunksList, OPTS.groupByExtension);
var resultString = JSON.stringify(result);
compilation.assets[OPTS.output] = {
source: function() {
return new Buffer(resultString);
},
size: function() {
return Buffer.byteLength(resultString);
}
};
callback();
});
};
function prepareResult(list, groupByExtension) {
if (!groupByExtension) {
return list;
}
return list.reduce(function(acc, filename) {
var ext = getFileExtension(filename);
acc[ext] = acc[ext] || [];
acc[ext].push(filename);
return acc;
}, {});
}
function shouldIncludeFile (whitelist, extension) {
if (!whitelist) {
return true;
}
return whitelist && whitelist.indexOf(extension) > -1;
}
function getFileExtension (filename) {
var extension = filename.split('.').reverse()[0];
if (!extension.length) {
extension = null;
}
return extension;
}
module.exports = ChunkListWebpackPlugin;