UNPKG

1.37 kBJavaScriptView Raw
1"use strict";
2
3const fs = require(`fs-extra`);
4
5const path = require(`path`);
6
7class GatsbyWebpackStatsExtractor {
8 constructor(options) {
9 this.plugin = {
10 name: `GatsbyWebpackStatsExtractor`
11 };
12 this.options = options || {};
13 }
14
15 apply(compiler) {
16 compiler.hooks.done.tapAsync(this.plugin.name, (stats, done) => {
17 let assets = {};
18 let assetsMap = {};
19
20 for (let chunkGroup of stats.compilation.chunkGroups) {
21 if (chunkGroup.name) {
22 let files = [];
23
24 for (let chunk of chunkGroup.chunks) {
25 files.push(...chunk.files);
26 }
27
28 assets[chunkGroup.name] = files.filter(f => f.slice(-4) !== `.map`);
29 assetsMap[chunkGroup.name] = files.filter(f => f.slice(-4) !== `.map` && f.slice(0, chunkGroup.name.length) === chunkGroup.name).map(filename => `/${filename}`);
30 }
31 }
32
33 const webpackStats = Object.assign({}, stats.toJson({
34 all: false,
35 chunkGroups: true
36 }), {
37 assetsByChunkName: assets
38 });
39 fs.writeFile(path.join(`public`, `chunk-map.json`), JSON.stringify(assetsMap), () => {
40 fs.writeFile(path.join(`public`, `webpack.stats.json`), JSON.stringify(webpackStats), done);
41 });
42 });
43 }
44
45}
46
47module.exports = GatsbyWebpackStatsExtractor;
48//# sourceMappingURL=gatsby-webpack-stats-extractor.js.map
\No newline at end of file