-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathplugin.js
56 lines (47 loc) · 1.79 KB
/
plugin.js
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
/* eslint no-console:0 */
import path from 'path';
import fs from 'fs';
import mkdirp from 'mkdirp';
let cssString = fs.readFileSync(path.join(__dirname, './style.css'), 'utf8');
let jsString = fs.readFileSync(path.join(__dirname, './pluginmain.js'), 'utf8');
export default class VisualizerPlugin {
constructor(opts = {filename: 'stats.html'}) {
this.opts = opts;
}
apply(compiler) {
compiler.plugin('emit', (compilation, callback) => {
let stats = compilation.getStats().toJson({
hash: true,
version: true,
timings: true,
assets: true,
chunks: true,
chunkModules: true,
modules: true,
reasons: true,
source: false,
});
let stringifiedStats = JSON.stringify(stats).replace(/</g, '\\u003c');
let html = `<!doctype html>
<meta charset="UTF-8">
<title>Webpack Visualizer</title>
<style>${cssString}</style>
<div id="App"></div>
<script>window.stats = ${stringifiedStats};</script>
<script>${jsString}</script>
`;
let outputFile = path.join(compilation.outputOptions.path, this.opts.filename);
mkdirp(path.dirname(outputFile), (mkdirpErr) => {
if (mkdirpErr) {
console.log('webpack-visualizer-plugin: error writing stats file');
}
fs.writeFile(outputFile, html, (err) => {
if (err) {
console.log('webpack-visualizer-plugin: error writing stats file');
}
callback();
});
});
});
}
}