postcss-unit-processor
Advanced tools
Comparing version
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" | ||
} | ||
} |
@@ -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; | ||
} | ||
``` |
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
22224
88.71%6
50%486
154.45%0
-100%141
2.92%4
Infinity%