forked from mystiq/hydrogen-web
Also derive variables in URLs
This commit is contained in:
parent
454345c9b2
commit
48d0242c80
2 changed files with 44 additions and 4 deletions
|
@ -43,17 +43,32 @@ function parseDeclarationValue(value) {
|
||||||
const parsed = valueParser(value);
|
const parsed = valueParser(value);
|
||||||
const variables = [];
|
const variables = [];
|
||||||
parsed.walk(node => {
|
parsed.walk(node => {
|
||||||
if (node.type !== "function" && node.value !== "var") {
|
if (node.type !== "function") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
switch (node.value) {
|
||||||
|
case "var": {
|
||||||
const variable = node.nodes[0];
|
const variable = node.nodes[0];
|
||||||
variables.push(variable.value);
|
variables.push(variable.value);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "url": {
|
||||||
|
const url = node.nodes[0].value;
|
||||||
|
// resolve url with some absolute url so that we get the query params without using regex
|
||||||
|
const params = new URL(url, "file://foo/bar/").searchParams;
|
||||||
|
const primary = params.get("primary");
|
||||||
|
const secondary = params.get("secondary");
|
||||||
|
if (primary) { variables.push(primary); }
|
||||||
|
if (secondary) { variables.push(secondary); }
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
return variables;
|
return variables;
|
||||||
}
|
}
|
||||||
|
|
||||||
function resolveDerivedVariable(decl, derive) {
|
function resolveDerivedVariable(decl, derive) {
|
||||||
const RE_VARIABLE_VALUE = /--((.+)--(.+)-(.+))/;
|
const RE_VARIABLE_VALUE = /(?:--)?((.+)--(.+)-(.+))/;
|
||||||
const variableCollection = parseDeclarationValue(decl.value);
|
const variableCollection = parseDeclarationValue(decl.value);
|
||||||
for (const variable of variableCollection) {
|
for (const variable of variableCollection) {
|
||||||
const matches = variable.match(RE_VARIABLE_VALUE);
|
const matches = variable.match(RE_VARIABLE_VALUE);
|
||||||
|
|
|
@ -134,6 +134,31 @@ module.exports.tests = function tests() {
|
||||||
const actualArray = compiledVariables.get("/foo/bar")["derived-variables"];
|
const actualArray = compiledVariables.get("/foo/bar")["derived-variables"];
|
||||||
const expectedArray = ["icon-color--darker-20", "my-alias=icon-color--darker-20", "my-alias--lighter-15"];
|
const expectedArray = ["icon-color--darker-20", "my-alias=icon-color--darker-20", "my-alias--lighter-15"];
|
||||||
assert.deepStrictEqual(actualArray.sort(), expectedArray.sort());
|
assert.deepStrictEqual(actualArray.sort(), expectedArray.sort());
|
||||||
|
},
|
||||||
|
|
||||||
|
"derived variable are supported in urls": async (assert) => {
|
||||||
|
const inputCSS = `
|
||||||
|
:root {
|
||||||
|
--foo-color: #ff0;
|
||||||
|
}
|
||||||
|
div {
|
||||||
|
background-color: var(--foo-color--lighter-50);
|
||||||
|
background: url("./foo/bar/icon.svg?primary=foo-color--darker-5");
|
||||||
|
}
|
||||||
|
a {
|
||||||
|
background: url("foo/bar/icon.svg");
|
||||||
|
}`;
|
||||||
|
const transformedColorLighter = offColor("#ff0").lighten(0.5);
|
||||||
|
const transformedColorDarker = offColor("#ff0").darken(0.05);
|
||||||
|
const outputCSS =
|
||||||
|
inputCSS +
|
||||||
|
`
|
||||||
|
:root {
|
||||||
|
--foo-color--lighter-50: ${transformedColorLighter.hex()};
|
||||||
|
--foo-color--darker-5: ${transformedColorDarker.hex()};
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
await run( inputCSS, outputCSS, {}, assert);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue