BundleAnalyzerPlugin.js
5.75 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
'use strict';
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var bfj = require('bfj-node4');
var path = require('path');
var mkdir = require('mkdirp');
var _require = require('chalk'),
bold = _require.bold;
var Logger = require('./Logger');
var viewer = require('./viewer');
var BundleAnalyzerPlugin = function () {
function BundleAnalyzerPlugin(opts) {
_classCallCheck(this, BundleAnalyzerPlugin);
this.opts = Object.assign({
analyzerMode: 'server',
analyzerHost: '127.0.0.1',
analyzerPort: 8888,
reportFilename: 'report.html',
defaultSizes: 'parsed',
openAnalyzer: true,
generateStatsFile: false,
statsFilename: 'stats.json',
statsOptions: null,
excludeAssets: null,
logLevel: 'info',
// deprecated
startAnalyzer: true
}, opts);
this.server = null;
this.logger = new Logger(this.opts.logLevel);
}
_createClass(BundleAnalyzerPlugin, [{
key: 'apply',
value: function apply(compiler) {
var _this = this;
this.compiler = compiler;
var done = function done(stats) {
stats = stats.toJson(_this.opts.statsOptions);
var actions = [];
if (_this.opts.generateStatsFile) {
actions.push(function () {
return _this.generateStatsFile(stats);
});
}
// Handling deprecated `startAnalyzer` flag
if (_this.opts.analyzerMode === 'server' && !_this.opts.startAnalyzer) {
_this.opts.analyzerMode = 'disabled';
}
if (_this.opts.analyzerMode === 'server') {
actions.push(function () {
return _this.startAnalyzerServer(stats);
});
} else if (_this.opts.analyzerMode === 'static') {
actions.push(function () {
return _this.generateStaticReport(stats);
});
}
if (actions.length) {
// Making analyzer logs to be after all webpack logs in the console
setImmediate(function () {
actions.forEach(function (action) {
return action();
});
});
}
};
if (compiler.hooks) {
compiler.hooks.done.tap('webpack-bundle-analyzer', done);
} else {
compiler.plugin('done', done);
}
}
}, {
key: 'generateStatsFile',
value: function () {
var _ref = _asyncToGenerator(function* (stats) {
var statsFilepath = path.resolve(this.compiler.outputPath, this.opts.statsFilename);
mkdir.sync(path.dirname(statsFilepath));
try {
yield bfj.write(statsFilepath, stats, {
space: 2,
promises: 'ignore',
buffers: 'ignore',
maps: 'ignore',
iterables: 'ignore',
circular: 'ignore'
});
this.logger.info(`${bold('Webpack Bundle Analyzer')} saved stats file to ${bold(statsFilepath)}`);
} catch (error) {
this.logger.error(`${bold('Webpack Bundle Analyzer')} error saving stats file to ${bold(statsFilepath)}: ${error}`);
}
});
function generateStatsFile(_x) {
return _ref.apply(this, arguments);
}
return generateStatsFile;
}()
}, {
key: 'startAnalyzerServer',
value: function () {
var _ref2 = _asyncToGenerator(function* (stats) {
if (this.server) {
(yield this.server).updateChartData(stats);
} else {
this.server = viewer.startServer(stats, {
openBrowser: this.opts.openAnalyzer,
host: this.opts.analyzerHost,
port: this.opts.analyzerPort,
bundleDir: this.getBundleDirFromCompiler(),
logger: this.logger,
defaultSizes: this.opts.defaultSizes,
excludeAssets: this.opts.excludeAssets
});
}
});
function startAnalyzerServer(_x2) {
return _ref2.apply(this, arguments);
}
return startAnalyzerServer;
}()
}, {
key: 'generateStaticReport',
value: function generateStaticReport(stats) {
viewer.generateReport(stats, {
openBrowser: this.opts.openAnalyzer,
reportFilename: path.resolve(this.compiler.outputPath, this.opts.reportFilename),
bundleDir: this.getBundleDirFromCompiler(),
logger: this.logger,
defaultSizes: this.opts.defaultSizes,
excludeAssets: this.opts.excludeAssets
});
}
}, {
key: 'getBundleDirFromCompiler',
value: function getBundleDirFromCompiler() {
return this.compiler.outputFileSystem.constructor.name === 'MemoryFileSystem' ? null : this.compiler.outputPath;
}
}]);
return BundleAnalyzerPlugin;
}();
module.exports = BundleAnalyzerPlugin;