Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Font Inlining #25

Merged
merged 3 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ export function viteSvgToWebfont<T extends GeneratedFontTypes = GeneratedFontTyp
const virtualModuleId = getVirtualModuleId(moduleId);
const resolvedVirtualModuleId = getResolvedVirtualModuleId(virtualModuleId);

const inline = options.inline
? <U extends string | undefined>(css: U) =>
css?.replace(/url\(".*?\.([^?]+)\?[^"]+"\)/g, (_, type: T) => {
const font = generatedFonts?.[type];
const fontData = typeof font === 'string' ? btoa(font) : font?.toString('base64');
return `url("data:${MIME_TYPES[type]};charset=utf-8;base64,${fontData}")`;
}) as U
: <U>(css: U) => css;
Azarattum marked this conversation as resolved.
Show resolved Hide resolved

const generate = async (updateFiles?: boolean) => {
if (updateFiles) {
processedOptions.files = parseFiles(options);
Expand All @@ -50,7 +59,7 @@ export function viteSvgToWebfont<T extends GeneratedFontTypes = GeneratedFontTyp
if (!isBuild && hasFilesToSave) {
const promises: Promise<void>[] = [];
if (processedOptions.css) {
promises.push(ensureDirExistsAndWriteFile(generatedFonts.generateCss(), processedOptions.cssDest));
promises.push(ensureDirExistsAndWriteFile(inline(generatedFonts.generateCss()), processedOptions.cssDest));
}
if (processedOptions.html) {
promises.push(ensureDirExistsAndWriteFile(generatedFonts.generateHtml(), processedOptions.htmlDest));
Expand Down Expand Up @@ -85,7 +94,7 @@ export function viteSvgToWebfont<T extends GeneratedFontTypes = GeneratedFontTyp
if (id !== resolvedVirtualModuleId) {
return undefined;
}
return generatedFonts?.generateCss?.(fileRefs) || '';
return inline(generatedFonts?.generateCss?.(fileRefs)) || '';
},
load(id) {
if (id !== resolvedVirtualModuleId) {
Expand All @@ -98,7 +107,7 @@ export function viteSvgToWebfont<T extends GeneratedFontTypes = GeneratedFontTyp
setupWatcher(options.context, ac.signal, () => generate(true));
}
await generate();
if (isBuild) {
if (isBuild && !options.inline) {
const emitted = processedOptions.types.map<[T, string]>(type => [
type,
`/${this.getFileName(this.emitFile({ type: 'asset', fileName: `assets/${processedOptions.fontName}-${guid()}.${type}`, source: generatedFonts?.[type] }))}`,
Expand All @@ -111,6 +120,9 @@ export function viteSvgToWebfont<T extends GeneratedFontTypes = GeneratedFontTyp
}
},
configureServer({ middlewares, reloadModule, moduleGraph }) {
if (options.inline) {
return;
}
for (const fontType of processedOptions.types) {
const fileName = `${processedOptions.fontName}.${fontType}`;
middlewares.use(`/${fileName}`, (_req, res) => {
Expand Down
5 changes: 5 additions & 0 deletions src/optionParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ export interface IconPluginOptions<T extends GeneratedFontTypes = GeneratedFontT
* @default 'vite-svg-2-webfont.css'
*/
moduleId?: string;
/**
* Inline font assets in CSS using base64 encoding.
* @default false
*/
inline?: boolean;
Azarattum marked this conversation as resolved.
Show resolved Hide resolved
}

export function parseIconTypesOption<T extends GeneratedFontTypes = GeneratedFontTypes>({ types }: Pick<IconPluginOptions<T>, 'types'>): T[] {
Expand Down