npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

eleventy-plugin-purgecss

v0.5.0

Published

Eleventy plugin that runs PurgeCSS after each build

Downloads

372

Readme

eleventy-plugin-purgecss

npm npm GitHub Workflow Status

Eleventy plugin that runs PurgeCSS after each build. PurgeCSS makes stylesheets smaller by inspecting the content they are used in, then discarding any CSS that is not referenced.

Inspired by the lovely jekyll-purgecss.

Installation

npm install --save eleventy-plugin-purgecss

Requires Eleventy 0.11.1 or higher.

Usage

This plugin executes PurgeCSS every time Eleventy builds your site and is designed to purge output CSS files in-place.

First, make sure you have a PurgeCSS configuration file, probably in the root of your project. Here is an example:

// purgecss.config.js

module.exports = {
  // Content files referencing CSS classes
  content: ["./_site/**/*.html"],

  // CSS files to be purged in-place
  css: ["./_site/**/*.css"],
};

Then register the plugin in .eleventy.js. By default, the plugin uses configuration from ./purgecss.config.js, but a different configuration file can be specified when registering the plugin.

// .eleventy.js

const purgeCssPlugin = require("eleventy-plugin-purgecss");

module.exports = function (eleventyConfig) {
  eleventyConfig.addPlugin(purgeCssPlugin, {
    // Optional: Specify the location of your PurgeCSS config
    config: "./purgecss.config.js",

    // Optional: Set quiet: true to suppress terminal output
    quiet: false,
  });
};

The CSS files specified in the configuration are now purged in-place every time the site is built.

Run only in production

Since purging CSS can take some time for larger sites, it may be beneficial to only enable this plugin when building for production. One way to do this is to wrap the plugin registration in a condition, like so:

if (process.env.NODE_ENV === "production") {
  eleventyConfig.addPlugin(purgeCssPlugin);
}

You can then enable the plugin by setting the NODE_ENV environment variable to "production". Environment variables can be configured graphically on platforms like Netlify, Vercel, and others. When building manually, the following command can be used in most shells:

NODE_ENV=production eleventy # or whatever your build command is

Troubleshooting

It can be useful to check that PurgeCSS removes the CSS you expect. If you enable the rejected or rejectedCss options in your PurgeCSS configuration, the plugin will output the purged CSS to the terminal (unless the plugin is set to quiet).

Known issues

Pre-1.0 versions of Eleventy do not wait for asynchronous event hooks before reloading the browser in --serve mode, which affects this plugin. This is not a problem when performing a regular build, but causes issues in --serve mode as Eleventy may reload the browser before the plugin has finished processing the files.

This was fixed in Eleventy 1.0, but if you're working with an older version, you might need to manually reload the page once the plugin reports "finished" to get the purged CSS.