Skip to content

Instantly share code, notes, and snippets.

@mandrasch
Last active October 21, 2024 08:10
Show Gist options
  • Select an option

  • Save mandrasch/ceb29fbde54d85fba4eff9e8680a284b to your computer and use it in GitHub Desktop.

Select an option

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/
@Nooshu

Nooshu commented Oct 18, 2024

Copy link
Copy Markdown

An ESM version for 11ty v3.0.0 looks like this:

import CleanCSS from 'clean-css';
import fs from 'fs';

eleventyConfig.on('afterBuild', () => {
  const inputFile = 'src/assets/css/main.css';
  const input = fs.readFileSync(inputFile, 'utf8');
  const output = new CleanCSS().minify(input);

  fs.writeFile('_site/assets/css/main.css', output.styles, (err) => {
    if (err) return console.log('Error minifying main.css' + err);
    //success
  });
});

@mandrasch

Copy link
Copy Markdown
Author

@Nooshu Cool, thanks very much for adding this information!

Thought about upgrading https://github.com/mandrasch/11ty-plain-bootstrap5 to v3 (and Vite) as well, but don't know If I'll find the time.

@Nooshu

Nooshu commented Oct 21, 2024

Copy link
Copy Markdown

No problem @mandrasch! I believe it will take a while for the community to migrate all resources across to v3! It may be that AI could help you with this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment