Skip to content

Commit

Permalink
fix: display more info about fonts download
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Feb 21, 2024
1 parent 0a9bf59 commit 72f1393
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
},
"dependencies": {
"@nuxt/kit": "^3.10.2",
"chalk": "^5.3.0",
"css-tree": "^2.3.1",
"defu": "^6.1.4",
"fontaine": "^0.4.1",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion src/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fsp from 'node:fs/promises'
import { addDevServerHandler, useNitro, useNuxt } from '@nuxt/kit'
import { eventHandler, createError, lazyEventHandler } from 'h3'
import { fetch } from 'ofetch'
import chalk from 'chalk'
import { defu } from 'defu'
import type { NitroConfig } from 'nitropack'
import { hasProtocol, joinURL } from 'ufo'
Expand Down Expand Up @@ -91,17 +92,25 @@ export function setupPublicAssetStrategy (options: ModuleOptions['assets'] = {})
nitro.hooks.hook('rollup:before', async () => {
await fsp.rm(cacheDir, { recursive: true, force: true })
await fsp.mkdir(cacheDir, { recursive: true })
logger.info('Downloading uncached fonts...')
let banner = false
for (const [filename, url] of renderedFontURLs) {
const key = 'data:fonts:' + filename
// Use nitro.storage to cache the font data between builds
let res = await nitro.storage.getItemRaw(key)
if (!res) {
if (!banner) {
banner = true
logger.info('Downloading fonts...')
}
logger.log(chalk.gray(' ├─ ' + url))
res = await fetch(url).then(r => r.arrayBuffer()).then(r => Buffer.from(r))
await nitro.storage.setItemRaw(key, res)
}
await fsp.writeFile(join(cacheDir, filename), res)
}
if (banner) {
logger.success('Fonts downloaded and cached.')
}
})
})

Expand Down

0 comments on commit 72f1393

Please sign in to comment.