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

@freddy38510/critters-webpack-plugin

v3.2.0

Published

Webpack plugin to inline critical CSS and lazy-load the rest.

Downloads

5

Readme

critters-webpack-plugin inlines your app's critical CSS and lazy-loads the rest.

critters-webpack-plugin npm

It's a little different from other options, because it doesn't use a headless browser to render content. This tradeoff allows Critters to be very fast and lightweight. It also means Critters inlines all CSS rules used by your document, rather than only those needed for above-the-fold content. For alternatives, see Similar Libraries.

Critters' design makes it a good fit when inlining critical CSS for prerendered/SSR'd Single Page Applications. It was developed to be an excellent compliment to prerender-loader, combining to dramatically improve first paint time for most Single Page Applications.

Features

  • Fast - no browser, few dependencies
  • Integrates with html-webpack-plugin
  • Works with webpack-dev-server / webpack serve
  • Supports preloading and/or inlining critical fonts
  • Prunes unused CSS keyframes and media queries
  • Removes inlined CSS rules from lazy-loaded stylesheets

Installation

First, install Critters as a development dependency:

npm i -D critters-webpack-plugin

Then, import Critters into your Webpack configuration and add it to your list of plugins:

// webpack.config.js
+const Critters = require('critters-webpack-plugin');

module.exports = {
  plugins: [
+    new Critters({
+      // optional configuration (see below)
+    })
  ]
}

That's it! Now when you run Webpack, the CSS used by your HTML will be inlined and the imports for your full CSS will be converted to load asynchronously.

Usage

Critters

Create a Critters plugin instance with the given options.

Parameters

  • options Options Options to control how Critters inlines CSS.

Examples

// webpack.config.js
module.exports = {
  plugins: [
    new Critters({
      // Outputs: <link rel="preload" onload="this.rel='stylesheet'">
      preload: 'swap',

      // Don't inline critical font-face rules, but preload the font URLs:
      preloadFonts: true,
    }),
  ],
};

Critters

All optional. Pass them to new Critters({ ... }).

Parameters

  • options

Properties

  • external Boolean Inline styles from external stylesheets (default: true)
  • inlineThreshold Number Inline external stylesheets smaller than a given size (default: 0)
  • minimumExternalSize Number If the non-critical external stylesheet would be below this size, just inline it (default: 0)
  • pruneSource Boolean Remove inlined rules from the external stylesheet (default: true)
  • mergeStylesheets Boolean Merges inlined stylesheets into a single <style> tag (default: true)
  • additionalStylesheets String[] Glob for matching other stylesheets which should be used to evaluate critical CSS (default: '')
  • preload String Which preload strategy to use
  • noscriptFallback Boolean Add <noscript> fallback to JS-based strategies
  • inlineFonts Boolean Inline critical font-face rules (default: false)
  • preloadFonts Boolean Preloads critical fonts (default: true)
  • fonts Boolean Shorthand for setting inlineFonts+preloadFonts- Values:
    • true to inline critical font-face rules and preload the fonts
    • false to don't inline any font-face rules and don't preload fonts
  • keyframes String Controls which keyframes rules are inlined.- Values:
    • "critical": (default) inline keyframes rules used by the critical CSS
    • "all" inline all keyframes rules
    • "none" remove all keyframes rules
  • compress Boolean Compress resulting critical CSS (default: true)
  • logLevel String Controls log level of the plugin (default: "info")

LogLevel

Controls log level of the plugin. Specifies the level the logger should use. A logger will not produce output for any log level beneath the specified level. Available levels and order are:

  • "info" (default)
  • "warn"
  • "error"
  • "trace"
  • "debug"
  • "silent"

Type: ("info" | "warn" | "error" | "trace" | "debug" | "silent")

PreloadStrategy

The mechanism to use for lazy-loading stylesheets. [JS] indicates that a strategy requires JavaScript (falls back to <noscript>).

  • default: Move stylesheet links to the end of the document and insert preload meta tags in their place.
  • "body": Move all external stylesheet links to the end of the document.
  • "media": Load stylesheets asynchronously by adding media="not x" and removing once loaded. [JS]
  • "swap": Convert stylesheet links to preloads that swap to rel="stylesheet" once loaded. [JS]
  • "js": Inject an asynchronous CSS loader similar to LoadCSS and use it to load stylesheets. [JS]
  • "js-lazy": Like "js", but the stylesheet is disabled until fully loaded.

Type: (default | "body" | "media" | "swap" | "js" | "js-lazy")

Similar Libraries

There are a number of other libraries that can inline Critical CSS, each with a slightly different approach. Here are a few great options:

License

Apache 2.0

This is not an official Google product.