diff --git a/scripts/generateChangedIconsCommentMarkup.mjs b/scripts/generateChangedIconsCommentMarkup.mjs index 593bf037d..179c74fec 100644 --- a/scripts/generateChangedIconsCommentMarkup.mjs +++ b/scripts/generateChangedIconsCommentMarkup.mjs @@ -1,6 +1,6 @@ import fs from 'fs'; import path from 'path'; -import { shuffle, readSvgDirectory, getCurrentDirPath } from './helpers.mjs'; +import { shuffle, readSvgDirectory, getCurrentDirPath, minifySvg } from './helpers.mjs'; const currentDir = getCurrentDirPath(import.meta.url); const ICONS_DIR = path.resolve(currentDir, '../icons'); @@ -17,11 +17,10 @@ const getImageTagsByFiles = (files, getBaseUrl, width) => files .map((file) => { const svgContent = fs.readFileSync(path.join(process.cwd(), file), 'utf-8'); - const strippedAttrsSVG = svgContent - .replace(/]*>/, '') - .replaceAll(/\n| {2}|\t/g, ''); + const strippedAttrsSVG = svgContent.replace(/]*>/, '') + const minifiedSvg = minifySvg(strippedAttrsSVG) - const base64 = Buffer.from(strippedAttrsSVG).toString('base64'); + const base64 = Buffer.from(minifiedSvg).toString('base64'); const url = getBaseUrl(file); const widthAttr = width ? `width="${width}"` : ''; diff --git a/scripts/helpers.mjs b/scripts/helpers.mjs index 214d5763e..14ac3a428 100644 --- a/scripts/helpers.mjs +++ b/scripts/helpers.mjs @@ -208,3 +208,17 @@ export const shuffle = (array) => { } return array; }; + +/** + * Minifies SVG + * + * @param {string} string + * @returns string + */ +export function minifySvg(string){ + return string ? string + .replace(/\>[\r\n ]+<") + .replace(/(<.*?>)|\s+/g, (m, $1) => $1 || ' ') + .trim() + : "" +}