Skip to content

Commit 9ff8cac

Browse files
committed
3.0.3
1 parent a6499df commit 9ff8cac

File tree

4 files changed

+33
-20
lines changed

4 files changed

+33
-20
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changes to PostCSS color-mod() Function
22

3+
### 3.0.3 (September 23, 2018)
4+
5+
- Fixed an issue with certain colors not being tranformed from variables
6+
37
### 3.0.2 (September 23, 2018)
48

59
- Fixed an incompatibility with other `importFrom` plugins

lib/import-from.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fs from 'fs';
22
import path from 'path';
33
import postcss from 'postcss';
44
import getCustomProperties from './get-custom-properties';
5+
import valueParser from 'postcss-values-parser';
56

67
/* Import Custom Properties from CSS AST
78
/* ========================================================================== */
@@ -14,8 +15,8 @@ function importCustomPropertiesFromCSSAST(root) {
1415
/* ========================================================================== */
1516

1617
async function importCustomPropertiesFromCSSFile(from) {
17-
const css = await readFile(path.resolve(from));
18-
const root = postcss.parse(css, { from: path.resolve(from) });
18+
const css = await readFile(from);
19+
const root = postcss.parse(css, { from });
1920

2021
return importCustomPropertiesFromCSSAST(root);
2122
}
@@ -29,14 +30,18 @@ function importCustomPropertiesFromObject(object) {
2930
Object(object).customProperties || Object(object)['custom-properties']
3031
);
3132

33+
for (const prop in customProperties) {
34+
customProperties[prop] = valueParser(customProperties[prop]).parse();
35+
}
36+
3237
return customProperties;
3338
}
3439

3540
/* Import Custom Properties from JSON file
3641
/* ========================================================================== */
3742

3843
async function importCustomPropertiesFromJSONFile(from) {
39-
const object = await readJSON(path.resolve(from));
44+
const object = await readJSON(from);
4045

4146
return importCustomPropertiesFromObject(object);
4247
}
@@ -45,7 +50,7 @@ async function importCustomPropertiesFromJSONFile(from) {
4550
/* ========================================================================== */
4651

4752
async function importCustomPropertiesFromJSFile(from) {
48-
const object = await import(path.resolve(from));
53+
const object = await import(from);
4954

5055
return importCustomPropertiesFromObject(object);
5156
}
@@ -70,7 +75,7 @@ export default function importCustomPropertiesFromSources(sources) {
7075
}
7176

7277
// source pathname
73-
const from = String(opts.from || '');
78+
const from = path.resolve(String(opts.from || ''));
7479

7580
// type of file being read from
7681
const type = (opts.type || path.extname(from).slice(1)).toLowerCase();

lib/transform.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,34 +37,38 @@ export default function transformAST(node, opts) {
3737
function transformVariables(node, opts) {
3838
walk(node, child => {
3939
if (isVariable(child)) {
40-
const [variableName, fallbackNode] = transformArgsByParams(child, [
40+
// get the custom property and fallback value from var()
41+
const [prop, fallbackNode] = transformArgsByParams(child, [
4142
// <value> , [ <fallback> ]?
4243
[transformWord, isComma, transformNode]
4344
]);
4445

45-
if (variableName in opts.customProperties) {
46-
let customPropertyValue = opts.customProperties[variableName];
46+
// if the custom property is known
47+
if (prop in opts.customProperties) {
48+
let customPropertyValue = opts.customProperties[prop];
4749

50+
// follow custom properties referencing custom properties
4851
if (looseVarMatch.test(customPropertyValue)) {
49-
if (typeof customPropertyValue === 'string') {
50-
customPropertyValue = parser(customPropertyValue).parse();
51-
}
52-
5352
const rootChildAST = customPropertyValue.clone();
5453

5554
transformVariables(rootChildAST, opts);
5655

57-
customPropertyValue = opts.customProperties[variableName] = rootChildAST;
56+
customPropertyValue = rootChildAST;
5857
}
5958

60-
child.replaceWith(parser.word({
61-
raws: child.raws,
62-
value: String(customPropertyValue)
63-
}));
64-
} else if (fallbackNode) {
59+
// replace var() with the custom property value
60+
if (customPropertyValue.nodes.length === 1 && customPropertyValue.nodes[0].nodes.length) {
61+
customPropertyValue.nodes[0].nodes.forEach(customPropertyChild => {
62+
child.parent.insertBefore(child, customPropertyChild);
63+
});
64+
}
65+
66+
child.remove();
67+
} else if (fallbackNode && fallbackNode.nodes.length === 1 && fallbackNode.nodes[0].nodes.length) {
68+
// otherwise, replace var() with the fallback value
6569
transformVariables(fallbackNode, opts);
6670

67-
child.replaceWith(...fallbackNode.nodes[0].nodes);
71+
child.replaceWith(...fallbackNode.nodes[0].nodes[0]);
6872
}
6973
}
7074
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "postcss-color-mod-function",
3-
"version": "3.0.2",
3+
"version": "3.0.3",
44
"description": "Modify colors using the color-mod() function in CSS",
55
"author": "Jonathan Neal <jonathantneal@hotmail.com>",
66
"license": "CC0-1.0",

0 commit comments

Comments
 (0)