webpack-bundle-analyzer 3.6.1 → 3.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/README.md +4 -3
- package/lib/BundleAnalyzerPlugin.js +21 -6
- package/lib/bin/analyzer.js +15 -5
- package/lib/viewer.js +26 -1
- package/package.json +1 -1
- package/public/viewer.js +2 -2
- package/public/viewer.js.map +1 -1
- package/src/BundleAnalyzerPlugin.js +13 -3
- package/src/bin/analyzer.js +17 -8
- package/src/viewer.js +16 -4
|
@@ -7,12 +7,11 @@ const Logger = require('./Logger');
|
|
|
7
7
|
const viewer = require('./viewer');
|
|
8
8
|
|
|
9
9
|
class BundleAnalyzerPlugin {
|
|
10
|
-
|
|
11
10
|
constructor(opts = {}) {
|
|
12
11
|
this.opts = {
|
|
13
12
|
analyzerMode: 'server',
|
|
14
13
|
analyzerHost: '127.0.0.1',
|
|
15
|
-
reportFilename:
|
|
14
|
+
reportFilename: null,
|
|
16
15
|
defaultSizes: 'parsed',
|
|
17
16
|
openAnalyzer: true,
|
|
18
17
|
generateStatsFile: false,
|
|
@@ -51,6 +50,8 @@ class BundleAnalyzerPlugin {
|
|
|
51
50
|
actions.push(() => this.startAnalyzerServer(stats.toJson()));
|
|
52
51
|
} else if (this.opts.analyzerMode === 'static') {
|
|
53
52
|
actions.push(() => this.generateStaticReport(stats.toJson()));
|
|
53
|
+
} else if (this.opts.analyzerMode === 'json') {
|
|
54
|
+
actions.push(() => this.generateJSONReport(stats.toJson()));
|
|
54
55
|
}
|
|
55
56
|
|
|
56
57
|
if (actions.length) {
|
|
@@ -115,10 +116,19 @@ class BundleAnalyzerPlugin {
|
|
|
115
116
|
}
|
|
116
117
|
}
|
|
117
118
|
|
|
119
|
+
async generateJSONReport(stats) {
|
|
120
|
+
await viewer.generateJSONReport(stats, {
|
|
121
|
+
reportFilename: path.resolve(this.compiler.outputPath, this.opts.reportFilename || 'report.json'),
|
|
122
|
+
bundleDir: this.getBundleDirFromCompiler(),
|
|
123
|
+
logger: this.logger,
|
|
124
|
+
excludeAssets: this.opts.excludeAssets
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
|
|
118
128
|
async generateStaticReport(stats) {
|
|
119
129
|
await viewer.generateReport(stats, {
|
|
120
130
|
openBrowser: this.opts.openAnalyzer,
|
|
121
|
-
reportFilename: path.resolve(this.compiler.outputPath, this.opts.reportFilename),
|
|
131
|
+
reportFilename: path.resolve(this.compiler.outputPath, this.opts.reportFilename || 'report.html'),
|
|
122
132
|
bundleDir: this.getBundleDirFromCompiler(),
|
|
123
133
|
logger: this.logger,
|
|
124
134
|
defaultSizes: this.opts.defaultSizes,
|
package/src/bin/analyzer.js
CHANGED
|
@@ -26,9 +26,10 @@ const program = commander
|
|
|
26
26
|
)
|
|
27
27
|
.option(
|
|
28
28
|
'-m, --mode <mode>',
|
|
29
|
-
'Analyzer mode. Should be `server` or `
|
|
30
|
-
|
|
31
|
-
|
|
29
|
+
'Analyzer mode. Should be `server`,`static` or `json`.' +
|
|
30
|
+
br('In `server` mode analyzer will start HTTP server to show bundle report.') +
|
|
31
|
+
br('In `static` mode single HTML file with bundle report will be generated.') +
|
|
32
|
+
br('In `json` mode single JSON file with bundle report will be generated.'),
|
|
32
33
|
'server'
|
|
33
34
|
)
|
|
34
35
|
.option(
|
|
@@ -45,8 +46,7 @@ const program = commander
|
|
|
45
46
|
)
|
|
46
47
|
.option(
|
|
47
48
|
'-r, --report <file>',
|
|
48
|
-
'Path to bundle report file that will be generated in `static` mode.'
|
|
49
|
-
'report.html'
|
|
49
|
+
'Path to bundle report file that will be generated in `static` mode.'
|
|
50
50
|
)
|
|
51
51
|
.option(
|
|
52
52
|
'-s, --default-sizes <type>',
|
|
@@ -86,7 +86,9 @@ let {
|
|
|
86
86
|
const logger = new Logger(logLevel);
|
|
87
87
|
|
|
88
88
|
if (!bundleStatsFile) showHelp('Provide path to Webpack Stats file as first argument');
|
|
89
|
-
if (mode !== 'server' && mode !== 'static'
|
|
89
|
+
if (mode !== 'server' && mode !== 'static' && mode !== 'json') {
|
|
90
|
+
showHelp('Invalid mode. Should be either `server`, `static` or `json`.');
|
|
91
|
+
}
|
|
90
92
|
if (mode === 'server') {
|
|
91
93
|
if (!host) showHelp('Invalid host name');
|
|
92
94
|
|
|
@@ -118,15 +120,22 @@ if (mode === 'server') {
|
|
|
118
120
|
excludeAssets,
|
|
119
121
|
logger: new Logger(logLevel)
|
|
120
122
|
});
|
|
121
|
-
} else {
|
|
123
|
+
} else if (mode === 'static') {
|
|
122
124
|
viewer.generateReport(bundleStats, {
|
|
123
125
|
openBrowser,
|
|
124
|
-
reportFilename: resolve(reportFilename),
|
|
126
|
+
reportFilename: resolve(reportFilename || 'report.html'),
|
|
125
127
|
defaultSizes,
|
|
126
128
|
bundleDir,
|
|
127
129
|
excludeAssets,
|
|
128
130
|
logger: new Logger(logLevel)
|
|
129
131
|
});
|
|
132
|
+
} else if (mode === 'json') {
|
|
133
|
+
viewer.generateJSONReport(bundleStats, {
|
|
134
|
+
reportFilename: resolve(reportFilename || 'report.json'),
|
|
135
|
+
bundleDir,
|
|
136
|
+
excludeAssets,
|
|
137
|
+
logger: new Logger(logLevel)
|
|
138
|
+
});
|
|
130
139
|
}
|
|
131
140
|
|
|
132
141
|
function showHelp(error) {
|
package/src/viewer.js
CHANGED
|
@@ -20,6 +20,7 @@ const assetsRoot = path.join(projectRoot, 'public');
|
|
|
20
20
|
module.exports = {
|
|
21
21
|
startServer,
|
|
22
22
|
generateReport,
|
|
23
|
+
generateJSONReport,
|
|
23
24
|
// deprecated
|
|
24
25
|
start: startServer
|
|
25
26
|
};
|
|
@@ -121,7 +122,7 @@ async function startServer(bundleStats, opts) {
|
|
|
121
122
|
async function generateReport(bundleStats, opts) {
|
|
122
123
|
const {
|
|
123
124
|
openBrowser = true,
|
|
124
|
-
reportFilename
|
|
125
|
+
reportFilename,
|
|
125
126
|
bundleDir = null,
|
|
126
127
|
logger = new Logger(),
|
|
127
128
|
defaultSizes = 'parsed',
|
|
@@ -158,9 +159,7 @@ async function generateReport(bundleStats, opts) {
|
|
|
158
159
|
mkdir.sync(path.dirname(reportFilepath));
|
|
159
160
|
fs.writeFileSync(reportFilepath, reportHtml);
|
|
160
161
|
|
|
161
|
-
logger.info(
|
|
162
|
-
`${bold('Webpack Bundle Analyzer')} saved report to ${bold(reportFilepath)}`
|
|
163
|
-
);
|
|
162
|
+
logger.info(`${bold('Webpack Bundle Analyzer')} saved report to ${bold(reportFilepath)}`);
|
|
164
163
|
|
|
165
164
|
if (openBrowser) {
|
|
166
165
|
opener(`file://${reportFilepath}`);
|
|
@@ -174,6 +173,19 @@ async function generateReport(bundleStats, opts) {
|
|
|
174
173
|
});
|
|
175
174
|
}
|
|
176
175
|
|
|
176
|
+
async function generateJSONReport(bundleStats, opts) {
|
|
177
|
+
const {reportFilename, bundleDir = null, logger = new Logger(), excludeAssets = null} = opts || {};
|
|
178
|
+
|
|
179
|
+
const chartData = getChartData({logger, excludeAssets}, bundleStats, bundleDir);
|
|
180
|
+
|
|
181
|
+
if (!chartData) return;
|
|
182
|
+
|
|
183
|
+
mkdir.sync(path.dirname(reportFilename));
|
|
184
|
+
fs.writeFileSync(reportFilename, JSON.stringify(chartData));
|
|
185
|
+
|
|
186
|
+
logger.info(`${bold('Webpack Bundle Analyzer')} saved JSON report to ${bold(reportFilename)}`);
|
|
187
|
+
}
|
|
188
|
+
|
|
177
189
|
function getAssetContent(filename) {
|
|
178
190
|
const assetPath = path.join(assetsRoot, filename);
|
|
179
191
|
|