-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathplugin.js
More file actions
72 lines (49 loc) · 3.19 KB
/
plugin.js
File metadata and controls
72 lines (49 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* eslint no-console:0 */
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
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; }; })();
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
var _fs = require('fs');
var _fs2 = _interopRequireDefault(_fs);
var _mkdirp = require('mkdirp');
var _mkdirp2 = _interopRequireDefault(_mkdirp);
var cssString = _fs2['default'].readFileSync(_path2['default'].join(__dirname, './style.css'), 'utf8');
var jsString = _fs2['default'].readFileSync(_path2['default'].join(__dirname, './pluginmain.js'), 'utf8');
var VisualizerPlugin = (function () {
function VisualizerPlugin() {
var opts = arguments.length <= 0 || arguments[0] === undefined ? { filename: 'stats.html' } : arguments[0];
_classCallCheck(this, VisualizerPlugin);
this.opts = opts;
}
_createClass(VisualizerPlugin, [{
key: 'apply',
value: function apply(compiler) {
var _this = this;
compiler.hooks.emit.tap('webpack-visualizer', function (compilation) {
var stats = compilation.getStats().toJson({ chunkModules: true });
var stringifiedStats = JSON.stringify(stats);
stringifiedStats = stringifiedStats.replace(/</g, '<').replace(/</g, '>');
var html = '<!doctype html>\n <meta charset="UTF-8">\n <title>Webpack Visualizer</title>\n <style>' + cssString + '</style>\n <div id="App"></div>\n <script>window.stats = ' + stringifiedStats + ';</script>\n <script>' + jsString + '</script>\n ';
var outputFile = _path2['default'].join(compilation.outputOptions.path, _this.opts.filename);
(0, _mkdirp2['default'])(_path2['default'].dirname(outputFile), function (mkdirpErr) {
if (mkdirpErr) {
console.log('webpack-visualizer-plugin: error writing stats file');
}
_fs2['default'].writeFile(outputFile, html, function (err) {
if (err) {
console.log('webpack-visualizer-plugin: error writing stats file');
}
});
});
});
}
}]);
return VisualizerPlugin;
})();
exports['default'] = VisualizerPlugin;
module.exports = exports['default'];