postcss-functions is a PostCSS plugin that allows developers to expose JavaScript functions for direct invocation within CSS files during the build process. It enables dynamic value generation, complex calculations, and programmatic transformations of CSS properties, extending the capabilities of standard CSS. The current stable version is 4.0.2, maintaining compatibility with PostCSS v8.x. While release cadence isn't strictly defined, major version updates (like 4.0.0) indicate significant changes and active maintenance. Its key differentiator is providing a clean bridge between JavaScript logic and CSS styling without relying on preprocessors, allowing for highly customized and reusable CSS utility functions defined in JavaScript.
npm install postcss-functionsVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic setup, defining custom JavaScript functions, and applying them within CSS using postcss-functions.
Instead of using glob patterns, manually `import` your JavaScript function files into your PostCSS configuration file and pass them directly to the `functions` option object. You can use a separate globbing library like `fast-glob` in your build script if you still require dynamic loading.
Ensure all exposed JavaScript functions explicitly return a string that represents a valid CSS value (e.g., 'red', '10px', 'rgb(0,0,0)').
Inside your JavaScript function, use `parseFloat()`, `parseInt()`, `JSON.parse()`, or other conversion methods as needed to handle string arguments, e.g., `const value = parseFloat(arg);`.
Ensure you are using `import functions from 'postcss-functions';` and then calling it like `postcss().use(functions({ /* options */ }))`.Install `postcss` alongside `postcss-functions`: `npm install --save-dev postcss postcss-functions` or `yarn add --dev postcss postcss-functions`.
Verify that the function name used in your CSS matches the key in the `functions` object passed to `postcss-functions`, and that the function object is correctly passed to the plugin.