BundleAnalyzerPlugin.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. logLevel: 'info',
  26. // deprecated
  27. startAnalyzer: true
  28. }, opts);
  29. this.server = null;
  30. this.logger = new Logger(this.opts.logLevel);
  31. }
  32. _createClass(BundleAnalyzerPlugin, [{
  33. key: 'apply',
  34. value: function apply(compiler) {
  35. var _this = this;
  36. this.compiler = compiler;
  37. var done = function done(stats) {
  38. stats = stats.toJson(_this.opts.statsOptions);
  39. var actions = [];
  40. if (_this.opts.generateStatsFile) {
  41. actions.push(function () {
  42. return _this.generateStatsFile(stats);
  43. });
  44. }
  45. // Handling deprecated `startAnalyzer` flag
  46. if (_this.opts.analyzerMode === 'server' && !_this.opts.startAnalyzer) {
  47. _this.opts.analyzerMode = 'disabled';
  48. }
  49. if (_this.opts.analyzerMode === 'server') {
  50. actions.push(function () {
  51. return _this.startAnalyzerServer(stats);
  52. });
  53. } else if (_this.opts.analyzerMode === 'static') {
  54. actions.push(function () {
  55. return _this.generateStaticReport(stats);
  56. });
  57. }
  58. if (actions.length) {
  59. // Making analyzer logs to be after all webpack logs in the console
  60. setImmediate(function () {
  61. actions.forEach(function (action) {
  62. return action();
  63. });
  64. });
  65. }
  66. };
  67. if (compiler.hooks) {
  68. compiler.hooks.done.tap('webpack-bundle-analyzer', done);
  69. } else {
  70. compiler.plugin('done', done);
  71. }
  72. }
  73. }, {
  74. key: 'generateStatsFile',
  75. value: function () {
  76. var _ref = _asyncToGenerator(function* (stats) {
  77. var statsFilepath = path.resolve(this.compiler.outputPath, this.opts.statsFilename);
  78. mkdir.sync(path.dirname(statsFilepath));
  79. try {
  80. yield bfj.write(statsFilepath, stats, {
  81. promises: 'ignore',
  82. buffers: 'ignore',
  83. maps: 'ignore',
  84. iterables: 'ignore',
  85. circular: 'ignore'
  86. });
  87. this.logger.info(`${bold('Webpack Bundle Analyzer')} saved stats file to ${bold(statsFilepath)}`);
  88. } catch (error) {
  89. this.logger.error(`${bold('Webpack Bundle Analyzer')} error saving stats file to ${bold(statsFilepath)}: ${error}`);
  90. }
  91. });
  92. function generateStatsFile(_x) {
  93. return _ref.apply(this, arguments);
  94. }
  95. return generateStatsFile;
  96. }()
  97. }, {
  98. key: 'startAnalyzerServer',
  99. value: function () {
  100. var _ref2 = _asyncToGenerator(function* (stats) {
  101. if (this.server) {
  102. (yield this.server).updateChartData(stats);
  103. } else {
  104. this.server = viewer.startServer(stats, {
  105. openBrowser: this.opts.openAnalyzer,
  106. host: this.opts.analyzerHost,
  107. port: this.opts.analyzerPort,
  108. bundleDir: this.getBundleDirFromCompiler(),
  109. logger: this.logger,
  110. defaultSizes: this.opts.defaultSizes
  111. });
  112. }
  113. });
  114. function startAnalyzerServer(_x2) {
  115. return _ref2.apply(this, arguments);
  116. }
  117. return startAnalyzerServer;
  118. }()
  119. }, {
  120. key: 'generateStaticReport',
  121. value: function generateStaticReport(stats) {
  122. viewer.generateReport(stats, {
  123. openBrowser: this.opts.openAnalyzer,
  124. reportFilename: path.resolve(this.compiler.outputPath, this.opts.reportFilename),
  125. bundleDir: this.getBundleDirFromCompiler(),
  126. logger: this.logger,
  127. defaultSizes: this.opts.defaultSizes
  128. });
  129. }
  130. }, {
  131. key: 'getBundleDirFromCompiler',
  132. value: function getBundleDirFromCompiler() {
  133. return this.compiler.outputFileSystem.constructor.name === 'MemoryFileSystem' ? null : this.compiler.outputPath;
  134. }
  135. }]);
  136. return BundleAnalyzerPlugin;
  137. }();
  138. module.exports = BundleAnalyzerPlugin;