UNPKG

2.97 kBJavaScriptView Raw
1const fs = require('fs');
2const path = require('path');
3const mkdir = require('mkdirp');
4const { bold } = require('chalk');
5
6const Logger = require('./Logger');
7const viewer = require('./viewer');
8
9class BundleAnalyzerPlugin {
10
11 constructor(opts) {
12 this.opts = {
13 analyzerMode: 'server',
14 analyzerHost: '127.0.0.1',
15 analyzerPort: 8888,
16 reportFilename: 'report.html',
17 defaultSizes: 'parsed',
18 openAnalyzer: true,
19 generateStatsFile: false,
20 statsFilename: 'stats.json',
21 statsOptions: null,
22 logLevel: 'info',
23 // deprecated
24 startAnalyzer: true,
25 ...opts
26 };
27
28 this.server = null;
29 this.logger = new Logger(this.opts.logLevel);
30 }
31
32 apply(compiler) {
33 this.compiler = compiler;
34
35 compiler.plugin('done', stats => {
36 stats = stats.toJson(this.opts.statsOptions);
37
38 const actions = [];
39
40 if (this.opts.generateStatsFile) {
41 actions.push(() => this.generateStatsFile(stats));
42 }
43
44 // Handling deprecated `startAnalyzer` flag
45 if (this.opts.analyzerMode === 'server' && !this.opts.startAnalyzer) {
46 this.opts.analyzerMode = 'disabled';
47 }
48
49 if (this.opts.analyzerMode === 'server') {
50 actions.push(() => this.startAnalyzerServer(stats));
51 } else if (this.opts.analyzerMode === 'static') {
52 actions.push(() => this.generateStaticReport(stats));
53 }
54
55 if (actions.length) {
56 // Making analyzer logs to be after all webpack logs in the console
57 setImmediate(() => {
58 actions.forEach(action => action());
59 });
60 }
61 });
62 }
63
64 generateStatsFile(stats) {
65 const statsFilepath = path.resolve(this.compiler.outputPath, this.opts.statsFilename);
66
67 mkdir.sync(path.dirname(statsFilepath));
68
69 fs.writeFileSync(
70 statsFilepath,
71 JSON.stringify(stats, null, 2)
72 );
73
74 this.logger.info(
75 `${bold('Webpack Bundle Analyzer')} saved stats file to ${bold(statsFilepath)}`
76 );
77 }
78
79 async startAnalyzerServer(stats) {
80 if (this.server) {
81 (await this.server).updateChartData(stats);
82 } else {
83 this.server = viewer.startServer(stats, {
84 openBrowser: this.opts.openAnalyzer,
85 host: this.opts.analyzerHost,
86 port: this.opts.analyzerPort,
87 bundleDir: this.getBundleDirFromCompiler(),
88 logger: this.logger,
89 defaultSizes: this.opts.defaultSizes
90 });
91 }
92 }
93
94 generateStaticReport(stats) {
95 viewer.generateReport(stats, {
96 openBrowser: this.opts.openAnalyzer,
97 reportFilename: path.resolve(this.compiler.outputPath, this.opts.reportFilename),
98 bundleDir: this.getBundleDirFromCompiler(),
99 logger: this.logger,
100 defaultSizes: this.opts.defaultSizes
101 });
102 }
103
104 getBundleDirFromCompiler() {
105 return (this.compiler.outputFileSystem.constructor.name === 'MemoryFileSystem') ? null : this.compiler.outputPath;
106 }
107
108}
109
110module.exports = BundleAnalyzerPlugin;