From 5d5eb93baac2d413e562f193a94304ffa6ae1c2f Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Fri, 25 Mar 2022 13:35:21 +0530 Subject: [PATCH 1/5] Implement plugin --- .gitignore | 3 +- scripts/postcss/css-url-processor.js | 81 ++++++++++++++++++++++++++++ scripts/postcss/svg-colorizer.js | 42 +++++++++++++++ 3 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 scripts/postcss/css-url-processor.js create mode 100644 scripts/postcss/svg-colorizer.js diff --git a/.gitignore b/.gitignore index 7f6220cf..089600eb 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ bundle.js target lib *.tar.gz -.eslintcache \ No newline at end of file +.eslintcache +.tmp diff --git a/scripts/postcss/css-url-processor.js b/scripts/postcss/css-url-processor.js new file mode 100644 index 00000000..854b549f --- /dev/null +++ b/scripts/postcss/css-url-processor.js @@ -0,0 +1,81 @@ +/* +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; + +function colorsFromURL(url, colorVariables) { + const params = new URL(`file://${url}`).searchParams; + const primary = params.get("primary"); + if (!primary) { + return null; + } + const secondary = params.get("secondary"); + const primaryColor = colorVariables[primary]; + const secondaryColor = colorVariables[secondary]; + if (!primaryColor) { + throw new Error(`Variable ${primary} not found in resolved color variables!`); + } + if (!secondaryColor) { + throw new Error(`Variable ${secondary} not found in resolved color variables!`); + } + return [primaryColor, secondaryColor]; +} + +function processURL(decl, replacer, colorVariables) { + const value = decl.value; + const parsed = valueParser(value); + parsed.walk(async node => { + if (node.type !== "function" || node.value !== "url") { + return; + } + const urlStringNode = node.nodes[0]; + const oldURL = urlStringNode.value; + const cssPath = decl.source?.input.file.replace(/[^/]*$/, ""); + const oldURLAbsolute = resolve(cssPath, oldURL); + const colors = colorsFromURL(oldURLAbsolute, colorVariables); + 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", + + Once(root) { + /* + Go through each declaration and if it contains an URL, replace the url with the result + of running replacer(url) + */ + root.walkDecls(decl => processURL(decl, opts.replacer, opts.colors)); + }, + }; +}; + +module.exports.postcss = true; + diff --git a/scripts/postcss/svg-colorizer.js b/scripts/postcss/svg-colorizer.js new file mode 100644 index 00000000..1a659e6a --- /dev/null +++ b/scripts/postcss/svg-colorizer.js @@ -0,0 +1,42 @@ +/* +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 fs = require("fs"); +const path = require("path"); +/** + * Builds a new svg with the colors replaced and returns its location. + * @param {string} svgLocation The location of the input svg file + * @param {string} primaryColor Primary color for the new svg + * @param {string} secondaryColor Secondary color for the new svg + */ +module.exports.buildColorizedSVG = function (svgLocation, primaryColor, secondaryColor) { + const svgCode = fs.readFileSync(svgLocation, { encoding: "utf-8"}); + let coloredSVGCode = svgCode.replaceAll("#ff00ff", primaryColor); + coloredSVGCode = coloredSVGCode.replaceAll("#00ffff", secondaryColor); + const fileName = svgLocation.match(/.+\/(.+\.svg)/)[1]; + const outputPath = path.resolve(__dirname, "../../.tmp"); + try { + fs.mkdirSync(outputPath); + } + catch (e) { + if (e.code !== "EEXIST") { + throw e; + } + } + const outputFile = `${outputPath}/${fileName}`; + fs.writeFileSync(outputFile, coloredSVGCode); + return outputFile; +} From 8c6400ab2c16c232223c21e471213863a8622376 Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Fri, 25 Mar 2022 14:11:09 +0530 Subject: [PATCH 2/5] utf-8 --> utf8 --- scripts/postcss/svg-colorizer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/postcss/svg-colorizer.js b/scripts/postcss/svg-colorizer.js index 1a659e6a..1895cfa0 100644 --- a/scripts/postcss/svg-colorizer.js +++ b/scripts/postcss/svg-colorizer.js @@ -23,7 +23,7 @@ const path = require("path"); * @param {string} secondaryColor Secondary color for the new svg */ module.exports.buildColorizedSVG = function (svgLocation, primaryColor, secondaryColor) { - const svgCode = fs.readFileSync(svgLocation, { encoding: "utf-8"}); + const svgCode = fs.readFileSync(svgLocation, { encoding: "utf8"}); let coloredSVGCode = svgCode.replaceAll("#ff00ff", primaryColor); coloredSVGCode = coloredSVGCode.replaceAll("#00ffff", secondaryColor); const fileName = svgLocation.match(/.+\/(.+\.svg)/)[1]; From 7046fcc7c74aef65256db3549cf4145c266cd077 Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Sun, 27 Mar 2022 20:00:43 +0530 Subject: [PATCH 3/5] Find list of resolved colors from result and also throw only if secondary color was provided --- scripts/postcss/css-url-processor.js | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/scripts/postcss/css-url-processor.js b/scripts/postcss/css-url-processor.js index 854b549f..d663ed0a 100644 --- a/scripts/postcss/css-url-processor.js +++ b/scripts/postcss/css-url-processor.js @@ -17,25 +17,25 @@ limitations under the License. const valueParser = require("postcss-value-parser"); const resolve = require("path").resolve; -function colorsFromURL(url, colorVariables) { +function colorsFromURL(url, colorMap) { const params = new URL(`file://${url}`).searchParams; const primary = params.get("primary"); if (!primary) { return null; } const secondary = params.get("secondary"); - const primaryColor = colorVariables[primary]; - const secondaryColor = colorVariables[secondary]; + const primaryColor = colorMap.get(primary); + const secondaryColor = colorMap.get(secondary); if (!primaryColor) { throw new Error(`Variable ${primary} not found in resolved color variables!`); } - if (!secondaryColor) { + if (secondary && !secondaryColor) { throw new Error(`Variable ${secondary} not found in resolved color variables!`); } return [primaryColor, secondaryColor]; } -function processURL(decl, replacer, colorVariables) { +function processURL(decl, replacer, colorMap) { const value = decl.value; const parsed = valueParser(value); parsed.walk(async node => { @@ -46,7 +46,7 @@ function processURL(decl, replacer, colorVariables) { const oldURL = urlStringNode.value; const cssPath = decl.source?.input.file.replace(/[^/]*$/, ""); const oldURLAbsolute = resolve(cssPath, oldURL); - const colors = colorsFromURL(oldURLAbsolute, colorVariables); + const colors = colorsFromURL(oldURLAbsolute, colorMap); if (!colors) { // If no primary color is provided via url params, then this url need not be handled. return; @@ -67,15 +67,22 @@ module.exports = (opts = {}) => { return { postcssPlugin: "postcss-url-to-variable", - Once(root) { + Once(root, {result}) { + /* + 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!"); + } /* Go through each declaration and if it contains an URL, replace the url with the result of running replacer(url) */ - root.walkDecls(decl => processURL(decl, opts.replacer, opts.colors)); + root.walkDecls(decl => processURL(decl, opts.replacer, colorMap)); + console.log("result", colorMap); }, }; }; module.exports.postcss = true; - From f2b4f2e069096a10cc487ff55eb43c4323e4d8c4 Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Sun, 27 Mar 2022 20:35:10 +0530 Subject: [PATCH 4/5] Remove console.log --- scripts/postcss/css-url-processor.js | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/postcss/css-url-processor.js b/scripts/postcss/css-url-processor.js index d663ed0a..1a0e4fdf 100644 --- a/scripts/postcss/css-url-processor.js +++ b/scripts/postcss/css-url-processor.js @@ -80,7 +80,6 @@ module.exports = (opts = {}) => { of running replacer(url) */ root.walkDecls(decl => processURL(decl, opts.replacer, colorMap)); - console.log("result", colorMap); }, }; }; From c0fb8a2c77bd0d446b796c0632210730b915ffb3 Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Wed, 6 Apr 2022 11:02:09 +0530 Subject: [PATCH 5/5] Throw error if no replacements were made --- scripts/postcss/svg-colorizer.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/scripts/postcss/svg-colorizer.js b/scripts/postcss/svg-colorizer.js index 1895cfa0..7d527ddb 100644 --- a/scripts/postcss/svg-colorizer.js +++ b/scripts/postcss/svg-colorizer.js @@ -26,6 +26,9 @@ module.exports.buildColorizedSVG = function (svgLocation, primaryColor, secondar const svgCode = fs.readFileSync(svgLocation, { encoding: "utf8"}); let coloredSVGCode = svgCode.replaceAll("#ff00ff", primaryColor); coloredSVGCode = coloredSVGCode.replaceAll("#00ffff", secondaryColor); + if (svgCode === coloredSVGCode) { + throw new Error("svg-colorizer made no color replacements! The input svg should only contain colors #ff00ff (primary, case-sensitive) and #00ffff (secondary, case-sensitive)."); + } const fileName = svgLocation.match(/.+\/(.+\.svg)/)[1]; const outputPath = path.resolve(__dirname, "../../.tmp"); try {