viewer.js 5.1 KB

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