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/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.use('/', (req, res) => {
63
- res.render('viewer', {
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
- get chartData() { return chartData },
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
- opener(url);
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
- await new Promise((resolve, reject) => {
144
- ejs.renderFile(
145
- `${projectRoot}/views/viewer.ejs`,
146
- {
147
- mode: 'static',
148
- title: resolveTitle(reportTitle),
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.sync(path.dirname(reportFilename));
191
- fs.writeFileSync(reportFilename, JSON.stringify(chartData));
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;
package/views/script.ejs DELETED
@@ -1,8 +0,0 @@
1
- <% if (mode === 'static') { %>
2
- <!-- <%= filename %> -->
3
- <script>
4
- <%- assetContent(filename) %>
5
- </script>
6
- <% } else { %>
7
- <script src="<%= filename %>"></script>
8
- <% } %>