webpack-bundle-analyzer 3.9.0 → 4.2.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 +29 -0
- package/lib/BundleAnalyzerPlugin.js +55 -85
- package/lib/analyzer.js +78 -35
- package/lib/bin/analyzer.js +1 -3
- package/lib/parseUtils.js +83 -10
- package/lib/statsUtils.js +90 -0
- package/lib/template.js +73 -0
- package/lib/tree/BaseFolder.js +1 -3
- package/lib/tree/ConcatenatedModule.js +4 -12
- package/lib/tree/ContentFolder.js +2 -8
- package/lib/tree/ContentModule.js +2 -8
- package/lib/tree/Folder.js +3 -11
- package/lib/utils.js +19 -4
- package/lib/viewer.js +121 -184
- package/package.json +47 -49
- package/public/viewer.js +4 -14
- package/public/viewer.js.LICENSE.txt +5 -0
- package/public/viewer.js.map +1 -1
- package/src/BundleAnalyzerPlugin.js +4 -11
- package/src/analyzer.js +76 -31
- package/src/bin/analyzer.js +1 -2
- package/src/parseUtils.js +96 -11
- package/src/statsUtils.js +82 -0
- package/{views/viewer.ejs → src/template.js} +50 -7
- package/src/tree/BaseFolder.js +1 -1
- package/src/tree/ConcatenatedModule.js +2 -2
- package/src/tree/Folder.js +1 -1
- package/src/utils.js +16 -4
- package/src/viewer.js +27 -73
- package/views/script.ejs +0 -8
package/src/viewer.js
CHANGED
|
@@ -5,16 +5,14 @@ const http = require('http');
|
|
|
5
5
|
const WebSocket = require('ws');
|
|
6
6
|
const _ = require('lodash');
|
|
7
7
|
const express = require('express');
|
|
8
|
-
const ejs = require('ejs');
|
|
9
|
-
const opener = require('opener');
|
|
10
|
-
const mkdir = require('mkdirp');
|
|
11
8
|
const {bold} = require('chalk');
|
|
12
9
|
|
|
13
10
|
const Logger = require('./Logger');
|
|
14
11
|
const analyzer = require('./analyzer');
|
|
12
|
+
const {open} = require('./utils');
|
|
13
|
+
const {renderViewer} = require('./template');
|
|
15
14
|
|
|
16
15
|
const projectRoot = path.resolve(__dirname, '..');
|
|
17
|
-
const assetsRoot = path.join(projectRoot, 'public');
|
|
18
16
|
|
|
19
17
|
function resolveTitle(reportTitle) {
|
|
20
18
|
if (typeof reportTitle === 'function') {
|
|
@@ -51,24 +49,18 @@ async function startServer(bundleStats, opts) {
|
|
|
51
49
|
if (!chartData) return;
|
|
52
50
|
|
|
53
51
|
const app = express();
|
|
54
|
-
|
|
55
|
-
// Explicitly using our `ejs` dependency to render templates
|
|
56
|
-
// Fixes #17
|
|
57
|
-
app.engine('ejs', require('ejs').renderFile);
|
|
58
|
-
app.set('view engine', 'ejs');
|
|
59
|
-
app.set('views', `${projectRoot}/views`);
|
|
60
52
|
app.use(express.static(`${projectRoot}/public`));
|
|
61
53
|
|
|
62
|
-
app.
|
|
63
|
-
res.
|
|
54
|
+
app.get('/', (req, res) => {
|
|
55
|
+
res.writeHead(200, {'Content-Type': 'text/html'});
|
|
56
|
+
const html = renderViewer({
|
|
64
57
|
mode: 'server',
|
|
65
58
|
title: resolveTitle(reportTitle),
|
|
66
|
-
|
|
59
|
+
chartData,
|
|
67
60
|
defaultSizes,
|
|
68
|
-
enableWebSocket: true
|
|
69
|
-
// Helpers
|
|
70
|
-
escapeJson
|
|
61
|
+
enableWebSocket: true
|
|
71
62
|
});
|
|
63
|
+
return res.end(html);
|
|
72
64
|
});
|
|
73
65
|
|
|
74
66
|
const server = http.createServer(app);
|
|
@@ -85,7 +77,7 @@ async function startServer(bundleStats, opts) {
|
|
|
85
77
|
);
|
|
86
78
|
|
|
87
79
|
if (openBrowser) {
|
|
88
|
-
|
|
80
|
+
open(url, logger);
|
|
89
81
|
}
|
|
90
82
|
});
|
|
91
83
|
});
|
|
@@ -140,44 +132,23 @@ async function generateReport(bundleStats, opts) {
|
|
|
140
132
|
|
|
141
133
|
if (!chartData) return;
|
|
142
134
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
chartData,
|
|
150
|
-
defaultSizes,
|
|
151
|
-
enableWebSocket: false,
|
|
152
|
-
// Helpers
|
|
153
|
-
assetContent: getAssetContent,
|
|
154
|
-
escapeJson
|
|
155
|
-
},
|
|
156
|
-
(err, reportHtml) => {
|
|
157
|
-
try {
|
|
158
|
-
if (err) {
|
|
159
|
-
logger.error(err);
|
|
160
|
-
reject(err);
|
|
161
|
-
return;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
const reportFilepath = path.resolve(bundleDir || process.cwd(), reportFilename);
|
|
165
|
-
|
|
166
|
-
mkdir.sync(path.dirname(reportFilepath));
|
|
167
|
-
fs.writeFileSync(reportFilepath, reportHtml);
|
|
168
|
-
|
|
169
|
-
logger.info(`${bold('Webpack Bundle Analyzer')} saved report to ${bold(reportFilepath)}`);
|
|
170
|
-
|
|
171
|
-
if (openBrowser) {
|
|
172
|
-
opener(`file://${reportFilepath}`);
|
|
173
|
-
}
|
|
174
|
-
resolve();
|
|
175
|
-
} catch (e) {
|
|
176
|
-
reject(e);
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
);
|
|
135
|
+
const reportHtml = renderViewer({
|
|
136
|
+
mode: 'static',
|
|
137
|
+
title: resolveTitle(reportTitle),
|
|
138
|
+
chartData,
|
|
139
|
+
defaultSizes,
|
|
140
|
+
enableWebSocket: false
|
|
180
141
|
});
|
|
142
|
+
const reportFilepath = path.resolve(bundleDir || process.cwd(), reportFilename);
|
|
143
|
+
|
|
144
|
+
fs.mkdirSync(path.dirname(reportFilepath), {recursive: true});
|
|
145
|
+
fs.writeFileSync(reportFilepath, reportHtml);
|
|
146
|
+
|
|
147
|
+
logger.info(`${bold('Webpack Bundle Analyzer')} saved report to ${bold(reportFilepath)}`);
|
|
148
|
+
|
|
149
|
+
if (openBrowser) {
|
|
150
|
+
open(`file://${reportFilepath}`, logger);
|
|
151
|
+
}
|
|
181
152
|
}
|
|
182
153
|
|
|
183
154
|
async function generateJSONReport(bundleStats, opts) {
|
|
@@ -187,29 +158,12 @@ async function generateJSONReport(bundleStats, opts) {
|
|
|
187
158
|
|
|
188
159
|
if (!chartData) return;
|
|
189
160
|
|
|
190
|
-
mkdir
|
|
191
|
-
fs.
|
|
161
|
+
await fs.promises.mkdir(path.dirname(reportFilename), {recursive: true});
|
|
162
|
+
await fs.promises.writeFile(reportFilename, JSON.stringify(chartData));
|
|
192
163
|
|
|
193
164
|
logger.info(`${bold('Webpack Bundle Analyzer')} saved JSON report to ${bold(reportFilename)}`);
|
|
194
165
|
}
|
|
195
166
|
|
|
196
|
-
function getAssetContent(filename) {
|
|
197
|
-
const assetPath = path.join(assetsRoot, filename);
|
|
198
|
-
|
|
199
|
-
if (!assetPath.startsWith(assetsRoot)) {
|
|
200
|
-
throw new Error(`"${filename}" is outside of the assets root`);
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
return fs.readFileSync(assetPath, 'utf8');
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* Escapes `<` characters in JSON to safely use it in `<script>` tag.
|
|
208
|
-
*/
|
|
209
|
-
function escapeJson(json) {
|
|
210
|
-
return JSON.stringify(json).replace(/</gu, '\\u003c');
|
|
211
|
-
}
|
|
212
|
-
|
|
213
167
|
function getChartData(analyzerOpts, ...args) {
|
|
214
168
|
let chartData;
|
|
215
169
|
const {logger} = analyzerOpts;
|