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

@veritashealth/postcss-critical-css

v4.0.2

Published

Generate critical CSS using PostCSS

Downloads

13

Readme

PostCSS Critical CSS

This plugin allows the user to define and output critical CSS using custom atRules, and/or custom CSS properties. Critical CSS may be output to one or more files, as defined within the plugin options and/or within the CSS. Depending on the plugin options used, processed CSS may be left unchanged, or critical CSS may be removed from it.

Install

npm install @veritashealth/postcss-critical-css --save

Examples

An example is available in this repo. See the /example directory, and use the command npm run example to test it out.

Usage examples

All examples given below show the input CSS and the critical CSS that is output from it. Note that the input CSS will remain unchanged, unless preserve is set to false in the plugin options. Use npm run example to see how this works.

Using the @critical atRule

/* In foo.css */
@critical;

.foo {
  border: 3px solid gray;
  display: flex;
  padding: 1em;
}

Will output:

/* In critical.css */
.foo {
  border: 3px solid gray;
  display: flex;
  padding: 1em;
}

Using the @critical atRule with a custom file path

/* In foo.css */
@critical bar.css;

.foo {
  border: 3px solid gray;
  display: flex;
  padding: 1em;
}

Will output:

/* In bar.css */
.foo {
  border: 3px solid gray;
  display: flex;
  padding: 1em;
}

Using the @critical atRule with multiple custom file paths

/* In foo.css */
@critical bar.css baz.css;

.foo {
  border: 3px solid gray;
  display: flex;
  padding: 1em;
}

Will output:

/* In bar.css and also in baz.css*/
.foo {
  border: 3px solid gray;
  display: flex;
  padding: 1em;
}

Using the @critical atRule with a subset of styles

/* In foo.css */
.foo {
  border: 3px solid gray;
  display: flex;
  padding: 1em;
}

@critical {
  .bar {
    border: 10px solid gold;
    color: gold;
  }
}

Will output:

/* In critical.css */
.bar {
  border: 10px solid gold;
  color: gold;
}

Using the @critical atRule with a subset of styles

/* In foo.css */
.foo {
  border: 3px solid gray;
  display: flex;
  padding: 1em;
}

@critical critical-1.css critical-2.css {
  .bar {
    border: 10px solid gold;
    color: gold;
  }
}

Will output:

/* In critical-1.css and also in critical-2.css */
.bar {
  border: 10px solid gold;
  color: gold;
}

Using the custom property, critical-selector

/* In foo.css */
.foo {
  critical-selector: this;
  border: 3px solid gray;
  display: flex;
  padding: 1em;
}

Will output:

/* In critical.css */
.foo {
  border: 3px solid gray;
  display: flex;
  padding: 1em;
}

Using the custom property, critical-selector, with a custom selector.

/* In foo.css */
.foo {
  critical-selector: .bar;
  border: 3px solid gray;
  display: flex;
  padding: 1em;
}

Will output:

/* In critical.css */
.bar {
  border: 3px solid gray;
  display: flex;
  padding: 1em;
}

Using the custom property, critical-filename

/* in foo.css */
.foo {
  critical-selector: this;
  critical-filename: secondary-critical.css;
  border: 3px solid gray;
  display: flex;
  padding: 1em;
}

Will output:

/* In secondary-critical.css */
.foo {
  border: 3px solid gray;
  display: flex;
  padding: 1em;
}

Using the custom property, critical-selector, with value scope

This allows the user to output the entire scope of a module, including children.

/* in foo.css */
.foo {
  critical-selector: scope;
  border: 3px solid gray;
  display: flex;
  padding: 1em;
}

.foo a {
  color: blue;
  text-decoration: none;
}

Will output:

/* In critical.css */
.foo {
  border: 3px solid gray;
  display: flex;
  padding: 1em;
}

.foo a {
  color: blue;
  text-decoration: none;
}

Plugin options

The plugin takes a single object as its only parameter. The following properties are valid:

| Arg | Type | Description | Default | ---|---|---|---| | outputPath | string | Path to which critical CSS should be output | Current working directory | | outputDest | string | Default critical CSS file name | "critical.css" | | preserve | boolean | Whether or not to remove selectors from primary CSS document once they’ve been marked as critical. This should prevent duplication of selectors across critical and non-critical CSS. | true | | minify | boolean | Minify output CSS? | true | | destDelim | string | Delimiter that separates multiple critical destination files. | ” “ |