Skip to content

Instantly share code, notes, and snippets.

@Lego2012
Last active December 14, 2022 16:01
Show Gist options
  • Save Lego2012/c88d4fe5dc8b9130b344e045ac439557 to your computer and use it in GitHub Desktop.
Save Lego2012/c88d4fe5dc8b9130b344e045ac439557 to your computer and use it in GitHub Desktop.
11ty: Eleventy CSS Minification

Once installed, we'll need to add the cssmin filter to our Eleventy Config file. At the root of your project, create a file called .eleventy.js and add the following:

const CleanCSS = require("clean-css");

module.exports = function(eleventyConfig) {
  eleventyConfig.addFilter("cssmin", function(code) {
    return new CleanCSS({}).minify(code).styles;
  });
};

In your navbar.njk file inside the _includes folder, add the following inside the <head/>:

<head>
<!-- capture the CSS content as a Nunjucks variable -->
    {% set css %}
    {% include "css/navbar.css" %}
    {% endset %}
    <!-- feed it through our cssmin filter to minify -->
    <style>
      {{css | cssmin | safe}}
    </style>
</head>
. . .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment