webpack-bundle-analyzer 3.3.1 → 3.5.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.
@@ -8,11 +8,10 @@ const viewer = require('./viewer');
8
8
 
9
9
  class BundleAnalyzerPlugin {
10
10
 
11
- constructor(opts) {
11
+ constructor(opts = {}) {
12
12
  this.opts = {
13
13
  analyzerMode: 'server',
14
14
  analyzerHost: '127.0.0.1',
15
- analyzerPort: 8888,
16
15
  reportFilename: 'report.html',
17
16
  defaultSizes: 'parsed',
18
17
  openAnalyzer: true,
@@ -23,7 +22,8 @@ class BundleAnalyzerPlugin {
23
22
  logLevel: 'info',
24
23
  // deprecated
25
24
  startAnalyzer: true,
26
- ...opts
25
+ ...opts,
26
+ analyzerPort: 'analyzerPort' in opts ? (opts.analyzerPort === 'auto' ? 0 : opts.analyzerPort) : 8888
27
27
  };
28
28
 
29
29
  this.server = null;
@@ -35,12 +35,11 @@ class BundleAnalyzerPlugin {
35
35
 
36
36
  const done = (stats, callback) => {
37
37
  callback = callback || (() => {});
38
- stats = stats.toJson(this.opts.statsOptions);
39
38
 
40
39
  const actions = [];
41
40
 
42
41
  if (this.opts.generateStatsFile) {
43
- actions.push(() => this.generateStatsFile(stats));
42
+ actions.push(() => this.generateStatsFile(stats.toJson(this.opts.statsOptions)));
44
43
  }
45
44
 
46
45
  // Handling deprecated `startAnalyzer` flag
@@ -49,9 +48,9 @@ class BundleAnalyzerPlugin {
49
48
  }
50
49
 
51
50
  if (this.opts.analyzerMode === 'server') {
52
- actions.push(() => this.startAnalyzerServer(stats));
51
+ actions.push(() => this.startAnalyzerServer(stats.toJson()));
53
52
  } else if (this.opts.analyzerMode === 'static') {
54
- actions.push(() => this.generateStaticReport(stats));
53
+ actions.push(() => this.generateStaticReport(stats.toJson()));
55
54
  }
56
55
 
57
56
  if (actions.length) {
@@ -128,7 +127,16 @@ class BundleAnalyzerPlugin {
128
127
  }
129
128
 
130
129
  getBundleDirFromCompiler() {
131
- return (this.compiler.outputFileSystem.constructor.name === 'MemoryFileSystem') ? null : this.compiler.outputPath;
130
+ switch (this.compiler.outputFileSystem.constructor.name) {
131
+ case 'MemoryFileSystem':
132
+ return null;
133
+ // Detect AsyncMFS used by Nuxt 2.5 that replaces webpack's MFS during development
134
+ // Related: #274
135
+ case 'AsyncMFS':
136
+ return null;
137
+ default:
138
+ return this.compiler.outputPath;
139
+ }
132
140
  }
133
141
 
134
142
  }
@@ -41,7 +41,6 @@ const program = commander
41
41
  .option(
42
42
  '-p, --port <n>',
43
43
  'Port that will be used in `server` mode to start HTTP server.',
44
- Number,
45
44
  8888
46
45
  )
47
46
  .option(
@@ -88,8 +87,12 @@ const logger = new Logger(logLevel);
88
87
 
89
88
  if (!bundleStatsFile) showHelp('Provide path to Webpack Stats file as first argument');
90
89
  if (mode !== 'server' && mode !== 'static') showHelp('Invalid mode. Should be either `server` or `static`.');
91
- if (mode === 'server' && !host) showHelp('Invalid host name');
92
- if (mode === 'server' && isNaN(port)) showHelp('Invalid port number');
90
+ if (mode === 'server') {
91
+ if (!host) showHelp('Invalid host name');
92
+
93
+ port = port === 'auto' ? 0 : Number(port);
94
+ if (isNaN(port)) showHelp('Invalid port. Should be a number or `auto`');
95
+ }
93
96
  if (!SIZES.has(defaultSizes)) showHelp(`Invalid default sizes option. Possible values are: ${[...SIZES].join(', ')}`);
94
97
 
95
98
  bundleStatsFile = resolve(bundleStatsFile);
package/src/utils.js CHANGED
@@ -1,6 +1,8 @@
1
1
  const {inspect} = require('util');
2
2
  const _ = require('lodash');
3
3
 
4
+ const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
5
+
4
6
  exports.createAssetsFilter = createAssetsFilter;
5
7
 
6
8
  function createAssetsFilter(excludePatterns) {
@@ -32,3 +34,17 @@ function createAssetsFilter(excludePatterns) {
32
34
  return () => true;
33
35
  }
34
36
  }
37
+
38
+ /**
39
+ * @desc get string of current time
40
+ * format: dd/MMM HH:mm
41
+ * */
42
+ exports.getCurrentTime = function () {
43
+ const time = new Date();
44
+ const year = time.getFullYear();
45
+ const month = MONTHS[time.getMonth()];
46
+ const day = time.getDate();
47
+ const hour = time.getHours();
48
+ const minute = time.getMinutes();
49
+ return `${day} ${month} ${year} at ${hour}:${minute}`;
50
+ };
package/src/viewer.js CHANGED
@@ -10,6 +10,7 @@ const opener = require('opener');
10
10
  const mkdir = require('mkdirp');
11
11
  const {bold} = require('chalk');
12
12
 
13
+ const utils = require('./utils');
13
14
  const Logger = require('./Logger');
14
15
  const analyzer = require('./analyzer');
15
16
 
@@ -23,6 +24,8 @@ module.exports = {
23
24
  start: startServer
24
25
  };
25
26
 
27
+ const title = `${process.env.npm_package_name || 'Webpack Bundle Analyzer'} [${utils.getCurrentTime()}]`;
28
+
26
29
  async function startServer(bundleStats, opts) {
27
30
  const {
28
31
  port = 8888,
@@ -52,11 +55,12 @@ async function startServer(bundleStats, opts) {
52
55
  app.use('/', (req, res) => {
53
56
  res.render('viewer', {
54
57
  mode: 'server',
55
- get chartData() { return JSON.stringify(chartData) },
56
- defaultSizes: JSON.stringify(defaultSizes),
58
+ title,
59
+ get chartData() { return chartData },
60
+ defaultSizes,
57
61
  enableWebSocket: true,
58
62
  // Helpers
59
- escapeScript
63
+ escapeJson
60
64
  });
61
65
  });
62
66
 
@@ -133,12 +137,13 @@ async function generateReport(bundleStats, opts) {
133
137
  `${projectRoot}/views/viewer.ejs`,
134
138
  {
135
139
  mode: 'static',
136
- chartData: JSON.stringify(chartData),
137
- defaultSizes: JSON.stringify(defaultSizes),
140
+ title,
141
+ chartData,
142
+ defaultSizes,
138
143
  enableWebSocket: false,
139
144
  // Helpers
140
145
  assetContent: getAssetContent,
141
- escapeScript
146
+ escapeJson
142
147
  },
143
148
  (err, reportHtml) => {
144
149
  try {
@@ -180,10 +185,10 @@ function getAssetContent(filename) {
180
185
  }
181
186
 
182
187
  /**
183
- * Escapes `<` characters in the string to safely use it in `<script>` tag.
188
+ * Escapes `<` characters in JSON to safely use it in `<script>` tag.
184
189
  */
185
- function escapeScript(value) {
186
- return String(value).replace(/</gu, '\\u003c');
190
+ function escapeJson(json) {
191
+ return JSON.stringify(json).replace(/</gu, '\\u003c');
187
192
  }
188
193
 
189
194
  function getChartData(analyzerOpts, ...args) {
package/views/script.ejs CHANGED
@@ -1,7 +1,7 @@
1
1
  <% if (mode === 'static') { %>
2
2
  <!-- <%= filename %> -->
3
3
  <script>
4
- <%- escapeScript(assetContent(filename)) %>
4
+ <%- assetContent(filename) %>
5
5
  </script>
6
6
  <% } else { %>
7
7
  <script src="<%= filename %>"></script>
package/views/viewer.ejs CHANGED
@@ -3,7 +3,8 @@
3
3
  <head>
4
4
  <meta charset="UTF-8"/>
5
5
  <meta name="viewport" content="width=device-width, initial-scale=1"/>
6
- <title>Webpack Bundle Analyzer</title>
6
+ <title><%= title %></title>
7
+ <link rel="shortcut icon" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAMAAACdt4HsAAABrVBMVEUAAAD///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////+O1foceMD///+J0/qK1Pr7/v8Xdr/9///W8P4UdL7L7P0Scr2r4Pyj3vwad8D5/f/2/f+55f3E6f34+/2H0/ojfMKpzOd0rNgQcb3F3O/j9f7c8v6g3Pz0/P/w+v/q+P7n9v6T1/uQ1vuE0vqLut/y+v+Z2fvt+f+15Pzv9fuc2/vR7v2V2Pvd6/bg9P7I6/285/2y4/yp3/zp8vk8i8kqgMT7/P31+fyv4vxGkcz6/P6/6P3j7vfS5PNnpNUxhcbO7f7F6v3O4vHK3/DA2u631Ouy0eqXweKJud5wqthfoNMMbLvY8f73+v2dxeR8sNtTmdDx9/zX6PSjyeaCtd1YnNGX2PuQveCGt95Nls42h8dLlM3F4vBtAAAAM3RSTlMAAyOx0/sKBvik8opWGBMOAe3l1snDm2E9LSb06eHc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hhyZI1nWJQHgznErZhbjoEsWqi8dQNoE294aldzFurwSABL2XXMf9+H1VQGke9exw5P/AnA5Pv5ngMul7LOvO922iwACu8WkCwLCafvM4CeWPxfA8lNHcWZSoi8EwMAIciKX2Z4SWCMAa3snCZ/G4EA8D6CMLNFsGQhkkz/gQNEBbPCbWsxGUpYVu3z8IyNAknwJkfPMEhLyrdi5RTyUVACkw4GSFRNWJNEW+fgPGwHD8/JxnRuLabN4CGNRkAE23na2+VmEAUmrYymSGjMAYqH84YUIyzgzs3XC7gNgH36Vcc4zKY9o9fgPBXUAiHHwVboBHGLiX6Zcjp1f2wu4tvzZKo0ecPnDtQYDQvJXaBeNzce45Fp28ZQLrEZVuFqgBwOalArKXnW1UzlnSusQKJqKYNuz4tOnI6sZG4zanpemv+7ySU2jbA9h6uhcgpfy6G2PahirDZ6zvq6zDduMVFTKvzw8wgyEdelwY9in3XkEPs3osJuwRQ4qTkfzifndg9Gfc4pdsu82+tTnHZTBa2EAMrqr2t43pguc8tNm7JQVQ2S0ukj2d22dhXYP0/veWtwKrCkNoNimAN5+Xr/oLrxswKbVJjteWrX7eR63o4j9q0GxnaBdWgGA5VStpanIjQmEhV0/nVt5VOFUvix6awJhPcAaTEShgrG+iGyvb5a0Ndb1YGHFPEwoqAinoaykaID1o1pdPNu7XsnCKQ3R+hwWIIhGvORcJUBYXe3Xa3vq/mF/N9V13ugufMkfXn+KHsRD0B8AAAAASUVORK5CYII=" type="image/x-icon" />
7
8
 
8
9
  <%- include('script', { filename: 'viewer.js' }) %>
9
10
  </head>
@@ -11,9 +12,9 @@
11
12
  <body>
12
13
  <div id="app"></div>
13
14
  <script>
14
- window.chartData = <%- escapeScript(chartData) %>;
15
- window.defaultSizes = <%- escapeScript(defaultSizes) %>;
16
- window.enableWebSocket = <%- escapeScript(enableWebSocket) %>;
15
+ window.chartData = <%- escapeJson(chartData) %>;
16
+ window.defaultSizes = <%- escapeJson(defaultSizes) %>;
17
+ window.enableWebSocket = <%- escapeJson(enableWebSocket) %>;
17
18
  </script>
18
19
  </body>
19
20
  </html>