2022-03-25 13:35:21 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const valueParser = require("postcss-value-parser");
|
|
|
|
const resolve = require("path").resolve;
|
2022-04-10 14:59:42 +05:30
|
|
|
let cssPath;
|
2022-03-25 13:35:21 +05:30
|
|
|
|
2022-03-27 20:00:43 +05:30
|
|
|
function colorsFromURL(url, colorMap) {
|
2022-03-25 13:35:21 +05:30
|
|
|
const params = new URL(`file://${url}`).searchParams;
|
|
|
|
const primary = params.get("primary");
|
|
|
|
if (!primary) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const secondary = params.get("secondary");
|
2022-03-27 20:00:43 +05:30
|
|
|
const primaryColor = colorMap.get(primary);
|
|
|
|
const secondaryColor = colorMap.get(secondary);
|
2022-03-25 13:35:21 +05:30
|
|
|
if (!primaryColor) {
|
|
|
|
throw new Error(`Variable ${primary} not found in resolved color variables!`);
|
|
|
|
}
|
2022-03-27 20:00:43 +05:30
|
|
|
if (secondary && !secondaryColor) {
|
2022-03-25 13:35:21 +05:30
|
|
|
throw new Error(`Variable ${secondary} not found in resolved color variables!`);
|
|
|
|
}
|
|
|
|
return [primaryColor, secondaryColor];
|
|
|
|
}
|
|
|
|
|
2022-03-27 20:00:43 +05:30
|
|
|
function processURL(decl, replacer, colorMap) {
|
2022-03-25 13:35:21 +05:30
|
|
|
const value = decl.value;
|
|
|
|
const parsed = valueParser(value);
|
2022-04-20 03:49:13 +05:30
|
|
|
parsed.walk(node => {
|
2022-03-25 13:35:21 +05:30
|
|
|
if (node.type !== "function" || node.value !== "url") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const urlStringNode = node.nodes[0];
|
|
|
|
const oldURL = urlStringNode.value;
|
|
|
|
const oldURLAbsolute = resolve(cssPath, oldURL);
|
2022-03-27 20:00:43 +05:30
|
|
|
const colors = colorsFromURL(oldURLAbsolute, colorMap);
|
2022-03-25 13:35:21 +05:30
|
|
|
if (!colors) {
|
|
|
|
// If no primary color is provided via url params, then this url need not be handled.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const newURL = replacer(oldURLAbsolute.replace(/\?.+/, ""), ...colors);
|
|
|
|
if (!newURL) {
|
|
|
|
throw new Error("Replacer failed to produce a replacement URL!");
|
|
|
|
}
|
|
|
|
urlStringNode.value = newURL;
|
|
|
|
});
|
|
|
|
decl.assign({prop: decl.prop, value: parsed.toString()})
|
|
|
|
}
|
|
|
|
|
|
|
|
/* *
|
|
|
|
* @type {import('postcss').PluginCreator}
|
|
|
|
*/
|
|
|
|
module.exports = (opts = {}) => {
|
|
|
|
return {
|
|
|
|
postcssPlugin: "postcss-url-to-variable",
|
|
|
|
|
2022-03-27 20:00:43 +05:30
|
|
|
Once(root, {result}) {
|
2022-04-10 14:59:08 +05:30
|
|
|
const cssFileLocation = root.source.input.from;
|
|
|
|
if (cssFileLocation.includes("type=runtime")) {
|
|
|
|
// If this is a runtime theme, don't process urls.
|
|
|
|
return;
|
|
|
|
}
|
2022-03-27 20:00:43 +05:30
|
|
|
/*
|
|
|
|
postcss-compile-variables should have sent the list of resolved colours down via results
|
|
|
|
*/
|
|
|
|
const {colorMap} = result.messages.find(m => m.type === "resolved-variable-map");
|
|
|
|
if (!colorMap) {
|
|
|
|
throw new Error("Postcss results do not contain resolved colors!");
|
|
|
|
}
|
2022-03-25 13:35:21 +05:30
|
|
|
/*
|
|
|
|
Go through each declaration and if it contains an URL, replace the url with the result
|
|
|
|
of running replacer(url)
|
|
|
|
*/
|
2022-04-10 14:59:42 +05:30
|
|
|
cssPath = root.source?.input.file.replace(/[^/]*$/, "");
|
2022-03-27 20:00:43 +05:30
|
|
|
root.walkDecls(decl => processURL(decl, opts.replacer, colorMap));
|
2022-03-25 13:35:21 +05:30
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports.postcss = true;
|