17
17
*/
18
18
19
19
const { resolve : resolvePath } = require ( "path" ) ;
20
+ /* eslint-disable import/no-extraneous-dependencies */
21
+ const { BundleAnalyzerPlugin } = require ( "webpack-bundle-analyzer" ) ;
22
+ /* eslint-enable import/no-extraneous-dependencies */
20
23
21
24
const r = m => resolvePath ( __dirname , m ) ;
22
25
26
+ /**
27
+ * Configuration for the `webpack-bundle-analyzer` plugin.
28
+ *
29
+ * @type {object }
30
+ * @since 0.1.0
31
+ */
32
+ const bundleAnalyzerPluginConfig = {
33
+ analyzerMode : "static" ,
34
+ generateStatsFile : true ,
35
+ openAnalyzer : false ,
36
+ reportFilename : r ( "../build/reports/webpack-bundle-analyzer/index.html" ) ,
37
+ statsFilename : r ( "../build/reports/webpack-bundle-analyzer/stats.json" )
38
+ } ;
39
+
23
40
/**
24
41
* Implementation of the Gatsby Node `onCreateWebpackConfig` API.
25
42
*
26
43
* @method onCreateWebpackConfig
27
44
* @param {object } actions Collection of functions provided by Gatsby used to manipulate the state of the build
28
- * process.
45
+ * @param { string } stage The name of the current Gatsby build process stage .
29
46
* @see https://gatsbyjs.org/docs/node-apis/#onCreateWebpackConfig
30
47
* @see https://gatsbyjs.org/docs/actions/#setWebpackConfig
31
48
* @since 0.1.0
32
49
*/
33
- const onCreateWebpackConfig = ( { actions } ) => {
50
+ const onCreateWebpackConfig = ( { actions, stage } ) => {
34
51
actions . setWebpackConfig ( {
35
52
resolve : {
36
53
alias : {
@@ -50,6 +67,15 @@ const onCreateWebpackConfig = ({ actions }) => {
50
67
}
51
68
}
52
69
} ) ;
70
+
71
+ switch ( stage ) {
72
+ case "build-html" :
73
+ case "build-javascript" :
74
+ actions . setWebpackConfig ( {
75
+ plugins : [ new BundleAnalyzerPlugin ( bundleAnalyzerPluginConfig ) ]
76
+ } ) ;
77
+ break ;
78
+ }
53
79
} ;
54
80
55
81
module . exports = onCreateWebpackConfig ;
0 commit comments