From d322f380ada0d77ab2e30ae6bb7672b7c7b7c332 Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Thu, 16 Jun 2022 21:26:16 +0530 Subject: [PATCH 1/2] Fix typo here This was causing the icons section to be omitted from the source section of the manifest. --- vite.common-config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vite.common-config.js b/vite.common-config.js index 8a82a9da..5d65f8e2 100644 --- a/vite.common-config.js +++ b/vite.common-config.js @@ -40,7 +40,7 @@ const commonOptions = { postcss: { plugins: [ compileVariables({derive, compiledVariables}), - urlVariables({compileVariables}), + urlVariables({compiledVariables}), urlProcessor({replacer}), // cssvariables({ // preserve: (declaration) => { From cfd347335ba80cccf5ff99675ff42d6841a9ff06 Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Thu, 16 Jun 2022 21:29:33 +0530 Subject: [PATCH 2/2] Move scope of variables down This was causing icons to be repeated in the css-file --- scripts/postcss/css-url-to-variables.js | 27 +++++++++++++++---------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/scripts/postcss/css-url-to-variables.js b/scripts/postcss/css-url-to-variables.js index 1d4666f4..82ddae82 100644 --- a/scripts/postcss/css-url-to-variables.js +++ b/scripts/postcss/css-url-to-variables.js @@ -20,11 +20,9 @@ const valueParser = require("postcss-value-parser"); * 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. */ -let counter; -let urlVariables; const idToPrepend = "icon-url"; -function findAndReplaceUrl(decl) { +function findAndReplaceUrl(decl, urlVariables, counter) { const value = decl.value; const parsed = valueParser(value); parsed.walk(node => { @@ -35,7 +33,8 @@ function findAndReplaceUrl(decl) { if (!url.match(/\.svg\?primary=.+/)) { return; } - const variableName = `${idToPrepend}-${counter++}`; + const count = counter.next().value; + const variableName = `${idToPrepend}-${count}`; urlVariables.set(variableName, url); node.value = "var"; node.nodes = [{ type: "word", value: `--${variableName}` }]; @@ -43,7 +42,7 @@ function findAndReplaceUrl(decl) { decl.assign({prop: decl.prop, value: parsed.toString()}) } -function addResolvedVariablesToRootSelector(root, { Rule, Declaration }) { +function addResolvedVariablesToRootSelector(root, { Rule, Declaration }, urlVariables) { const newRule = new Rule({ selector: ":root", source: root.source }); // Add derived css variables to :root urlVariables.forEach((value, key) => { @@ -53,29 +52,35 @@ function addResolvedVariablesToRootSelector(root, { Rule, Declaration }) { root.append(newRule); } -function populateMapWithIcons(map, cssFileLocation) { +function populateMapWithIcons(map, cssFileLocation, urlVariables) { const location = cssFileLocation.match(/(.+)\/.+\.css/)?.[1]; const sharedObject = map.get(location); sharedObject["icon"] = Object.fromEntries(urlVariables); } +function *createCounter() { + for (let i = 0; ; ++i) { + yield i; + } +} + /* * * @type {import('postcss').PluginCreator} */ module.exports = (opts = {}) => { - urlVariables = new Map(); - counter = 0; return { postcssPlugin: "postcss-url-to-variable", Once(root, { Rule, Declaration }) { - root.walkDecls(decl => findAndReplaceUrl(decl)); + const urlVariables = new Map(); + const counter = createCounter(); + root.walkDecls(decl => findAndReplaceUrl(decl, urlVariables, counter)); if (urlVariables.size) { - addResolvedVariablesToRootSelector(root, { Rule, Declaration }); + addResolvedVariablesToRootSelector(root, { Rule, Declaration }, urlVariables); } if (opts.compiledVariables){ const cssFileLocation = root.source.input.from; - populateMapWithIcons(opts.compiledVariables, cssFileLocation); + populateMapWithIcons(opts.compiledVariables, cssFileLocation, urlVariables); } }, };