Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 6 additions & 1 deletion packages/fontless/src/assets.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { FontFaceData } from 'unifont'
import type { RawFontFaceData } from './types'
import { fileURLToPath } from 'node:url'
import { hash } from 'ohash'
import { extname } from 'pathe'
import { filename } from 'pathe/utils'
Expand All @@ -14,6 +15,7 @@ export interface NormalizeFontDataContext {
dev: boolean
renderedFontURLs: Map<string, string>
assetsBaseURL: string
devFilesystemURL?: string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need to pass this? can't we just directly convert file:/// url into a path?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my testing, I couldn't get vite to convert url(file:///...) into url(/@fs/...) in css files. Plus I had to account for a custom base like /_nuxt/@fs/....

callback?: (filename: string, url: string) => void
}

Expand All @@ -39,7 +41,10 @@ export function normalizeFontData(context: NormalizeFontDataContext, faces: RawF
source.originalURL = source.url

source.url = context.dev
? joinRelativeURL(context.assetsBaseURL, file)
? source.url.startsWith('file://') && context.devFilesystemURL
// use a direct filesystem URL in dev mode for local files when available
? joinRelativeURL(context.devFilesystemURL, fileURLToPath(source.url))
: joinRelativeURL(context.assetsBaseURL, file)
: joinURL(context.assetsBaseURL, file)

context.callback?.(file, source.url)
Expand Down
8 changes: 8 additions & 0 deletions packages/fontless/src/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { FontFamilyInjectionPluginOptions } from './utils'

import { Buffer } from 'node:buffer'
import fsp from 'node:fs/promises'
import { fileURLToPath } from 'node:url'
import { defu } from 'defu'
import { resolve } from 'pathe'
import { joinRelativeURL, joinURL } from 'ufo'
Expand All @@ -30,6 +31,7 @@ export function fontless(_options?: FontlessOptions): Plugin {
dev: config.mode === 'development',
renderedFontURLs: new Map<string, string>(),
assetsBaseURL: options.assets?.prefix || '/fonts',
devFilesystemURL: joinRelativeURL(config.base, '@fs'),
}

publicDir = resolve(config.root, config.build.outDir, `.${joinURL(config.base, assetContext.assetsBaseURL)}`)
Expand Down Expand Up @@ -81,6 +83,12 @@ export function fontless(_options?: FontlessOptions): Plugin {
},
async writeBundle() {
for (const [filename, url] of assetContext.renderedFontURLs) {
if (url.startsWith('file://')) {
// copy the file to the public directory instead of fetching it
await fsp.mkdir(publicDir, { recursive: true })
await fsp.copyFile(fileURLToPath(url), joinRelativeURL(publicDir, filename))
continue
}
const key = `data:fonts:${filename}`
// Use storage to cache the font data between builds
let res = await storage.getItemRaw(key)
Expand Down