Skip to content

Instantly share code, notes, and snippets.

@ryanscherler
Created November 30, 2018 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanscherler/f16b98cf645f1e4daab7ba3131b98c8e to your computer and use it in GitHub Desktop.
Save ryanscherler/f16b98cf645f1e4daab7ba3131b98c8e to your computer and use it in GitHub Desktop.
Add PurgeCSS in production for Gatsby
// Add PurgeCSS in production
// See: https://github.com/gatsbyjs/gatsby/issues/5778#issuecomment-402481270
const PurgeCssPlugin = require(`purgecss-webpack-plugin`)
const path = require(`path`)
const glob = require(`glob`)
const PATHS = {
src: path.join(__dirname, `src`)
}
// Nonessential options removed for brevity
const purgeCssConfig = {
paths: glob.sync(`${PATHS.src}/**/*.js`, { nodir: true }),
}
exports.onCreateWebpackConfig = ({ actions, stage }) => {
if (stage.includes(`develop`)) return
// Add PurgeCSS in production
if (stage.includes(`build`)) {
actions.setWebpackConfig({
plugins: [new PurgeCssPlugin(purgeCssConfig)]
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment