- Remove unneeded CSS with ease
- Webpack or PostCSS mode
- Already comes with mighty default settings
- Built on top of purgecss
- Nuxt 2 (and only Nuxt 2) support
- Fully tested!
- Add
nuxt-purgecss
dependency to your project
yarn add --dev nuxt-purgecss # or npm install --save-dev nuxt-purgecss
- Add
nuxt-purgecss
to thebuildModules
section ofnuxt.config.js
export default {
buildModules: [
// Simple usage
'nuxt-purgecss',
// With options
['nuxt-purgecss', { /* module options */ }],
]
}
dependency
(No --dev
or --save-dev
flags) and use modules
section in nuxt.config.js
instead of buildModules
.
Before diving into the individual attributes, please have a look at the default settings of the module.
The defaults will scan all your .vue
or .js
components in the common Nuxt folders, as well as checking your nuxt.config.js
for used classes.
Furthermore, typical classes (like these needed for transitions, the nuxt link ones or those set when using scoped styles) are whitelisted already.
These settings should be a good foundation for a variety of projects.
You can define every option either as function or as static value (primitives, objects, arrays, ...). if you use a function, the default value will be provided as the first argument.
If you don't use a function to define you properties, the module will try to
merge them with the default values. This can be handy for paths
, whitelist
and so on because
the defaults are quite sensible. If you don't want to have the defaults include, just use a function.
- Type:
String
('webpack' or 'postcss') - Default:
postcss
Defines the mode, PurgeCSS should be used in.
- Webpack mode can only be used with
build.extractCSS: true
- PostCSS mode can only be used with a
build.postcss
object (no array) or default settings
- Type:
Boolean
- Default:
options.dev === false
(Disabled duringnuxt dev
, enabled otherwise)
Enables/Disables the module
Please read the PurgeCSS docs for information about PurgeCSS-related information.
Instead of content
we use paths
to specify the paths PurgeCSS should look into (explained here.
This applies to both modes, not only to webpack mode
.
//nuxt.config.js
export default {
buildModules: [
'nuxt-purgecss',
]
}
//nuxt.config.js
export default {
buildModules: [
'nuxt-purgecss',
],
purgeCSS: {
whitelist: () => ['only-this-class'],
}
}
//nuxt.config.js
export default {
buildModules: [
'nuxt-purgecss',
],
purgeCSS: {
whitelist: ['defaults-and-this-class'],
}
}
//nuxt.config.js
export default {
buildModules: [
'nuxt-purgecss',
],
purgeCSS: {
whitelist: (defaultWhitelst) => defaultWhitelst.slice(1),
}
}
Only one extractor can be applied to each file extention. If you want to apply a custom extractor to the extensions that the default extractor already covers, you have to override the default extractor. This is only possible with the functional notation.
//nuxt.config.js
export default {
buildModules: [
'nuxt-purgecss',
],
purgeCSS: {
extractors: () => [
{
extractor(content) {
return content.match(/[A-z0-9-:\\/]+/g)
},
extensions: ['html', 'vue', 'js']
},
{
extractor(content) {
return content.match(/[A-z0-9-\\/]+/g)
},
extensions: ['vue'] // This will not work, because the above extractor is applied to 'vue' already.
}
]
}
}
- Review the Release Notes for all changes
- Bump to 1.x
- Ensure the plugin is running in the right mode
- If you used the default mode, you have to add
mode: 'webpack'
to your config. - If you used the
postcss
mode, you can remove themode: 'postcss'
line from your config - If you used this module only with the Nuxt
tailwind
module, you don't need to do anything
- If you used the default mode, you have to add
- Read about the internal changes of PurgeCSS 2
- Update your extractor and change the syntax from a class to a function (see 4.)
- Unused styles from SFCs are now purged. If you don't want that, whitelist them.
- The regex for CSS classes changed. This should not be breaking in most cases.
- The whitelist now includes nuxt link classes (e.g.
nuxt-link-active
). If you whitelisted these before, you can remove them. - The whitelist now includes move transition classes. If you whitelisted these before, you can remove them.
- Test on your staging server (or locally) before deploying!
Copyright (c) Alexander Lichter