-
Notifications
You must be signed in to change notification settings - Fork 8
Purge unused CSS utilities #1000
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
Conversation
This comment has been minimized.
This comment has been minimized.
thanks for doing this! we may want to test the admin theme too. also, you can see some quick screenshots of where there might be some issues: http://ronswanbot.herokuapp.com/ghost-inspector/suite-results/612d26c69bed453ad10dee52 |
@aekong Yeah, I expected there to be issues on the first run. I'm trying to walk that back by safe-listing all of the SCSS partials with |
Lookin better with all of the Sass safe listed! |
I could be wrong, but it looks like this PR might shave about 13K off our CSS bundle (according to Lighthouse):
|
require('postcss-import')({ | ||
filter: path => path.endsWith('.css') | ||
}), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is where we inline @import
statements, but only for paths that end in .css
(i.e., not Sass).
web/themes/custom/sfgovpl/src/sass/components/_last-updated.scss
Outdated
Show resolved
Hide resolved
web/themes/custom/sfgovpl/src/sass/components/_person-card.scss
Outdated
Show resolved
Hide resolved
web/themes/custom/sfgovpl/src/sass/components/cards/_featured-item.scss
Outdated
Show resolved
Hide resolved
web/themes/custom/sfgovpl/src/sass/components/cards/_location.scss
Outdated
Show resolved
Hide resolved
web/themes/custom/sfgovpl/src/sass/field/_field-related-depts.scss
Outdated
Show resolved
Hide resolved
web/themes/custom/sfgovpl/src/sass/paragraphs/_paragraph-facts.scss
Outdated
Show resolved
Hide resolved
web/themes/custom/sfgovpl/src/sass/paragraphs/_paragraph-people-3elected.scss
Outdated
Show resolved
Hide resolved
looks pretty good! did notice one thing based on the purged json report:
It looks like the safelist purge comment was already added to web/themes/custom/sfgovpl/src/sass/view/_view-meetings.scss, so not sure why the upcoming meetings view listing is off:
I thought it might be that the twig templates set the classnames in an array and then output them in a twig expression, but that's happening everywhere else, too,so not sure why these selectors are being purged. |
I think this is because of the way that postcss hoists
I know it might seem silly to add a bunch of tooling around safe-listing entire files, but it's one thing we did at GitHub that ended up being extremely helpful before big refactors. |
i'm good for this to get merged in, thanks for putting it together! do you think we should add the bit from your pr description:
to the readme? I'm hoping we'll steer clear of writing too much more custom css and hopefully start using more of the design system utilities, but while the design system matures we'll probaby have to, and it'd be good to have the heads up when building locally |
* Update README.md adding notes from helpful pr description * add links * update purgecss code links Co-authored-by: Shawn Allen <[email protected]>
This PR introduces PurgeCSS to our CSS build process to "purge" unused selectors from our stylesheets. Here's how it works:
rejected: true
tells PurgeCSS to collect and report information about the list of rejected ("purged") CSS when it runscontent: [...]
lists paths (mostly globs) where it should look for HTML elements, attributes, and any "token" that looks like a class name. Currently I have this set to pull in:templates/**/*.{twig,html}
to match all templates in this Drupal themesrc/**/*.js
to match class names that we may use to either query or add/remove in our JavaScript../../../modules/custom/**/*.{html,inc,php,theme,twig}
to match files that might contain classes in any of our custom modulescontent
globs listed above, and "rejects" selectors that don't match anything found in the content{filename}.css.purged.json
alongside each{filename}.css
, and outputs some useful info to the consoleSo now, when you run
npm run build-css
you should see some helpful output like this:I've added a new
utilities.css
bundle that importsdist/css/utilities.css
from the design system and purges utilities that aren't referenced in any of our templates. That specific@import
rule is handled by postcss-import, which I've configured to only inline imports whose URL ends in.css
, so that it doesn't conflict with the Sass imports.Testing
I think this is a combination of gut checking the rejected selector lists and QA, but in a nutshell: We need to make sure that this isn't rejecting the wrong selectors.
If there's CSS that's being purged from bundles besides
utilities.css
(which is basically purge-able by design because it includes every permutation of every utility class), then we should be deleting the corresponding (S)CSS! (Fun fact: creating this PR was actually how I discovered that all of the CSS in ourtwitter.css
bundle was irrelevant, which allowed it to be deleted in #998 💪 )Disabling PurgeCSS
If it looks like PurgeCSS is over-aggressively purging selectors that are used, we'll need to do one or both of the following:
Add
purgecess ignore
comments to CSS that we want to prevent from being purged. You can run scripts/add-purgecss-comments on one or more files to safe-list them entirely:Add safe lists to our PurgeCSS config to prevent specific selectors from being purged.