Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
postcss-functions
Advanced tools
The postcss-functions npm package allows you to define and use custom functions within your CSS. This can be useful for a variety of tasks such as calculations, transformations, and dynamic value generation.
Custom Function Definitions
This feature allows you to define custom functions that can be used within your CSS. In the example, a function `calcWidth` is defined to add two pixel values together.
const postcss = require('postcss');
const functions = require('postcss-functions');
const css = `
.example {
width: calcWidth(100px, 50px);
}
`;
const output = postcss([
functions({
functions: {
calcWidth: (value1, value2) => {
return parseFloat(value1) + parseFloat(value2) + 'px';
}
}
})
]).process(css).css;
console.log(output);
Dynamic Value Generation
This feature allows you to generate dynamic values within your CSS. In the example, a function `dynamicColor` is defined to generate a random color.
const postcss = require('postcss');
const functions = require('postcss-functions');
const css = `
.example {
color: dynamicColor();
}
`;
const output = postcss([
functions({
functions: {
dynamicColor: () => {
return '#'+Math.floor(Math.random()*16777215).toString(16);
}
}
})
]).process(css).css;
console.log(output);
String Manipulation
This feature allows you to manipulate strings within your CSS. In the example, a function `appendText` is defined to concatenate two strings.
const postcss = require('postcss');
const functions = require('postcss-functions');
const css = `
.example {
content: appendText('Hello', ' World');
}
`;
const output = postcss([
functions({
functions: {
appendText: (text1, text2) => {
return `'${text1 + text2}'`;
}
}
})
]).process(css).css;
console.log(output);
The postcss-mixins package allows you to define and use mixins in your CSS. While it doesn't provide custom functions, it offers a way to reuse chunks of CSS code, which can achieve similar results in some cases.
The postcss-simple-vars package allows you to use variables in your CSS. While it doesn't provide custom functions, it can be used to store and reuse values, which can simplify complex CSS.
The postcss-calc package allows you to reduce calc() references in your CSS. It doesn't provide custom functions but can simplify mathematical expressions in your CSS.
PostCSS plugin for exposing JavaScript functions.
npm install --save-dev postcss postcss-functions
import fs from 'fs';
import postcss from 'postcss';
import functions from 'postcss-functions';
const options = {
//options
};
const css = fs.readFileSync('input.css', 'utf8');
postcss()
.use(functions(options))
.process(css)
.then((result) => {
const output = result.css;
});
Example of a function call:
body {
prop: foobar();
}
functions
Type: Object
An object containing functions. The function name will correspond with the object key.
Example:
import postcssFunctions from 'postcss-functions';
import { fromString, fromRgb } from 'css-color-converter';
function darken(value, frac) {
const darken = 1 - parseFloat(frac);
const rgba = fromString(value).toRgbaArray();
const r = rgba[0] * darken;
const g = rgba[1] * darken;
const b = rgba[2] * darken;
return fromRgb([r,g,b]).toHexString();
}
postcssFunctions({
functions: { darken }
});
.foo {
/* make 10% darker */
color: darken(blue, 0.1);
}
glob
?Versions prior to 4.0.0 had a globbing feature built in, but I've since decided to remove this feature from postcss-functions
. This means one less dependency and a smaller package size. For people still interested in this feature, you are free to pair postcss-functions
with the globbing library of your choice and pass the import
ed JavaScript files to the functions
option as described above.
FAQs
PostCSS plugin for exposing JavaScript functions
The npm package postcss-functions receives a total of 180,547 weekly downloads. As such, postcss-functions popularity was classified as popular.
We found that postcss-functions demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.