webpack-bundle-analyzer 4.0.0 → 4.4.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/lib/viewer.js CHANGED
@@ -8,13 +8,9 @@ const http = require('http');
8
8
 
9
9
  const WebSocket = require('ws');
10
10
 
11
- const _ = require('lodash');
12
-
13
- const express = require('express');
14
-
15
- const ejs = require('ejs');
11
+ const sirv = require('sirv');
16
12
 
17
- const mkdir = require('mkdirp');
13
+ const _ = require('lodash');
18
14
 
19
15
  const {
20
16
  bold
@@ -28,8 +24,11 @@ const {
28
24
  open
29
25
  } = require('./utils');
30
26
 
27
+ const {
28
+ renderViewer
29
+ } = require('./template');
30
+
31
31
  const projectRoot = path.resolve(__dirname, '..');
32
- const assetsRoot = path.join(projectRoot, 'public');
33
32
 
34
33
  function resolveTitle(reportTitle) {
35
34
  if (typeof reportTitle === 'function') {
@@ -64,29 +63,27 @@ async function startServer(bundleStats, opts) {
64
63
  };
65
64
  let chartData = getChartData(analyzerOpts, bundleStats, bundleDir);
66
65
  if (!chartData) return;
67
- const app = express(); // Explicitly using our `ejs` dependency to render templates
68
- // Fixes #17
69
-
70
- app.engine('ejs', require('ejs').renderFile);
71
- app.set('view engine', 'ejs');
72
- app.set('views', `${projectRoot}/views`);
73
- app.use(express.static(`${projectRoot}/public`));
74
- app.use('/', (req, res) => {
75
- res.render('viewer', {
76
- mode: 'server',
77
- title: resolveTitle(reportTitle),
78
-
79
- get chartData() {
80
- return chartData;
81
- },
82
-
83
- defaultSizes,
84
- enableWebSocket: true,
85
- // Helpers
86
- escapeJson
87
- });
66
+ const sirvMiddleware = sirv(`${projectRoot}/public`, {
67
+ // disables caching and traverse the file system on every request
68
+ dev: true
69
+ });
70
+ const server = http.createServer((req, res) => {
71
+ if (req.method === 'GET' && req.url === '/') {
72
+ const html = renderViewer({
73
+ mode: 'server',
74
+ title: resolveTitle(reportTitle),
75
+ chartData,
76
+ defaultSizes,
77
+ enableWebSocket: true
78
+ });
79
+ res.writeHead(200, {
80
+ 'Content-Type': 'text/html'
81
+ });
82
+ res.end(html);
83
+ } else {
84
+ sirvMiddleware(req, res);
85
+ }
88
86
  });
89
- const server = http.createServer(app);
90
87
  await new Promise(resolve => {
91
88
  server.listen(port, host, () => {
92
89
  resolve();
@@ -144,39 +141,23 @@ async function generateReport(bundleStats, opts) {
144
141
  excludeAssets
145
142
  }, bundleStats, bundleDir);
146
143
  if (!chartData) return;
147
- await new Promise((resolve, reject) => {
148
- ejs.renderFile(`${projectRoot}/views/viewer.ejs`, {
149
- mode: 'static',
150
- title: resolveTitle(reportTitle),
151
- chartData,
152
- defaultSizes,
153
- enableWebSocket: false,
154
- // Helpers
155
- assetContent: getAssetContent,
156
- escapeJson
157
- }, (err, reportHtml) => {
158
- try {
159
- if (err) {
160
- logger.error(err);
161
- reject(err);
162
- return;
163
- }
164
-
165
- const reportFilepath = path.resolve(bundleDir || process.cwd(), reportFilename);
166
- mkdir.sync(path.dirname(reportFilepath));
167
- fs.writeFileSync(reportFilepath, reportHtml);
168
- logger.info(`${bold('Webpack Bundle Analyzer')} saved report to ${bold(reportFilepath)}`);
169
-
170
- if (openBrowser) {
171
- open(`file://${reportFilepath}`, logger);
172
- }
173
-
174
- resolve();
175
- } catch (e) {
176
- reject(e);
177
- }
178
- });
144
+ const reportHtml = renderViewer({
145
+ mode: 'static',
146
+ title: resolveTitle(reportTitle),
147
+ chartData,
148
+ defaultSizes,
149
+ enableWebSocket: false
150
+ });
151
+ const reportFilepath = path.resolve(bundleDir || process.cwd(), reportFilename);
152
+ fs.mkdirSync(path.dirname(reportFilepath), {
153
+ recursive: true
179
154
  });
155
+ fs.writeFileSync(reportFilepath, reportHtml);
156
+ logger.info(`${bold('Webpack Bundle Analyzer')} saved report to ${bold(reportFilepath)}`);
157
+
158
+ if (openBrowser) {
159
+ open(`file://${reportFilepath}`, logger);
160
+ }
180
161
  }
181
162
 
182
163
  async function generateJSONReport(bundleStats, opts) {
@@ -191,29 +172,13 @@ async function generateJSONReport(bundleStats, opts) {
191
172
  excludeAssets
192
173
  }, bundleStats, bundleDir);
193
174
  if (!chartData) return;
194
- mkdir.sync(path.dirname(reportFilename));
195
- fs.writeFileSync(reportFilename, JSON.stringify(chartData));
175
+ await fs.promises.mkdir(path.dirname(reportFilename), {
176
+ recursive: true
177
+ });
178
+ await fs.promises.writeFile(reportFilename, JSON.stringify(chartData));
196
179
  logger.info(`${bold('Webpack Bundle Analyzer')} saved JSON report to ${bold(reportFilename)}`);
197
180
  }
198
181
 
199
- function getAssetContent(filename) {
200
- const assetPath = path.join(assetsRoot, filename);
201
-
202
- if (!assetPath.startsWith(assetsRoot)) {
203
- throw new Error(`"${filename}" is outside of the assets root`);
204
- }
205
-
206
- return fs.readFileSync(assetPath, 'utf8');
207
- }
208
- /**
209
- * Escapes `<` characters in JSON to safely use it in `<script>` tag.
210
- */
211
-
212
-
213
- function escapeJson(json) {
214
- return JSON.stringify(json).replace(/</gu, '\\u003c');
215
- }
216
-
217
182
  function getChartData(analyzerOpts, ...args) {
218
183
  let chartData;
219
184
  const {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webpack-bundle-analyzer",
3
- "version": "4.0.0",
3
+ "version": "4.4.0",
4
4
  "description": "Webpack plugin and CLI utility that represents bundle content as convenient interactive zoomable treemap",
5
5
  "author": "Yury Grunin <grunin.ya@ya.ru>",
6
6
  "license": "MIT",
@@ -30,22 +30,17 @@
30
30
  "files": [
31
31
  "public",
32
32
  "lib",
33
- "src",
34
- "views"
33
+ "src"
35
34
  ],
36
35
  "dependencies": {
37
36
  "acorn": "^8.0.4",
38
37
  "acorn-walk": "^8.0.0",
39
- "bfj": "^7.0.2",
40
38
  "chalk": "^4.1.0",
41
39
  "commander": "^6.2.0",
42
- "ejs": "^3.1.5",
43
- "express": "^4.17.1",
44
- "filesize": "^6.1.0",
45
- "gzip-size": "^5.1.1",
40
+ "gzip-size": "^6.0.0",
46
41
  "lodash": "^4.17.20",
47
- "mkdirp": "^1.0.4",
48
42
  "opener": "^1.5.2",
43
+ "sirv": "^1.0.7",
49
44
  "ws": "^7.3.1"
50
45
  },
51
46
  "devDependencies": {
@@ -57,6 +52,7 @@
57
52
  "@babel/preset-react": "7.12.5",
58
53
  "@babel/register": "7.12.1",
59
54
  "@babel/runtime": "7.12.5",
55
+ "@carrotsearch/foamtree": "3.5.0",
60
56
  "autoprefixer": "10.0.1",
61
57
  "babel-eslint": "10.1.0",
62
58
  "babel-loader": "8.1.0",
@@ -73,6 +69,8 @@
73
69
  "eslint-config-th0r-react": "2.0.1",
74
70
  "eslint-plugin-react": "7.21.5",
75
71
  "exports-loader": "1.1.1",
72
+ "filesize": "^6.1.0",
73
+ "globby": "11.0.1",
76
74
  "gulp": "4.0.2",
77
75
  "gulp-babel": "8.0.0",
78
76
  "mobx": "5.15.7",