BundleAnalyzerPlugin.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. 'use strict';
  2. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  3. function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
  4. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  5. var bfj = require('bfj-node4');
  6. var path = require('path');
  7. var mkdir = require('mkdirp');
  8. var _require = require('chalk'),
  9. bold = _require.bold;
  10. var Logger = require('./Logger');
  11. var viewer = require('./viewer');
  12. var BundleAnalyzerPlugin = function () {
  13. function BundleAnalyzerPlugin(opts) {
  14. _classCallCheck(this, BundleAnalyzerPlugin);
  15. this.opts = Object.assign({
  16. analyzerMode: 'server',
  17. analyzerHost: '127.0.0.1',
  18. analyzerPort: 8888,
  19. reportFilename: 'report.html',
  20. defaultSizes: 'parsed',
  21. openAnalyzer: true,
  22. generateStatsFile: false,
  23. statsFilename: 'stats.json',
  24. statsOptions: null,
  25. excludeAssets: null,
  26. logLevel: 'info',
  27. // deprecated
  28. startAnalyzer: true
  29. }, opts);
  30. this.server = null;
  31. this.logger = new Logger(this.opts.logLevel);
  32. }
  33. _createClass(BundleAnalyzerPlugin, [{
  34. key: 'apply',
  35. value: function apply(compiler) {
  36. var _this = this;
  37. this.compiler = compiler;
  38. var done = function done(stats) {
  39. stats = stats.toJson(_this.opts.statsOptions);
  40. var actions = [];
  41. if (_this.opts.generateStatsFile) {
  42. actions.push(function () {
  43. return _this.generateStatsFile(stats);
  44. });
  45. }
  46. // Handling deprecated `startAnalyzer` flag
  47. if (_this.opts.analyzerMode === 'server' && !_this.opts.startAnalyzer) {
  48. _this.opts.analyzerMode = 'disabled';
  49. }
  50. if (_this.opts.analyzerMode === 'server') {
  51. actions.push(function () {
  52. return _this.startAnalyzerServer(stats);
  53. });
  54. } else if (_this.opts.analyzerMode === 'static') {
  55. actions.push(function () {
  56. return _this.generateStaticReport(stats);
  57. });
  58. }
  59. if (actions.length) {
  60. // Making analyzer logs to be after all webpack logs in the console
  61. setImmediate(function () {
  62. actions.forEach(function (action) {
  63. return action();
  64. });
  65. });
  66. }
  67. };
  68. if (compiler.hooks) {
  69. compiler.hooks.done.tap('webpack-bundle-analyzer', done);
  70. } else {
  71. compiler.plugin('done', done);
  72. }
  73. }
  74. }, {
  75. key: 'generateStatsFile',
  76. value: function () {
  77. var _ref = _asyncToGenerator(function* (stats) {
  78. var statsFilepath = path.resolve(this.compiler.outputPath, this.opts.statsFilename);
  79. mkdir.sync(path.dirname(statsFilepath));
  80. try {
  81. yield bfj.write(statsFilepath, stats, {
  82. space: 2,
  83. promises: 'ignore',
  84. buffers: 'ignore',
  85. maps: 'ignore',
  86. iterables: 'ignore',
  87. circular: 'ignore'
  88. });
  89. this.logger.info(`${bold('Webpack Bundle Analyzer')} saved stats file to ${bold(statsFilepath)}`);
  90. } catch (error) {
  91. this.logger.error(`${bold('Webpack Bundle Analyzer')} error saving stats file to ${bold(statsFilepath)}: ${error}`);
  92. }
  93. });
  94. function generateStatsFile(_x) {
  95. return _ref.apply(this, arguments);
  96. }
  97. return generateStatsFile;
  98. }()
  99. }, {
  100. key: 'startAnalyzerServer',
  101. value: function () {
  102. var _ref2 = _asyncToGenerator(function* (stats) {
  103. if (this.server) {
  104. (yield this.server).updateChartData(stats);
  105. } else {
  106. this.server = viewer.startServer(stats, {
  107. openBrowser: this.opts.openAnalyzer,
  108. host: this.opts.analyzerHost,
  109. port: this.opts.analyzerPort,
  110. bundleDir: this.getBundleDirFromCompiler(),
  111. logger: this.logger,
  112. defaultSizes: this.opts.defaultSizes,
  113. excludeAssets: this.opts.excludeAssets
  114. });
  115. }
  116. });
  117. function startAnalyzerServer(_x2) {
  118. return _ref2.apply(this, arguments);
  119. }
  120. return startAnalyzerServer;
  121. }()
  122. }, {
  123. key: 'generateStaticReport',
  124. value: function generateStaticReport(stats) {
  125. viewer.generateReport(stats, {
  126. openBrowser: this.opts.openAnalyzer,
  127. reportFilename: path.resolve(this.compiler.outputPath, this.opts.reportFilename),
  128. bundleDir: this.getBundleDirFromCompiler(),
  129. logger: this.logger,
  130. defaultSizes: this.opts.defaultSizes,
  131. excludeAssets: this.opts.excludeAssets
  132. });
  133. }
  134. }, {
  135. key: 'getBundleDirFromCompiler',
  136. value: function getBundleDirFromCompiler() {
  137. return this.compiler.outputFileSystem.constructor.name === 'MemoryFileSystem' ? null : this.compiler.outputPath;
  138. }
  139. }]);
  140. return BundleAnalyzerPlugin;
  141. }();
  142. module.exports = BundleAnalyzerPlugin;