Skip to content

Commit

Permalink
Merge pull request #195 from GSA-TTS/154-unused-css
Browse files Browse the repository at this point in the history
Explore adding Purgecss to remove unused selectors from CSS
  • Loading branch information
jonathanbobel authored Aug 5, 2024
2 parents 499955d + 70d0a4e commit 5845a09
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
31 changes: 31 additions & 0 deletions config/purgecss-options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const purgeCSSOptions = {
css: ["./_site/assets/styles/*.css"],
content: [
"./_site/**/*.{html,js,svg}",
],
defaultExtractor: content => content.match(/[\w-/:]+(?<!:)/g) || [],
safelist: {
standard: [
/abbr/,
"kbd",
"samp",
"sub",
"optgroup",
"fieldset",
"summary",
"cite",
"dfn",
"pre",
],
deep: [
/usa-in-page.+/
]
},
dynamicAttributes: [
'contentEditable',
'title',
'type'
]
};

module.exports = purgeCSSOptions;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"dev:assets": "npm run assets:refresh && npm run assets:watch",
"dev:debug": "DEBUG=* npx @11ty/eleventy --serve --watch",
"dev:serve": "npx @11ty/eleventy --serve --watch",
"federalist": "npm run build",
"federalist": "npm run build && ELEVENTY_ENV=production npm run assets:autoprefix",
"serve": "npx @11ty/eleventy --serve",
"start": "npx @11ty/eleventy --serve",
"test": "echo \"Error: no test specified\" && exit 1",
Expand Down
11 changes: 8 additions & 3 deletions postcss.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
const purgeCSSOptions = require("./config/purgecss-options");

module.exports = {
plugins: [require('autoprefixer')({
map: process.env.ELEVENTY_ENV !== 'production'
})],
plugins: [
require('autoprefixer')({
map: process.env.ELEVENTY_ENV !== 'production'
}),
...(process.env.ELEVENTY_ENV === 'production' ? [require("@fullhuman/postcss-purgecss")(purgeCSSOptions)] : [])
],
};

0 comments on commit 5845a09

Please sign in to comment.