-
-
Save danklammer/78a85964b8e97ded1af83625c41b9c53 to your computer and use it in GitHub Desktop.
Eleventy: Purge CSS for each html file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { PurgeCSS } = require('purgecss'); | |
/** | |
* Remove any CSS not used on the page and inline the remaining CSS in the | |
* <head>. | |
* | |
* @see {@link https://github.com/FullHuman/purgecss} | |
*/ | |
eleventyConfig.addTransform('purge-and-inline-css', async (content, outputPath) => { | |
if (process.env.ELEVENTY_ENV !== 'production' || !outputPath.endsWith('.html')) { | |
return content; | |
} | |
const purgeCSSResults = await new PurgeCSS().purge({ | |
content: [{ raw: content }], | |
css: ['dist/css/style.css'], | |
keyframes: true, | |
}); | |
return content.replace('<!-- INLINE CSS-->', '<style>' + purgeCSSResults[0].css + '</style>'); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% if site.environment === 'production' %} | |
<!-- INLINE CSS--> | |
{% else %} | |
<link rel="stylesheet" href="/css/style.css"> | |
{% endif %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment