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 🙏

© 2026 – Pkg Stats / Ryan Hefner

postcss-auto-var-fallback

v1.1.0

Published

A PostCSS plugin that automatically adds fallback values to CSS variables based on their definitions in other CSS files.

Readme

PostCSS Auto Var Fallback

npm npm license

A PostCSS plugin that automatically adds fallback values to CSS variables based on their definitions in other CSS files.

Why CSS Variable Fallbacks Matter

CSS Custom Properties (variables) are powerful, but they have a key limitation: when a variable is undefined, the property using it becomes invalid. This can lead to unexpected styling issues, especially in:

  • Theming systems where variables might be defined in different files
  • Component libraries where you want to provide default values
  • Multi-tenant applications with customizable styling
  • Progressive enhancement scenarios where you need fallbacks

This plugin solves these problems by automatically adding fallbacks to your CSS variables based on their definitions in other files, ensuring your styles degrade gracefully.

Features

  • 🔄 Automatically adds fallbacks to CSS variables
  • 📁 Uses variable definitions from multiple CSS files
  • 🔄 Resolves nested variable references
  • ⚠️ Handles circular references gracefully
  • 🚀 Optimized with caching for performance
  • 🧪 Thoroughly tested with a comprehensive test suite

Installation

npm install postcss-auto-var-fallback --save-dev

or

pnpm add postcss-auto-var-fallback -D

Usage

Basic Usage

// postcss.config.js
module.exports = {
    plugins: [
        require('postcss-auto-var-fallback')({
            fallbacks: [
                './src/styles/variables.css',
                './src/styles/theme.css',
                require.resolve("@yourcompany-designsystem/theme.css")
            ]
        })
    ]
}

With Webpack

// webpack.config.js
module.exports = {
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          'style-loader',
          'css-loader',
          {
            loader: 'postcss-loader',
            options: {
              postcssOptions: {
                plugins: [
                  require('postcss-auto-var-fallback')({
                    fallbacks: ['./src/styles/variables.css']
                  })
                ]
              }
            }
          }
        ]
      }
    ]
  }
}

How It Works

Given the following CSS files:

variables.css:

:root {
  --primary-color: #3498db;
  --secondary-color: #2ecc71;
  --font-size: 16px;
  --spacing: var(--font-size);
}

component.css:

.button {
  background-color: var(--primary-color);
  color: var(--text-color);
  padding: var(--spacing);
}

After processing with this plugin:

.button {
  background-color: var(--primary-color, #3498db);
  color: var(--text-color); /* Unchanged because --text-color is not defined */
  padding: var(--spacing, 16px); /* Resolves nested variables */
}

Options

| Option | Type | Description | Default | |--------|------|-------------|---------| | fallbacks | Array<string> | Paths to CSS files containing variable definitions. Later files override earlier ones. | [] |

Advanced Examples

Handling Nested Variables

The plugin can resolve deeply nested variable references:

variables.css:

:root {
  --base-size: 4px;
  --spacing-unit: var(--base-size);
  --spacing-small: calc(var(--spacing-unit) * 2);
  --spacing-medium: calc(var(--spacing-unit) * 4);
}

component.css:

.card {
  padding: var(--spacing-medium);
}

After processing:

.card {
  padding: var(--spacing-medium, calc(4px * 4));
}

Theme Overrides

The plugin respects the order of fallback files, with later files taking precedence:

base-theme.css:

:root {
  --primary-color: blue;
}

custom-theme.css:

:root {
  --primary-color: purple;
}

When configured with fallbacks: ['base-theme.css', 'custom-theme.css'], the plugin will use purple as the fallback value.

Best Practices

  1. Order your fallback files by priority - The last file in the array has the highest precedence.
  2. Use relative paths - Paths are resolved relative to the CSS file being processed.
  3. Avoid circular references - The plugin detects and skips circular variable references.
  4. Consider file size - For large projects, be selective about which variables need fallbacks.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments