Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always run PurgeCSS if the project configures it #2

Merged
merged 5 commits into from
Jan 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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