Skip to content

Commit

Permalink
Add and configure the webpack-bundle-analyzer plugin
Browse files Browse the repository at this point in the history
Configured  the `webpack-bundle-analyzer` (1) plugin to generate a
"static" report with a JSON stats file stored in a newly created build
directory within the project root.

References:
  (1) https://github.com/webpack-contrib/webpack-bundle-analyzer

GH-31
  • Loading branch information
arcticicestudio committed Nov 19, 2018
1 parent 187237a commit 87fb27a
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions .gatsby/onCreateWebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,37 @@
*/

const { resolve: resolvePath } = require("path");
/* eslint-disable import/no-extraneous-dependencies */
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
/* eslint-enable import/no-extraneous-dependencies */

const r = m => resolvePath(__dirname, m);

/**
* Configuration for the `webpack-bundle-analyzer` plugin.
*
* @type {object}
* @since 0.1.0
*/
const bundleAnalyzerPluginConfig = {
analyzerMode: "static",
generateStatsFile: true,
openAnalyzer: false,
reportFilename: r("../build/reports/webpack-bundle-analyzer/index.html"),
statsFilename: r("../build/reports/webpack-bundle-analyzer/stats.json")
};

/**
* Implementation of the Gatsby Node `onCreateWebpackConfig` API.
*
* @method onCreateWebpackConfig
* @param {object} actions Collection of functions provided by Gatsby used to manipulate the state of the build
* process.
* @param {string} stage The name of the current Gatsby build process stage.
* @see https://gatsbyjs.org/docs/node-apis/#onCreateWebpackConfig
* @see https://gatsbyjs.org/docs/actions/#setWebpackConfig
* @since 0.1.0
*/
const onCreateWebpackConfig = ({ actions }) => {
const onCreateWebpackConfig = ({ actions, stage }) => {
actions.setWebpackConfig({
resolve: {
alias: {
Expand All @@ -50,6 +67,15 @@ const onCreateWebpackConfig = ({ actions }) => {
}
}
});

switch (stage) {
case "build-html":
case "build-javascript":
actions.setWebpackConfig({
plugins: [new BundleAnalyzerPlugin(bundleAnalyzerPluginConfig)]
});
break;
}
};

module.exports = onCreateWebpackConfig;

0 comments on commit 87fb27a

Please sign in to comment.