Skip to content

Instantly share code, notes, and snippets.

@mandrasch
Last active September 8, 2022 13:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mandrasch/ceb29fbde54d85fba4eff9e8680a284b to your computer and use it in GitHub Desktop.
Save mandrasch/ceb29fbde54d85fba4eff9e8680a284b to your computer and use it in GitHub Desktop.
Eleventy: Minify CSS and overwrite file (without inline integration)
module.exports = function (eleventyConfig) {
// npm install --save-dev clean-css
eleventyConfig.on('afterBuild', () => {
const CleanCSS = require("clean-css");
const fs = require('fs');
// Run me after the build ends
var inputFile = 'src/assets/css/main.css';
var input = fs.readFileSync(inputFile, 'utf8');
var output = new CleanCSS().minify(input);
fs.writeFile('_site/assets/css/main.css', output.styles, function (err) {
if (err) return console.log('Error minifying main.css' + err);
//success
});
});
}
// if you want to use inline styles, see: https://www.11ty.dev/docs/quicktips/inline-css/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment