hydrogen-web/scripts/postcss/css-url-to-variables.js

86 lines
2.8 KiB
JavaScript
Raw Permalink Normal View History

2022-03-24 16:36:01 +05:30
/*
Copyright 2021 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2022-03-14 12:11:46 +05:30
const valueParser = require("postcss-value-parser");
2022-03-25 11:46:48 +05:30
2022-03-25 11:40:21 +05:30
/**
2022-04-03 16:46:26 +05:30
* This plugin extracts content inside url() into css variables and adds the variables to the root section.
* This plugin is used in conjunction with css-url-processor plugin to colorize svg icons.
2022-03-25 11:40:21 +05:30
*/
2022-03-24 16:36:01 +05:30
let counter;
2022-03-25 11:46:48 +05:30
let urlVariables;
2022-03-24 16:36:01 +05:30
const idToPrepend = "icon-url";
2022-03-14 12:11:46 +05:30
2022-03-25 16:08:15 +05:30
function findAndReplaceUrl(decl) {
2022-03-14 12:11:46 +05:30
const value = decl.value;
const parsed = valueParser(value);
parsed.walk(node => {
if (node.type !== "function" || node.value !== "url") {
return;
}
const url = node.nodes[0].value;
if (!url.match(/\.svg\?primary=.+/)) {
return;
}
2022-03-24 16:36:01 +05:30
const variableName = `${idToPrepend}-${counter++}`;
urlVariables.set(variableName, url);
2022-03-25 16:06:40 +05:30
node.value = "var";
node.nodes = [{ type: "word", value: `--${variableName}` }];
2022-03-14 12:11:46 +05:30
});
decl.assign({prop: decl.prop, value: parsed.toString()})
}
function addResolvedVariablesToRootSelector(root, { Rule, Declaration }) {
const newRule = new Rule({ selector: ":root", source: root.source });
// Add derived css variables to :root
2022-03-24 16:36:01 +05:30
urlVariables.forEach((value, key) => {
2022-03-25 16:06:40 +05:30
const declaration = new Declaration({ prop: `--${key}`, value: `url("${value}")`});
2022-03-14 12:11:46 +05:30
newRule.append(declaration);
});
root.append(newRule);
}
2022-04-04 12:02:59 +05:30
function populateMapWithIcons(map, cssFileLocation) {
const location = cssFileLocation.match(/(.+)\/.+\.css/)?.[1];
2022-04-04 12:10:54 +05:30
const sharedObject = map.get(location);
sharedObject["icon"] = Object.fromEntries(urlVariables);
}
2022-03-14 12:11:46 +05:30
/* *
* @type {import('postcss').PluginCreator}
*/
module.exports = (opts = {}) => {
2022-03-25 11:46:48 +05:30
urlVariables = new Map();
2022-03-24 16:36:01 +05:30
counter = 0;
2022-03-14 12:11:46 +05:30
return {
postcssPlugin: "postcss-url-to-variable",
Once(root, { Rule, Declaration }) {
2022-03-25 16:08:15 +05:30
root.walkDecls(decl => findAndReplaceUrl(decl));
if (urlVariables.size) {
addResolvedVariablesToRootSelector(root, { Rule, Declaration });
}
if (opts.compiledVariables){
const cssFileLocation = root.source.input.from;
2022-04-04 12:02:59 +05:30
populateMapWithIcons(opts.compiledVariables, cssFileLocation);
}
2022-03-14 12:11:46 +05:30
},
};
};
module.exports.postcss = true;