viewer.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. "use strict";
  2. const path = require('path');
  3. const fs = require('fs');
  4. const http = require('http');
  5. const WebSocket = require('ws');
  6. const sirv = require('sirv');
  7. const {
  8. bold
  9. } = require('picocolors');
  10. const Logger = require('./Logger');
  11. const analyzer = require('./analyzer');
  12. const {
  13. open
  14. } = require('./utils');
  15. const {
  16. renderViewer
  17. } = require('./template');
  18. const projectRoot = path.resolve(__dirname, '..');
  19. function resolveTitle(reportTitle) {
  20. if (typeof reportTitle === 'function') {
  21. return reportTitle();
  22. } else {
  23. return reportTitle;
  24. }
  25. }
  26. module.exports = {
  27. startServer,
  28. generateReport,
  29. generateJSONReport,
  30. getEntrypoints,
  31. // deprecated
  32. start: startServer
  33. };
  34. async function startServer(bundleStats, opts) {
  35. const {
  36. port = 8888,
  37. host = '127.0.0.1',
  38. openBrowser = true,
  39. bundleDir = null,
  40. logger = new Logger(),
  41. defaultSizes = 'parsed',
  42. excludeAssets = null,
  43. reportTitle,
  44. analyzerUrl
  45. } = opts || {};
  46. const analyzerOpts = {
  47. logger,
  48. excludeAssets
  49. };
  50. let chartData = getChartData(analyzerOpts, bundleStats, bundleDir);
  51. const entrypoints = getEntrypoints(bundleStats);
  52. if (!chartData) return;
  53. const sirvMiddleware = sirv(`${projectRoot}/public`, {
  54. // disables caching and traverse the file system on every request
  55. dev: true
  56. });
  57. const server = http.createServer((req, res) => {
  58. if (req.method === 'GET' && req.url === '/') {
  59. const html = renderViewer({
  60. mode: 'server',
  61. title: resolveTitle(reportTitle),
  62. chartData,
  63. entrypoints,
  64. defaultSizes,
  65. enableWebSocket: true
  66. });
  67. res.writeHead(200, {
  68. 'Content-Type': 'text/html'
  69. });
  70. res.end(html);
  71. } else {
  72. sirvMiddleware(req, res);
  73. }
  74. });
  75. await new Promise(resolve => {
  76. server.listen(port, host, () => {
  77. resolve();
  78. const url = analyzerUrl({
  79. listenPort: port,
  80. listenHost: host,
  81. boundAddress: server.address()
  82. });
  83. logger.info(`${bold('Webpack Bundle Analyzer')} is started at ${bold(url)}\n` + `Use ${bold('Ctrl+C')} to close it`);
  84. if (openBrowser) {
  85. open(url, logger);
  86. }
  87. });
  88. });
  89. const wss = new WebSocket.Server({
  90. server
  91. });
  92. wss.on('connection', ws => {
  93. ws.on('error', err => {
  94. // Ignore network errors like `ECONNRESET`, `EPIPE`, etc.
  95. if (err.errno) return;
  96. logger.info(err.message);
  97. });
  98. });
  99. return {
  100. ws: wss,
  101. http: server,
  102. updateChartData
  103. };
  104. function updateChartData(bundleStats) {
  105. const newChartData = getChartData(analyzerOpts, bundleStats, bundleDir);
  106. if (!newChartData) return;
  107. chartData = newChartData;
  108. wss.clients.forEach(client => {
  109. if (client.readyState === WebSocket.OPEN) {
  110. client.send(JSON.stringify({
  111. event: 'chartDataUpdated',
  112. data: newChartData
  113. }));
  114. }
  115. });
  116. }
  117. }
  118. async function generateReport(bundleStats, opts) {
  119. const {
  120. openBrowser = true,
  121. reportFilename,
  122. reportTitle,
  123. bundleDir = null,
  124. logger = new Logger(),
  125. defaultSizes = 'parsed',
  126. excludeAssets = null
  127. } = opts || {};
  128. const chartData = getChartData({
  129. logger,
  130. excludeAssets
  131. }, bundleStats, bundleDir);
  132. const entrypoints = getEntrypoints(bundleStats);
  133. if (!chartData) return;
  134. const reportHtml = renderViewer({
  135. mode: 'static',
  136. title: resolveTitle(reportTitle),
  137. chartData,
  138. entrypoints,
  139. defaultSizes,
  140. enableWebSocket: false
  141. });
  142. const reportFilepath = path.resolve(bundleDir || process.cwd(), reportFilename);
  143. fs.mkdirSync(path.dirname(reportFilepath), {
  144. recursive: true
  145. });
  146. fs.writeFileSync(reportFilepath, reportHtml);
  147. logger.info(`${bold('Webpack Bundle Analyzer')} saved report to ${bold(reportFilepath)}`);
  148. if (openBrowser) {
  149. open(`file://${reportFilepath}`, logger);
  150. }
  151. }
  152. async function generateJSONReport(bundleStats, opts) {
  153. const {
  154. reportFilename,
  155. bundleDir = null,
  156. logger = new Logger(),
  157. excludeAssets = null
  158. } = opts || {};
  159. const chartData = getChartData({
  160. logger,
  161. excludeAssets
  162. }, bundleStats, bundleDir);
  163. if (!chartData) return;
  164. await fs.promises.mkdir(path.dirname(reportFilename), {
  165. recursive: true
  166. });
  167. await fs.promises.writeFile(reportFilename, JSON.stringify(chartData));
  168. logger.info(`${bold('Webpack Bundle Analyzer')} saved JSON report to ${bold(reportFilename)}`);
  169. }
  170. function getChartData(analyzerOpts, ...args) {
  171. let chartData;
  172. const {
  173. logger
  174. } = analyzerOpts;
  175. try {
  176. chartData = analyzer.getViewerData(...args, analyzerOpts);
  177. } catch (err) {
  178. logger.error(`Could't analyze webpack bundle:\n${err}`);
  179. logger.debug(err.stack);
  180. chartData = null;
  181. } // chartData can either be an array (bundleInfo[]) or null. It can't be an plain object anyway
  182. if ( // analyzer.getViewerData() doesn't failed in the previous step
  183. chartData && !Array.isArray(chartData)) {
  184. logger.error("Could't find any javascript bundles in provided stats file");
  185. chartData = null;
  186. }
  187. return chartData;
  188. }
  189. function getEntrypoints(bundleStats) {
  190. if (bundleStats === null || bundleStats === undefined || !bundleStats.entrypoints) {
  191. return [];
  192. }
  193. return Object.values(bundleStats.entrypoints).map(entrypoint => entrypoint.name);
  194. }