File tree Expand file tree Collapse file tree 2 files changed +52
-1
lines changed
Expand file tree Collapse file tree 2 files changed +52
-1
lines changed Original file line number Diff line number Diff line change 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+ / u r l \( ( [ " ' ] ) \. \. \/ f o n t s \/ m a t e r i a l d e s i g n i c o n s - w e b f o n t \. w o f f 2 [ ^ " ' ] * \1\) \s + f o r m a t \( ( [ " ' ] ) w o f f 2 \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+ / @ f o n t - f a c e \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+ } ;
Original file line number Diff line number Diff line change @@ -9,6 +9,8 @@ import Vuetify from 'vite-plugin-vuetify';
99
1010import eslintPlugin from '@nabla/vite-plugin-eslint' ;
1111
12+ import mdiWoff2OnlyPlugin from './plugins/vite-plugin-mdi-woff2' ;
13+
1214// https://vite.dev/config/
1315export 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 ( / \/ ( s r c | a p p ) \/ [ ^ ? ] * \. ( v u e | s v e l t e | m ? [ j t ] s x ? ) $ / )
43- } )
45+ } ) ,
46+ mdiWoff2OnlyPlugin ( )
4447 ] ,
4548 // Needed so vite-plugin-vuetify can process source code correctly.
4649 optimizeDeps : {
You can’t perform that action at this time.
0 commit comments