Gatsby Plugin Purgecss (beta)
For Gatsby 2 only
What is this plugin about?
This plugin allows Gatsby to remove unused css from css/sass/less/stylus files and modules using purgecss.
Supported files
Installation
npm i --save-dev gatsby-plugin-purgecss
Usage
Add the plugin AFTER other css plugins
module.exports = {
plugins: [
`gatsby-plugin-stylus`,
`gatsby-plugin-sass`,
`gatsby-plugin-less`,
`gatsby-plugin-purgecss`
]
};
Important Notes
Running
This plugin only runs when building the project (gatsby build).
To make sure the plugin is working, set rejected: true
option to print out the removed selectors.
Selector matching
This plugin loads css files (or transformed output from css plugins) and searches for matching selectors in js, jsx, ts, tsx files in src/
. It does not know which css file belongs to which source file. Therefore, for eg., if there is a class .module
in some css file, it will not be removed if it used in any script file under src/
.
Whitelist ['html', 'body']
Since html and body tags do not appear in src/
files, it is whitelisted by default to not be removed.
If there is a need to modify the whitelist, it is recommended to keep these tags and append the required selectors using the option
whitelist: ['html', 'body', '.my-selector']
Options
This plugins supports most purgecss options as is (except css
).
Read about purgecss options in detail
{
resolve: `gatsby-plugin-purgecss`,
options: {
rejected?: boolean,
whitelist?: Array<string>,
extractors?: Array<ExtractorsObj>,
whitelistPatterns?: Array<RegExp>,
whitelistPatternsChildren?: Array<RegExp>,
keyframes?: boolean,
fontFace?: boolean,
content: Array<string | RawContent>,
}
}
Versioning
Gatsy-plugin-purgecss use SemVer for versioning.
It will try to match official gatsby plugin's major version when released.
Acknowledgment
This project was made possible due to the incredible work done on the following projects:
License
This project is licensed under the MIT License - see the LICENSE file
for details.