PostCSS JavaScript Functions
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.
Common errors
-
TypeError: functions is not a function
cause The `postcss-functions` module is imported incorrectly, or the imported value is not invoked as a function.fixEnsure you are using `import functions from 'postcss-functions';` and then calling it like `postcss().use(functions({ /* options */ }))`. -
Error: Cannot find module 'postcss'
cause The `postcss` peer dependency is not installed.fixInstall `postcss` alongside `postcss-functions`: `npm install --save-dev postcss postcss-functions` or `yarn add --dev postcss postcss-functions`. -
CSS: Unknown word at line X, column Y (e.g., `background-color: unknownFunction(arg);`)
cause A function called in CSS was not provided in the `functions` option or has a misspelled name.fixVerify 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.
Warnings
- breaking The `glob` feature, which allowed specifying function files via glob patterns, was removed in version 4.0.0. This change reduces package size and dependencies.
- gotcha Functions defined in the `functions` option must return valid CSS values. If a function returns an invalid or non-string value (e.g., `undefined`, `null`, a complex object), PostCSS may output unexpected CSS or throw an error during processing.
- gotcha Arguments passed to CSS functions from `postcss-functions` are always strings. If you expect numbers or booleans, you must explicitly parse them within your JavaScript function.
Install
-
npm install postcss-functions -
yarn add postcss-functions -
pnpm add postcss-functions
Imports
- functions
import { functions } from 'postcss-functions';import functions from 'postcss-functions';
- postcss
const postcss = require('postcss');import postcss from 'postcss';
- ProcessOptions
import { ProcessOptions } from 'postcss';
Quickstart
import fs from 'fs';
import postcss from 'postcss';
import functions from 'postcss-functions';
const inputCss = `
body {
/* Example using a custom JS function */
background-color: myCustomColor('darken', 'blue', 0.2);
}
.foo {
color: darken(red, 0.1);
}
`;
// Define custom JavaScript functions to be exposed to PostCSS
const customFunctions = {
myCustomColor: (mode, color, amount) => {
// A more complex example, could involve external libraries
if (mode === 'darken') {
// Simplified darkening logic for demonstration
return `rgba(0, 0, ${Math.floor(255 * (1 - parseFloat(amount)))}, 1)`;
}
return color;
},
darken: (value, frac) => {
// Basic example of a function, similar to the README
const darkenFactor = 1 - parseFloat(frac);
// In a real scenario, use a color manipulation library
// For demo, just return a string
return `rgba(0, 0, ${Math.floor(255 * darkenFactor)}, 1)`;
}
};
postcss()
.use(functions({ functions: customFunctions }))
.process(inputCss, { from: undefined })
.then((result) => {
console.log('Processed CSS:\n', result.css);
})
.catch((error) => {
console.error('PostCSS processing failed:', error);
});