postcss-resolution-independence
Advanced tools
Comparing version 1.0.1 to 1.1.0
52
index.js
@@ -1,3 +0,2 @@ | ||
const postcss = require('postcss'); | ||
const parser = require('postcss-values-parser'); | ||
const {parse} = require('postcss-values-parser'); | ||
@@ -24,3 +23,3 @@ /** | ||
*/ | ||
module.exports = postcss.plugin('postcss-resolution-independence', | ||
module.exports = | ||
({ | ||
@@ -37,26 +36,31 @@ baseSize = 24, | ||
return (css) => { | ||
css.walkDecls(decl => { | ||
const nodes = parser(decl.value, {loose: true}).parse() | ||
nodes.walkNumberNodes(node => { | ||
const value = parseFloat(node.value) | ||
// The standard unit to convert (if no unit, we assume the base unit) | ||
if (node.unit === unit) { | ||
const scaledValue = Math.abs(value * minScaleFactor); | ||
if (scaledValue && scaledValue <= minUnitSize) { | ||
if (Math.abs(value) >= minUnitSize) { | ||
node.value = minUnitSize * (value < 0 ? -1 : 1); | ||
return { | ||
postcssPlugin: 'postcss-resolution-independence', | ||
Once (css) { | ||
css.walkDecls(decl => { | ||
const nodes = parse(decl.value, {ignoreUnknownWords: true}) | ||
nodes.walkNumerics(node => { | ||
const value = parseFloat(node.value) | ||
// The standard unit to convert (if no unit, we assume the base unit) | ||
if (node.unit === unit) { | ||
const scaledValue = Math.abs(value * minScaleFactor); | ||
if (scaledValue && scaledValue <= minUnitSize) { | ||
if (Math.abs(value) >= minUnitSize) { | ||
node.value = minUnitSize * (value < 0 ? -1 : 1); | ||
} | ||
} else { | ||
node.value = parseFloat((value / baseSize).toFixed(precision)); | ||
node.unit = riUnit; | ||
} | ||
} else { | ||
node.value = parseFloat((value / baseSize).toFixed(precision)); | ||
node.unit = riUnit; | ||
} else if (node.unit === absoluteUnit) { | ||
// The absolute unit to convert to our standard unit | ||
node.unit = unit; | ||
} | ||
} else if (node.unit === absoluteUnit) { | ||
// The absolute unit to convert to our standard unit | ||
node.unit = unit; | ||
} | ||
}); | ||
decl.value = nodes.toString(); | ||
}); | ||
decl.value = nodes.toString(); | ||
}); | ||
} | ||
}; | ||
}); | ||
}; | ||
module.exports.postcss = true; |
{ | ||
"name": "postcss-resolution-independence", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Resolution independence conversion plugin for PostCSS", | ||
@@ -33,9 +33,12 @@ "author": "Jason Robitaille <jason.robitaille@lge.com>", | ||
"dependencies": { | ||
"postcss": "^7.0.0", | ||
"postcss-values-parser": "^2.0.1" | ||
"postcss-values-parser": "^6.0.2" | ||
}, | ||
"devDependencies": { | ||
"chai": "^4.3.4", | ||
"mocha": "^9.1.3" | ||
"mocha": "^9.1.3", | ||
"postcss": "^8.4.4" | ||
}, | ||
"peerDependencies": { | ||
"postcss": "^8.0.0" | ||
}, | ||
"scripts": { | ||
@@ -42,0 +45,0 @@ "test": "mocha --reporter spec" |
const chai = require('chai'); | ||
const postcss = require('postcss'); | ||
const parser = require('postcss-values-parser'); | ||
const {parse} = require('postcss-values-parser'); | ||
const riPlugin = require('..'); | ||
@@ -13,3 +13,3 @@ | ||
.then(function (result) { | ||
return parser(result.root.nodes[0].value, {loose:true}).parse(); | ||
return parse(result.root.nodes[0].value, {ignoreUnknownWords:true}); | ||
}) | ||
@@ -34,4 +34,4 @@ ); | ||
return processValue().then(out => { | ||
expect(parseFloat(out.nodes[0].nodes[0].value)).to.equal(1.5); | ||
expect(out.nodes[0].nodes[0].unit).to.equal('rem'); | ||
expect(parseFloat(out.nodes[0].value)).to.equal(1.5); | ||
expect(out.nodes[0].unit).to.equal('rem'); | ||
}); | ||
@@ -43,4 +43,4 @@ }); | ||
return processValue().then(out => { | ||
expect(parseFloat(out.nodes[0].nodes[0].value)).to.equal(2); | ||
expect(out.nodes[0].nodes[0].unit).to.equal('vh'); | ||
expect(parseFloat(out.nodes[0].value)).to.equal(2); | ||
expect(out.nodes[0].unit).to.equal('vh'); | ||
}); | ||
@@ -52,4 +52,4 @@ }); | ||
return processValue({unit: 'em'}).then(out => { | ||
expect(parseFloat(out.nodes[0].nodes[0].value)).to.equal(2); | ||
expect(out.nodes[0].nodes[0].unit).to.equal('rem'); | ||
expect(parseFloat(out.nodes[0].value)).to.equal(2); | ||
expect(out.nodes[0].unit).to.equal('rem'); | ||
}); | ||
@@ -61,4 +61,4 @@ }); | ||
return processValue({unit: 'abspx'}).then(out => { | ||
expect(parseFloat(out.nodes[0].nodes[0].value)).to.equal(48); | ||
expect(out.nodes[0].nodes[0].unit).to.equal('px'); | ||
expect(parseFloat(out.nodes[0].value)).to.equal(48); | ||
expect(out.nodes[0].unit).to.equal('px'); | ||
}); | ||
@@ -70,4 +70,4 @@ }); | ||
return processValue({value: 0.24}).then(out => { | ||
expect(parseFloat(out.nodes[0].nodes[0].value)).to.equal(0.01); | ||
expect(out.nodes[0].nodes[0].unit).to.equal('rem'); | ||
expect(parseFloat(out.nodes[0].value)).to.equal(0.01); | ||
expect(out.nodes[0].unit).to.equal('rem'); | ||
}); | ||
@@ -79,4 +79,4 @@ }); | ||
return processValue({value: 9}).then(out => { | ||
expect(parseFloat(out.nodes[0].nodes[0].value)).to.equal(9); | ||
expect(out.nodes[0].nodes[0].unit).to.equal('px'); | ||
expect(parseFloat(out.nodes[0].value)).to.equal(9); | ||
expect(out.nodes[0].unit).to.equal('px'); | ||
}); | ||
@@ -89,4 +89,4 @@ }); | ||
return processValue().then(out => { | ||
expect(parseFloat(out.nodes[0].nodes[0].value)).to.equal(24); | ||
expect(out.nodes[0].nodes[0].unit).to.equal('px'); | ||
expect(parseFloat(out.nodes[0].value)).to.equal(24); | ||
expect(out.nodes[0].unit).to.equal('px'); | ||
}); | ||
@@ -99,4 +99,4 @@ }); | ||
return processValue().then(out => { | ||
expect(parseFloat(out.nodes[0].nodes[0].value)).to.equal(1.45); | ||
expect(out.nodes[0].nodes[0].unit).to.equal('rem'); | ||
expect(parseFloat(out.nodes[0].value)).to.equal(1.45); | ||
expect(out.nodes[0].unit).to.equal('rem'); | ||
}); | ||
@@ -103,0 +103,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22957
6
206
3
+ Addedcolor-name@1.1.4(transitive)
+ Addedis-url-superb@4.0.0(transitive)
+ Addednanoid@3.3.8(transitive)
+ Addedpicocolors@1.1.1(transitive)
+ Addedpostcss@8.5.0(transitive)
+ Addedpostcss-values-parser@6.0.2(transitive)
+ Addedquote-unquote@1.0.0(transitive)
+ Addedsource-map-js@1.2.1(transitive)
- Removedpostcss@^7.0.0
- Removedflatten@1.0.3(transitive)
- Removedindexes-of@1.0.1(transitive)
- Removedpicocolors@0.2.1(transitive)
- Removedpostcss@7.0.39(transitive)
- Removedpostcss-values-parser@2.0.1(transitive)
- Removedsource-map@0.6.1(transitive)
- Removeduniq@1.0.1(transitive)
Updatedpostcss-values-parser@^6.0.2