Skip to content

Commit

Permalink
refactor: share storage instance between meta/assets
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Mar 23, 2024
1 parent c1dc27e commit d5b8184
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 12 deletions.
7 changes: 1 addition & 6 deletions src/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import { hasProtocol, joinURL } from 'ufo'
import { extname, join } from 'pathe'
import { filename } from 'pathe/utils'
import { hash } from 'ohash'
import { createStorage } from 'unstorage'
import fsDriver from 'unstorage/drivers/fs-lite'

import { storage } from './cache'
import { logger } from './logger'
import { formatToExtension, parseFont } from './css/render'
import type { FontFaceData, ModuleOptions, NormalizedFontFaceData } from './types'
Expand Down Expand Up @@ -92,10 +91,6 @@ export function setupPublicAssetStrategy (options: ModuleOptions['assets'] = {})
} satisfies NitroConfig)

// TODO: refactor to use nitro storage when it can be cached between builds
const storage = createStorage(fsDriver({
base: 'node_modules/.cache/nuxt/fonts'
}))

nuxt.hook('nitro:init', async (nitro) => {
if (nuxt.options.dev) { return }
nitro.hooks.hook('rollup:before', async () => {
Expand Down
8 changes: 4 additions & 4 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import fsDriver from 'unstorage/drivers/fs'

import type { Awaitable } from './types'

export const cacheBase = 'node_modules/.cache/nuxt/fonts/meta'

// TODO: refactor to use nitro storage when possible
const storage = createStorage({
driver: fsDriver({
base: 'node_modules/.cache/nuxt/fonts/meta',
})
export const storage = createStorage({
driver: fsDriver({ base: cacheBase })
})

export async function cachedData<T = unknown> (key: string, fetcher: () => Awaitable<T>, options?: {
Expand Down
6 changes: 4 additions & 2 deletions test/setup.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { promises as fsp } from 'node:fs'

import { cacheBase } from '../src/cache'

export async function setup () {
await fsp.rm('./node_modules/.cache/nuxt/fonts', { recursive: true, force: true })
await fsp.rm('./playground/node_modules/.cache/nuxt/fonts', { recursive: true, force: true })
await fsp.rm('./' + cacheBase, { recursive: true, force: true })
await fsp.rm('./playground' + cacheBase , { recursive: true, force: true })
console.log('✅ Cleared font cache.')
}

0 comments on commit d5b8184

Please sign in to comment.