-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite-plugin-font-encoder.ts
More file actions
executable file
·275 lines (242 loc) · 9.13 KB
/
Copy pathvite-plugin-font-encoder.ts
File metadata and controls
executable file
·275 lines (242 loc) · 9.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/**
* Vite Plugin: Font Encoder
*
* Encodes fonts as base64 (compressed) and embeds them in JS code
*
* Usage:
* - Compresses fonts (if not already compressed)
* - Encodes compressed fonts to base64
* - Generates JS module with font data
*/
import { readFile, writeFile, mkdir } from 'node:fs/promises';
import { resolve, dirname, basename, extname } from 'node:path';
import { createReadStream, createWriteStream } from 'node:fs';
import { pipeline } from 'node:stream/promises';
import { createGzip } from 'node:zlib';
import type { Plugin } from 'vite';
import { fileURLToPath } from 'node:url';
//
const __dirname = import.meta?.dirname ?? fileURLToPath(new URL('.', import.meta.url));
//
interface FontEncoderOptions {
/** Font directory to scan */
fontDir: string;
/** Output file path for generated font registry */
outputFile?: string;
/** Whether to compress fonts (if not already compressed) */
compress?: boolean;
/** Font metadata configuration */
fontConfig?: Record<string, {
family: string;
style?: string;
weight?: string | number;
}>;
}
/**
* Read file and encode to base64
*/
async function encodeToBase64(filePath: string): Promise<string> {
const data = await readFile(filePath);
return data.toString('base64');
}
/**
* Compress file using gzip
*/
async function compressFile(inputPath: string, outputPath: string): Promise<void> {
await mkdir(dirname(outputPath), { recursive: true });
const readStream = createReadStream(inputPath);
const writeStream = createWriteStream(outputPath);
const gzipStream = createGzip({ level: 9 });
await pipeline(readStream, gzipStream, writeStream);
}
/**
* Generate font metadata from filename
*/
function parseFontMetadata(filename: string): {
family: string;
style: string;
weight: string | number;
} {
const baseName = basename(filename, extname(filename));
// Parse Inter font filenames
// Examples: InterVariable, InterVariable-Italic, Inter-Regular, InterDisplay-Bold
let family = 'Inter';
let style = 'normal';
let weight: string | number = 'normal';
if (baseName.includes('InterVariable')) {
family = 'InterVariable';
weight = '100 900'; // Variable font weight range
if (baseName.includes('Italic')) {
style = 'italic';
}
} else if (baseName.includes('InterDisplay')) {
family = 'InterDisplay';
// Extract weight from filename
const weightMatch = baseName.match(/(Thin|ExtraLight|Light|Regular|Medium|SemiBold|Bold|ExtraBold|Black)/);
if (weightMatch) {
const weightMap: Record<string, number> = {
'Thin': 100,
'ExtraLight': 200,
'Light': 300,
'Regular': 400,
'Medium': 500,
'SemiBold': 600,
'Bold': 700,
'ExtraBold': 800,
'Black': 900
};
weight = weightMap[weightMatch[1]] || 400;
}
if (baseName.includes('Italic')) {
style = 'italic';
}
} else if (baseName.startsWith('Inter-')) {
family = 'Inter';
const weightMatch = baseName.match(/(Thin|ExtraLight|Light|Regular|Medium|SemiBold|Bold|ExtraBold|Black)/);
if (weightMatch) {
const weightMap: Record<string, number> = {
'Thin': 100,
'ExtraLight': 200,
'Light': 300,
'Regular': 400,
'Medium': 500,
'SemiBold': 600,
'Bold': 700,
'ExtraBold': 800,
'Black': 900
};
weight = weightMap[weightMatch[1]] || 400;
}
if (baseName.includes('Italic')) {
style = 'italic';
}
}
return { family, style, weight };
}
/**
* Vite plugin to encode fonts
*/
export function fontEncoder(options: FontEncoderOptions): Plugin {
const {
fontDir,
outputFile = resolve(fontDir, './src/ts/font-registry.ts'),
compress = false,
fontConfig = {}
} = options;
let generatedFonts: Array<{
key: string;
metadata: {
base64: string;
family: string;
style: string;
weight: string | number;
compressed: boolean;
};
}> = [];
return {
name: 'vite-plugin-font-encoder',
enforce: 'pre',
async buildStart() {
// This will be called during build
// We'll generate the font registry here
console.log('[font-encoder] Generating font registry...');
// Scan font directories
const fontFiles: string[] = [];
// Helper to scan directory
const scanDir = async (dir: string, basePath: string = '') => {
const { readdir } = await import('node:fs/promises');
const entries = await readdir(dir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = resolve(dir, entry.name);
if (entry.isDirectory()) {
await scanDir(fullPath, `${basePath}/${entry.name}`);
} else if (entry.isFile() && (entry.name.endsWith('.woff2') || entry.name.endsWith('.woff'))) {
fontFiles.push(fullPath);
}
}
};
await scanDir(fontDir);
// Process each font file
for (const fontPath of fontFiles) {
const relativePath = fontPath.replace(fontDir + '/', '');
const metadata = parseFontMetadata(fontPath);
// Check if custom config exists
const customConfig = fontConfig[relativePath];
if (customConfig) {
metadata.family = customConfig.family || metadata.family;
metadata.style = customConfig.style || metadata.style;
metadata.weight = customConfig.weight || metadata.weight;
}
// Encode to base64
let base64: string;
let isCompressed = false;
// woff2 files are already compressed, just encode to base64
// For other formats, we can optionally compress them
if (fontPath.endsWith('.woff2')) {
base64 = await encodeToBase64(fontPath);
// woff2 is already compressed, but we'll mark it as needing decompression
// using Compression Streams API (though it's actually already decompressed)
// Actually, woff2 is already compressed, so we don't need to decompress it
isCompressed = false;
} else if (compress && !fontPath.endsWith('.woff2')) {
// Compress non-woff2 files and encode
const tempPath = fontPath + '.gz';
await compressFile(fontPath, tempPath);
base64 = await encodeToBase64(tempPath);
isCompressed = true;
// Clean up temp file
await import('node:fs/promises').then(fs => fs.unlink(tempPath));
} else {
// Just encode without compression
base64 = await encodeToBase64(fontPath);
isCompressed = false;
}
// Create cache key
const key = relativePath.replace(/[\/\\]/g, '_').replace(/\.(woff2?|gz)$/i, '');
generatedFonts.push({
key,
metadata: {
base64,
family: metadata.family,
style: metadata.style,
weight: metadata.weight,
compressed: isCompressed
}
});
}
// Generate font registry TypeScript file
const registryContent = `/**
* Font Registry
*
* Auto-generated by vite-plugin-font-encoder
* DO NOT EDIT MANUALLY
*/
export const fontRegistry: Record<string, any> = {
${generatedFonts.map(({ key, metadata }) => ` '${key}': {
base64: '${metadata.base64}',
family: '${metadata.family}',
style: '${metadata.style}',
weight: ${typeof metadata.weight === 'string' ? `'${metadata.weight}'` : metadata.weight},
compressed: ${metadata.compressed}
}`).join(',\n')}
};
`;
// Write registry file
await mkdir(dirname(outputFile), { recursive: true });
await writeFile(outputFile, registryContent, 'utf-8');
console.log(`[font-encoder] Generated font registry with ${generatedFonts.length} fonts`);
},
generateBundle() {
// Ensure font registry is included in bundle
// This is handled by the import system
}
};
}
//
const encoder: any = fontEncoder({
fontDir: resolve(__dirname, "./assets/fonts"),
outputFile: resolve(__dirname, "./src/ts/font-registry.ts"),
compress: true // woff2 files are already compressed
});
//
encoder.buildStart?.();