You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

postcss-unit-processor

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

postcss-unit-processor - npm Package Compare versions

Comparing version

to
1.1.0

.babelrc

41

index.js

@@ -1,2 +0,4 @@

const unitRegex = /"[^"]+"|'[^']+'|url\([^)]+\)|var\([^)]+\)|(\d*\.?\d+)(px|pt|pc|cm|mm|in|%|em|rem|ch|vh|vw|vmin|vmax|ex)/g;
const defaultUnits = [
'px', 'pt', 'pc', 'cm', 'mm', 'in', '%', 'em', 'rem', 'ch', 'vh', 'vw', 'vmin', 'vmax', 'ex'
];

@@ -49,5 +51,30 @@ const filterPropList = {

mediaQuery: false,
exclude: /node_modules/i
exclude: /node_modules/i,
customUnitList: []
};
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
function createUnitRegex(customUnitList) {
let userUnits = Array.isArray(customUnitList)
? customUnitList.filter(
(u) => typeof u === 'string' && u.trim() && /^[a-zA-Z%]+$/.test(u)
)
: [];
const unitSet = new Set(defaultUnits);
for (const u of userUnits) {
unitSet.add(u);
}
const unitStr = Array.from(unitSet).map(escapeRegExp).join('|');
return new RegExp(
`"[^"]+"|'[^']+'|url\\([^)]+\\)|var\\([^)]+\\)|(\\d*\\.?\\d+)(${unitStr})`,
'g'
);
}
function createUnitReplace(processor, unitPrecision, root) {

@@ -100,3 +127,3 @@ return (node) => (m, $1, $2) => {

return selector.match(regex);
return regex.test(selector);
});

@@ -156,5 +183,8 @@ }

const exclude = opts.exclude;
const customUnitList = opts.customUnitList
let isExcludeFile = false;
let unitReplace;
const unitRegex = createUnitRegex(customUnitList);
return {

@@ -168,2 +198,3 @@ postcssPlugin: "postcss-unit-processor",

exclude &&
filePath &&
((type.isFunction(exclude) && exclude(filePath)) ||

@@ -186,2 +217,4 @@ (type.isString(exclude) && filePath.indexOf(exclude) !== -1) ||

unitRegex.lastIndex = 0;
if (

@@ -216,2 +249,4 @@ !unitRegex.test(decl.value) ||

unitRegex.lastIndex = 0;
if (opts.mediaQuery && atRule.name === "media") {

@@ -218,0 +253,0 @@ if (atRule.__unitProcessorFinished === true || !unitRegex.test(atRule.params)) {

{
"name": "postcss-unit-processor",
"version": "1.0.1",
"version": "1.1.0",
"description": "PostCSS plugin to process css unit.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "jest",
"test:coverage": "jest --coverage"
},

@@ -26,3 +27,9 @@ "repository": {

"postcss": "^8.0.0"
},
"devDependencies": {
"@babel/preset-env": "^7.28.0",
"babel-jest": "^30.0.4",
"jest": "^30.0.4",
"postcss": "^8.5.6"
}
}

46

README.md

@@ -17,22 +17,2 @@ # postcss-unit-processor

### Input/Output
```css
// input
h1 {
margin: 0 0 20px;
font-size: 32px;
line-height: 1.2;
letter-spacing: 1px;
}
// output
h1 {
margin: 0 0 20px;
font-size: 32px;
line-height: 1.2;
letter-spacing: 1px;
}
```
### Example

@@ -74,3 +54,4 @@

mediaQuery: false,
exclude: /node_modules/i
exclude: /node_modules/i,
customUnitList: []
}

@@ -110,2 +91,6 @@ ```

- `function (file) { return file.indexOf('exclude') !== -1; }`
- `customUnitList` (Array) List of custom units to process in addition to default units.
- Values should be strings representing unit names (e.g., `['dp', 'rpx']`).
- Only non-empty strings containing letters and the percentage sign are accepted.
- Custom units are added to the default set of units like `px`, `rem`, `vw`, etc.

@@ -140,1 +125,20 @@ ### Use with gulp-postcss and autoprefixer

```
#### Input/Output
```css
// input
h1 {
margin: 0 0 20px;
font-size: 32px;
line-height: 1.2;
letter-spacing: 1px;
}
// output
h1 {
margin: 0 0 10px;
font-size: 16px;
line-height: 1.2;
letter-spacing: 0.5px;
}
```