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 🙏

© 2025 – Pkg Stats / Ryan Hefner

postcss-if-function

v0.1.8

Published

PostCSS plugin for transforming CSS if() functions to native CSS

Readme

postcss-if-function

Default CI/CD Pipeline npm version Build Status

A PostCSS plugin for transforming CSS if() functions into native CSS @media and @supports rules at build time.

This plugin is part of the css-if-polyfill project and provides build-time transformation of conditional CSS, eliminating the need for runtime JavaScript processing when using only media() and supports() functions.

Input CSS:

.responsive {
	width: if(media(width <= 768px): 100%; else: 50%);
}

Expected Output:

.responsive {
	width: 50%;
}
@media (width <= 768px) {
	.responsive {
		width: 100%;
	}
}

Installation

npm install postcss-if-function postcss

Usage

PostCSS CLI

# Transform CSS using PostCSS CLI
npx postcss input.css --output output.css --use postcss-if-function

# With custom PostCSS config file
npx postcss input.css --output output.css --config postcss.config.js

Basic Programmatic Usage

// Named export (recommended)
import postcss from "postcss";
import { postcssIfFunction } from "postcss-if-function";

// Or default export (for compatibility)
import postcss from "postcss";
import postcssIfFunction from "postcss-if-function";

const css = `
.example {
  color: if(media(max-width: 768px): blue; else: red);
  font-size: if(supports(display: grid): 1.2rem; else: 1rem);
}
`;

const result = await postcss([postcssIfFunction()]).process(css, {
	from: undefined
});

console.log(result.css);

Output:

.example {
	color: red;
}
@media (max-width: 768px) {
	.example {
		color: blue;
	}
}
.example {
	font-size: 1rem;
}
@supports (display: grid) {
	.example {
		font-size: 1.2rem;
	}
}

With Options

const result = await postcss([
	postcssIfFunction({
		logTransformations: true,
		preserveOriginal: false,
		skipSelectors: [".no-transform"]
	})
]).process(css, { from: undefined });

With PostCSS Config File

// postcss.config.js
import { postcssIfFunction } from "postcss-if-function";

export default {
	plugins: [
		postcssIfFunction({
			logTransformations: process.env.NODE_ENV === "development"
		})
	]
};

With Popular PostCSS Tools

Vite

// vite.config.js
import { defineConfig } from "vite";
import { postcssIfFunction } from "postcss-if-function";

export default defineConfig({
	css: {
		postcss: {
			plugins: [
				postcssIfFunction({
					logTransformations: process.env.NODE_ENV === "development"
				})
			]
		}
	}
});

Webpack

// webpack.config.js
module.exports = {
	module: {
		rules: [
			{
				test: /\.css$/,
				use: [
					"style-loader",
					"css-loader",
					{
						loader: "postcss-loader",
						options: {
							postcssOptions: {
								plugins: [
									[
										"postcss-if-function",
										{
											logTransformations: true
										}
									]
								]
							}
						}
					}
				]
			}
		]
	}
};

Next.js

// next.config.js
module.exports = {
	experimental: {
		postcss: {
			plugins: {
				"postcss-if-function": {
					logTransformations: process.env.NODE_ENV === "development"
				}
			}
		}
	}
};

Options

| Option | Type | Default | Description | | -------------------- | ---------- | ------- | -------------------------------------------------------------------------- | | preserveOriginal | boolean | false | Whether to preserve original CSS alongside transformations (for debugging) | | logTransformations | boolean | false | Whether to log transformation statistics to console | | skipSelectors | string[] | [] | Array of selectors to skip transformation for |

Supported Transformations

Media Queries

Input CSS:

.responsive {
	width: if(media(width <= 768px): 100%; else: 50%);
}

Expected Output:

.responsive {
	width: 50%;
}
@media (width <= 768px) {
	.responsive {
		width: 100%;
	}
}

Feature Support Queries

Input CSS:

.grid {
	display: if(supports(display: grid): grid; else: block);
}

Expected Output:

.grid {
	display: block;
}
@supports (display: grid) {
	.grid {
		display: grid;
	}
}

Multiple Conditions

Input CSS:

.responsive {
	padding: if(
		media(width >= 1200px): 40px; media(width >= 768px): 30px;
			media(width >= 480px): 20px; else: 15px
	);
}

Expected Output:

.responsive {
	padding: 15px;
}
@media (width >= 480px) {
	.responsive {
		padding: 20px;
	}
}
@media (width >= 768px) {
	.responsive {
		padding: 30px;
	}
}
@media (width >= 1200px) {
	.responsive {
		padding: 40px;
	}
}

Limitations

  • Style Functions Not Supported: This plugin only transforms media() and supports() functions. For style() functions (which depend on runtime DOM state), use the css-if-polyfill runtime (browser) library
  • Static Analysis Only: The plugin performs static analysis and cannot handle dynamically generated CSS
  • PostCSS Compatibility: Requires PostCSS 8.0.0 or higher

Integration with Runtime Polyfill

For complete CSS if() support including style() functions, combine this plugin with the runtime polyfill:

  1. Use this PostCSS plugin for build-time transformation of media() and supports()
  2. Use css-if-polyfill runtime for style() functions
<!-- For style() functions only -->
<script src="https://cdn.jsdelivr.net/npm/css-if-polyfill/dist/index.modern.js"></script>

Performance Considerations

This plugin is designed for optimal build-time performance, transforming CSS if() functions to native CSS without runtime overhead. However, there are some architectural considerations:

Current Implementation

  • Double Parsing: The plugin currently parses CSS twice - once by PostCSS and once by the transformation engine
  • String-based Transformation: The transformation engine outputs CSS strings that are re-parsed into PostCSS AST nodes

Future Optimization Opportunities

  • Reduced CSS Code: We'll make use of CSS Nesting capabilities as soon as any of the browsers are EOL (#35).
  • Direct AST Transformation: The transformation engine could be modified to output PostCSS AST nodes directly, eliminating the double parsing overhead
  • Streaming Processing: For very large CSS files, streaming transformation could reduce memory usage

For most typical usage scenarios, the current performance is excellent and the double parsing overhead is negligible compared to the benefits of build-time transformation.

Contributing

See the main Contributing Guide for details on how to contribute to this project.

License

MIT © Maximilian Franzke

Related

Further solutions