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

postcss-lightningcss

v1.0.1

Published

PostCSS plugin to use lightningcss

Downloads

29,522

Readme

postcss-lightningcss

Latest version License GitHub Workflow Status Sonar Coverage Sonar Quality Gate Status NPM Downloads

This PostCSS plugin uses lightningcss to compile and minify your CSS.

lightningcss is more than a minifier as it can replace several PostCSS plugins such as autoprefixer. You can find the complete list of features in the package's documentation and the list of plugins it can replace below.

Install

yarn add -D postcss-lightningcss

Usage

const postcss = require("postcss");
const postcssLightningcss = require("postcss-lightningcss");

postcss([postcssLightningcss(/* pluginOptions */)]).process(
  YOUR_CSS /*, processOptions */
);

Options

const postcssLightningcss = require("postcss-lightningcss");

postcssLightningcss({
  // Use a browserslist query that will inform which browsers are supported
  // Will add or remove vendor prefixes that are needed or not anymore
  browsers: ">= .25%",
  // Auto set lightningcssOptions.cssModules, possible values:
  // - auto - enable CSS modules for all files matching `/\.module(s)?\.\w+$/i.test(filename)`
  // - RegExp - enable CSS modules for all files matching `/RegExp/i.test(filename)`
  // - boolean - true enable css modules for all files
  // Will be overwrite by the lightningcssOptions cssModules(boolean) option
  cssModules: /\.module\.css/,

  // Callback containing the transformed class names for cssModules
  // this runs only if cssModules is enabled
  cssModulesJSON(cssFileName: string, json: object, outputFileName: string): {},
  // Will pass all options to lightningcss
  // Check out their documentation for details:
  // https://www.npmjs.com/package/lightningcss#user-content-documentation
  lightningcssOptions: {
    // Enable minification (default: true)
    minify: true,

    // Enable source maps (default: true)
    sourceMap: true,

    // Compile as cssModules (default: undefined)
    cssModules: false,

    // For which browsers to compile (default: undefined)
    // Can be omitted if you set the `browsers` option
    targets: {
      safari: (13 << 16) | (2 << 8), // represents Safari 13.2.0
    },

    // Individually enable various drafts
    drafts: {
      // Enable css nesting (default: undefined)
      nesting: false,
    },
  },
});

The detailed list of lightningcssOptions can be found here

CSS Modules

When transforming CSS Modules, class names get changed. To get the mapping of the class names, you can specify a cssModulesJSON function like the following:

const path = require("path");
const fs = require("fs");

postcssLightningcss({
  cssModules: /\.module\.css/,
  cssModulesJSON(cssFileName, json, outputFileName) {
    const cssName = path.basename(cssFileName, ".css");
    const jsonFileName = path.resolve("./build/" + cssName + ".json");
    fs.writeFileSync(jsonFileName, JSON.stringify(json));
  },
});

You may also combine this with custom naming patterns:

postcssLightningcss({
  cssModulesJSON(cssFileName, json, outputFileName) {
    // Your export function here
  },
  lightningcssOptions: {
    cssModules: {
      pattern: "my-company-[name]-[hash]-[local]",
    },
  },
});

Note that using a custom pattern cannot be used at the same time as using a RegEx to conditionally enable CSS modules because the cssModules option within lightningcssOptions takes precedence over the plugin's cssModules option

About source maps

Source maps will pass through lightningcss. But many mappings will be lost in translation; lightningcss creates only a source map for selectors. Mappings for properties cannot be re-created after this transformation.

PostCSS plugins that you can remove if you have lightningcss

This list is not exhaustive. If you have a doubt, have a look at the Lightning CSS Playground

The rows marked as "Depends on browser config" will convert your CSS only if:

  1. You set the browsers or lightningcssOptions.targets options
  2. one of the target browsers doesn't support the feature

| PostCSS Plugin | lightningcss option | | --------------------------------------- | ---------------------------------------- | | autoprefixer | Depends on browser config | | postcss-clamp | Depends on browser config | | postcss-color-hex-alpha | Depends on browser config | | postcss-color-hsl | Depends on browser config | | postcss-color-rgb | Depends on browser config | | postcss-color-function | Depends on browser config | | postcss-color-rebeccapurple | Depends on browser config | | postcss-custom-media | lightningcssOptions.drafts.customMedia | | postcss-double-position-gradients | Depends on browser config | | postcss-hwb-function | Depends on browser config | | postcss-lab-function | Depends on browser config | | postcss-logical | Depends on browser config (1) | | postcss-media-minmax | Depends on browser config | | postcss-multi-value-display | Depends on browser config | | postcss-nesting | lightningcssOptions.drafts.nesting | | postcss-normalize-display-values | Depends on browser config | | postcss-oklab-function | Depends on browser config | | postcss-overflow-shorthand | Depends on browser config | | postcss-place | Depends on browser config | | postcss-progressive-custom-properties | Depends on browser config (2) |

  1. lightningcss doesn't support the logical shorthand keyword as its syntax is likely to change
  2. Progressive custom properties work only if the properties don't contain properties themselves:
  • lab(29.2345% 39.3825 20.0664) has a proper fallback
  • oklch(40% 0.234 0.39 / var(--opacity-50)) will not have a fallback

License

MIT