Created
March 8, 2019 23:03
-
-
Save ion1/f88d95fbff2354d30f1a1f1c09527b99 to your computer and use it in GitHub Desktop.
Gridsome purgecss-plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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