Skip to content

Commit

Permalink
Merge pull request #2 from webfactory/purgecss-support
Browse files Browse the repository at this point in the history
Add support for PurgeCSS
  • Loading branch information
polarbirke committed Jan 18, 2021
2 parents fa594a8 + 3e12caa commit 0fe51f3
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 6 deletions.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,41 @@ module.exports = {
"npmdir": "node_modules"
}
```

### Additional config options

#### PurgeCSS

[PurgeCSS](https://purgecss.com/) is a tool to automatically remove unused CSS. webfactory-gulp-preset comes with
postcss-purgecss preinstalled, but it is only activated if a `purgeCss` object is defined as a property of the
`styles` object in `gulp-config.js`.

All documented options (for the PostCSS implementation) are supported.

Example (excerpt from `gulp-config.js`):

```js
// […]
styles: {
files: [
{
name: 'main.css',
files: [
'PATH_TO_PROJECT_ASSETS_DIR/scss/main.scss',
],
destDir: 'css'
}
],
purgeCss: {
content: [
'./src/PROJECT_TEMPLATES/**/*.twig',
'./src/JS_COMPONENTS_THAT_USE_CSS_SELECTORS/**/*.js',
'./vendor/PACKAGE/TEMPLATES/**/*.twig',
],
safelist: ['ANY_VALID_CSS_SELECTOR']
},
postCssPlugins: postCssPlugins,
watch: ['src/**/*.scss']
},
// […]
```
27 changes: 24 additions & 3 deletions config/postcss-plugins-default.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
const $ = require('../plugins')(); /// lädt alle gulp-*-Module in $.*
const $ = require('../plugins')(); /// load all gulp-*-Modules in $.*

// This custom extractor will also match selectors that contain
// special chars like "_", ":", "\" and "@"
function utilityCssExtractor(content) {
return content.match(/[a-zA-Z0-9-_:@\/]+/g)
}

function postCssPlugins(config, stylesheet) {
// Grab a PurgeCSS config to use;
// always prefers a stylesheet-specific one over a global config for all CSS files
let purgeCssConfig = stylesheet.purgeCss || config.styles.purgeCss;

return [
$.autoprefixer(),
$.postcssurl({
Expand All @@ -10,8 +20,19 @@ function postCssPlugins(config, stylesheet) {
}
return $.path.relative(`${config.webdir}/${stylesheet.destDir}/`, asset.absolutePath).split("\\").join("/");
}
})
];
}),
// conditionally run PurgeCSS if any config (local or global) was found
purgeCssConfig ? $.purgecss({
content: purgeCssConfig.content,
extractors: [
{
extractor: utilityCssExtractor,
extensions: ['twig', 'js']
}
],
safelist: purgeCssConfig.safelist
}) : false
].filter(Boolean); // Strip falsy values (this enables conditional plugins like PurgeCSS)
}

exports.postCssPlugins = postCssPlugins;
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "webfactory-gulp-preset",
"version": "1.0.5",
"version": "1.1.0",
"dependencies": {
"@fullhuman/postcss-purgecss": "^3.0.0",
"@fullhuman/postcss-purgecss": "^3.1.3",
"autoprefixer": "^10.0.0",
"browser-sync": "^2.26.7",
"browserslist-config-webfactory": "^1.0.0",
Expand Down
3 changes: 2 additions & 1 deletion plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ module.exports = function() {
$['browserSync'] = require('browser-sync').create();
$['log'] = require('fancy-log');
$['path'] = require('path');
$['postcssurl'] = require("postcss-url");
$['postcssurl'] = require('postcss-url');
$['purgecss'] = require('@fullhuman/postcss-purgecss');
$['through2'] = require('through2');

return $;
Expand Down

0 comments on commit 0fe51f3

Please sign in to comment.