From c5f4a75d4b7b3bee0218cfe7b6527fb66d812fb2 Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Fri, 15 Jul 2022 14:57:00 +0530 Subject: [PATCH] Split code so that it can be reused --- .../{svg-colorizer.mjs => svg-builder.mjs} | 7 ++---- scripts/postcss/svg-colorizer.js | 24 +++++++++++++++++++ vite.common-config.js | 2 +- 3 files changed, 27 insertions(+), 6 deletions(-) rename scripts/postcss/{svg-colorizer.mjs => svg-builder.mjs} (82%) create mode 100644 scripts/postcss/svg-colorizer.js diff --git a/scripts/postcss/svg-colorizer.mjs b/scripts/postcss/svg-builder.mjs similarity index 82% rename from scripts/postcss/svg-colorizer.mjs rename to scripts/postcss/svg-builder.mjs index fbe48b55..1bbc4010 100644 --- a/scripts/postcss/svg-colorizer.mjs +++ b/scripts/postcss/svg-builder.mjs @@ -17,6 +17,7 @@ limitations under the License. import {readFileSync, mkdirSync, writeFileSync} from "fs"; import {resolve} from "path"; import {h32} from "xxhashjs"; +import {getColoredSvgString} from "./svg-colorizer.js"; function createHash(content) { const hasher = new h32(0); @@ -32,11 +33,7 @@ function createHash(content) { */ export function buildColorizedSVG(svgLocation, primaryColor, secondaryColor) { const svgCode = 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 coloredSVGCode = getColoredSvgString(svgCode, primaryColor, secondaryColor); const fileName = svgLocation.match(/.+[/\\](.+\.svg)/)[1]; const outputName = `${fileName.substring(0, fileName.length - 4)}-${createHash(coloredSVGCode)}.svg`; const outputPath = resolve(__dirname, "../../.tmp"); diff --git a/scripts/postcss/svg-colorizer.js b/scripts/postcss/svg-colorizer.js new file mode 100644 index 00000000..cb291726 --- /dev/null +++ b/scripts/postcss/svg-colorizer.js @@ -0,0 +1,24 @@ +/* +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. +*/ + +export function getColoredSvgString(svgString, primaryColor, secondaryColor) { + let coloredSVGCode = svgString.replaceAll("#ff00ff", primaryColor); + coloredSVGCode = coloredSVGCode.replaceAll("#00ffff", secondaryColor); + if (svgString === 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)."); + } + return coloredSVGCode; +} diff --git a/vite.common-config.js b/vite.common-config.js index aaea47a9..bcf17115 100644 --- a/vite.common-config.js +++ b/vite.common-config.js @@ -8,7 +8,7 @@ const path = require("path"); const manifest = require("./package.json"); const version = manifest.version; const compiledVariables = new Map(); -import {buildColorizedSVG as replacer} from "./scripts/postcss/svg-colorizer.mjs"; +import {buildColorizedSVG as replacer} from "./scripts/postcss/svg-builder.mjs"; import {derive} from "./scripts/postcss/color.mjs"; const commonOptions = {