forked from alihazemfarouk/webpack-bundle-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
144 lines (126 loc) · 4.33 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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;
const _compilation = function(compilation, callback) {
const failedModule = 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);
} else {
output.message = '';
}
self.writeOutput(compiler, output);
};
if (compilation.hooks){
const plugin = {name: 'BundleTrackerPlugin'};
compilation.hooks.failedModule.tap(plugin, failedModule);
} else {
compilation.plugin('failed-module', failedModule);
}
};
const compile = function(factory, callback) {
self.writeOutput(compiler, {status: 'compiling'});
};
const 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.forEach(function(chunk){
var files = Array.from(chunk.files).map(function(file){
var F = {name: file};
var publicPath = self.options.publicPath || compiler.options.output.publicPath;
if (publicPath) {
F.publicPath = publicPath + file;
}
if (compiler.options.output.path) {
F.path = path.join(compiler.options.output.path, file);
}
return F;
});
chunks[chunk.name] = files;
});
var output = {
status: 'done',
chunks: chunks
};
if (stats.compilation.entrypoints){
var entryPoints = {};
stats.compilation.entrypoints.forEach(function(value, entrypoint) {
entrypointsChunks = value.chunks.map(function(chunk) {
var files = Array.from(chunk.files).map(function(file){
var F = {name: file};
var publicPath = self.options.publicPath || compiler.options.output.publicPath;
if (publicPath) {
F.publicPath = publicPath + file;
}
if (compiler.options.output.path) {
F.path = path.join(compiler.options.output.path, file);
}
return F;
});
return files;
});
entryPoints[entrypoint] = entrypointsChunks;
});
output['entryPoints'] = entryPoints
}
if (self.options.logTime === true) {
output.startTime = stats.startTime;
output.endTime = stats.endTime;
}
self.writeOutput(compiler, output);
};
if (compiler.hooks) {
const plugin = {name: 'BundleTrackerPlugin'};
compiler.hooks.compilation.tap(plugin, _compilation);
compiler.hooks.compile.tap(plugin, compile);
compiler.hooks.done.tap(plugin, done);
} else {
compiler.plugin('compilation', _compilation);
compiler.plugin('compile', compile);
compiler.plugin('done', done);
}
};
Plugin.prototype.writeOutput = function(compiler, contents) {
var outputDir = this.options.path || '.';
var outputFilename = path.join(outputDir, this.options.filename || DEFAULT_OUTPUT_FILENAME);
var publicPath = this.options.publicPath || compiler.options.output.publicPath;
if (publicPath) {
contents.publicPath = 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;