-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathplugin.js
45 lines (37 loc) · 1.57 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
/* 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.hooks.emit.tap('webpack-visualizer', (compilation) => {
let stats = compilation.getStats().toJson({chunkModules: true});
let stringifiedStats = JSON.stringify(stats);
stringifiedStats = stringifiedStats.replace(/</g, '<').replace(/</g, '>');
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');
}
});
});
});
}
}