Skip to content

Commit e0fec9d

Browse files
authored
Merge pull request #473 from sronveaux/add-vite-plugin-mdi-woff2
Added Vite plugin to keep only woff2 format of MDI icon font
2 parents 9291605 + ec5f3d1 commit e0fec9d

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

plugins/vite-plugin-mdi-woff2.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
export default function mdiWoff2OnlyPlugin (options) {
2+
return {
3+
name: 'mdi-woff2-only',
4+
enforce: 'pre',
5+
transform (code, id) {
6+
if (!id.includes('node_modules/@mdi/font/css/materialdesignicons.css')) {
7+
return null;
8+
}
9+
10+
// Extract the WOFF2 url(...) format("woff2")
11+
const woff2Match = code.match(
12+
/url\((["'])\.\.\/fonts\/materialdesignicons-webfont\.woff2[^"']*\1\)\s+format\((["'])woff2\2\)/
13+
)
14+
if (woff2Match) {
15+
// Replace the src: ...; lines to reference WOFF2 only
16+
// But keep their number consistent to impact source map as less as possible
17+
const woff2 = woff2Match[0];
18+
19+
const newCode = code.replace(
20+
/@font-face\s*{([\s\S]*?)}/,
21+
(match, inner) => {
22+
const cleanedInner = inner
23+
.split('\n')
24+
.map((line) => {
25+
if (!line.trim().startsWith('src:')) {
26+
return line;
27+
}
28+
if (!line.includes('woff2')) {
29+
return '';
30+
} else {
31+
const indent = line.match(/^\s*/)[0];
32+
return `${indent}src: ${woff2};`;
33+
}
34+
}).join('\n');
35+
36+
return `@font-face {${cleanedInner}}`;
37+
}
38+
);
39+
40+
return {
41+
code: newCode,
42+
map: null
43+
};
44+
}
45+
return null;
46+
}
47+
};
48+
};

vite.config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import Vuetify from 'vite-plugin-vuetify';
99

1010
import eslintPlugin from '@nabla/vite-plugin-eslint';
1111

12+
import mdiWoff2OnlyPlugin from './plugins/vite-plugin-mdi-woff2';
13+
1214
// https://vite.dev/config/
1315
export default defineConfig(({ mode }) => {
1416
const __dirname = dirname(fileURLToPath(import.meta.url));
@@ -40,7 +42,8 @@ export default defineConfig(({ mode }) => {
4042
eslintPlugin({
4143
// Lint src and app directories.
4244
shouldLint: (path) => path.match(/\/(src|app)\/[^?]*\.(vue|svelte|m?[jt]sx?)$/)
43-
})
45+
}),
46+
mdiWoff2OnlyPlugin()
4447
],
4548
// Needed so vite-plugin-vuetify can process source code correctly.
4649
optimizeDeps: {

0 commit comments

Comments
 (0)