Skip to content

Instantly share code, notes, and snippets.

@ion1
Created March 8, 2019 23:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ion1/f88d95fbff2354d30f1a1f1c09527b99 to your computer and use it in GitHub Desktop.
Save ion1/f88d95fbff2354d30f1a1f1c09527b99 to your computer and use it in GitHub Desktop.
Gridsome purgecss-plugin
const fs = require('mz/fs')
const hirestime = require('hirestime')
const Purgecss = require('purgecss')
class PurgecssPlugin {
static defaultOptions () {
return {}
}
constructor (api, originalOptions) {
api.afterBuild(async ({ queue, config: { assetsDir } }) => {
const time = hirestime()
const options = {
...originalOptions,
content: [
`${assetsDir}/**/*.html`,
`${assetsDir}/**/*.js`,
...queue.map(page => page.htmlOutput)
],
css: [`${assetsDir}/**/*.css`],
rejected: true
}
const result = new Purgecss(options).purge()
for (const { file, css } of result) {
await fs.writeFile(file, css)
}
const numRejected = result.reduce((acc, { rejected }) => acc + rejected.length, 0)
console.info(
`Purge CSS (${result.length} stylesheets` +
`, ${numRejected} selectors rejected)` +
` - ${time(hirestime.S)}s`
)
})
}
}
module.exports = PurgecssPlugin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment