forked from django-webpack/webpack-bundle-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
100 lines (86 loc) · 2.91 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
var path = require('path');
var fs = require('fs');
var stripAnsi = require('strip-ansi');
var mkdirp = require('mkdirp');
var extend = require('deep-extend');
var assets = {};
var DEFAULT_OUTPUT_FILENAME = 'webpack-stats.json';
var DEFAULT_LOG_TIME = false;
function Plugin(options) {
this.contents = {};
this.options = options || {};
this.options.filename = this.options.filename || DEFAULT_OUTPUT_FILENAME;
if (this.options.logTime === undefined) {
this.options.logTime = DEFAULT_LOG_TIME;
}
}
Plugin.prototype.apply = function(compiler) {
var self = this;
compiler.plugin('compilation', function(compilation, callback) {
compilation.plugin('failed-module', function(fail){
var output = {
status: 'error',
error: fail.error.name || 'unknown-error'
};
if (fail.error.module !== undefined) {
output.file = fail.error.module.userRequest;
}
if (fail.error.error !== undefined) {
output.message = stripAnsi(fail.error.error.codeFrame);
}
self.writeOutput(compiler, output);
});
});
compiler.plugin('compile', function(factory, callback) {
self.writeOutput(compiler, {status: 'compiling'});
});
compiler.plugin('done', function(stats){
if (stats.compilation.errors.length > 0) {
var error = stats.compilation.errors[0];
self.writeOutput(compiler, {
status: 'error',
error: error['name'] || 'unknown-error',
message: stripAnsi(error['message'])
});
return;
}
var chunks = {};
stats.compilation.chunks.map(function(chunk){
var files = chunk.files.map(function(file){
var F = {name: file};
if (compiler.options.output.publicPath) {
F.publicPath= compiler.options.output.publicPath + file;
}
if (compiler.options.output.path) {
F.path = path.join(compiler.options.output.path, file);
}
return F;
});
var chunkName = chunk.name || '_dynamic';
chunks[chunkName] = [].concat(chunks[chunkName] || [], files);
});
var output = {
status: 'done',
chunks: chunks
};
if (self.options.logTime === true) {
output.startTime = stats.startTime;
output.endTime = stats.endTime;
}
self.writeOutput(compiler, output);
});
};
Plugin.prototype.writeOutput = function(compiler, contents) {
var outputDir = this.options.path || '.';
var outputFilename = path.join(outputDir, this.options.filename || DEFAULT_OUTPUT_FILENAME);
if (compiler.options.output.publicPath) {
contents.publicPath = compiler.options.output.publicPath;
}
mkdirp.sync(path.dirname(outputFilename));
this.contents = extend(this.contents, contents);
fs.writeFileSync(
outputFilename,
JSON.stringify(this.contents, null, this.options.indent)
);
};
module.exports = Plugin;