From 60ef8244e9613f46ec89f3d705cde7aab7dba0ac Mon Sep 17 00:00:00 2001 From: sronveaux Date: Tue, 20 Jan 2026 12:01:40 +0100 Subject: [PATCH] Changed Vite WOFF2 plugin to use PostCSS instead of mangling strings manually --- plugins/vite-plugin-mdi-woff2.js | 74 ++++++++++++++++++-------------- 1 file changed, 41 insertions(+), 33 deletions(-) diff --git a/plugins/vite-plugin-mdi-woff2.js b/plugins/vite-plugin-mdi-woff2.js index b386f062..836a4cb8 100644 --- a/plugins/vite-plugin-mdi-woff2.js +++ b/plugins/vite-plugin-mdi-woff2.js @@ -1,48 +1,56 @@ +import postcss from 'postcss'; +import fs from 'node:fs'; + export default function mdiWoff2OnlyPlugin (options) { return { name: 'mdi-woff2-only', enforce: 'pre', - transform (code, id) { + async transform (code, id) { if (!id.includes('node_modules/@mdi/font/css/materialdesignicons.css')) { return null; } - // Extract the WOFF2 url(...) format("woff2") - const woff2Match = code.match( - /url\((["'])\.\.\/fonts\/materialdesignicons-webfont\.woff2[^"']*\1\)\s+format\((["'])woff2\2\)/ - ) - if (woff2Match) { - // Replace the src: ...; lines to reference WOFF2 only - // But keep their number consistent to impact source map as less as possible - const woff2 = woff2Match[0]; + // Load original source map directly from node_modules + const mapPath = `${id}.map`; + let previousMap; + if (fs.existsSync(mapPath)) { + previousMap = fs.readFileSync(mapPath, 'utf-8'); + } - const newCode = code.replace( - /@font-face\s*{([\s\S]*?)}/, - (match, inner) => { - const cleanedInner = inner - .split('\n') - .map((line) => { - if (!line.trim().startsWith('src:')) { - return line; - } - if (!line.includes('woff2')) { - return ''; + const result = await postcss([ + { + postcssPlugin: 'remove-fonts-except-woff2', + AtRule: { + 'font-face': (atRule) => { + atRule.walkDecls('src', (decl) => { + // Extract the WOFF2 url(...) if present inside the src declaration + const woff2Match = decl.value.match(/url\((["']?)([^"')]+\.woff2[^"')]*?)\1\)/); + + if (woff2Match) { + // Replace the src: ...; declaration to reference WOFF2 only + decl.value = `url("${woff2Match[2]}") format("woff2")`; } else { - const indent = line.match(/^\s*/)[0]; - return `${indent}src: ${woff2};`; + // Remove declaration completely if WOFF2 is not present + decl.remove(); } - }).join('\n'); - - return `@font-face {${cleanedInner}}`; + }); + } } - ); + } + ]).process(code, { + from: id, + to: id, + map: { + inline: false, + annotation: false, + prev: previousMap + } + }); - return { - code: newCode, - map: null - }; - } - return null; + return { + code: result.css, + map: result.map.toJSON() + }; } }; -}; +}