From d5b818418fcc252ce0d83abc2989327eed747b70 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sat, 23 Mar 2024 11:18:43 +0000 Subject: [PATCH] refactor: share storage instance between meta/assets --- src/assets.ts | 7 +------ src/cache.ts | 8 ++++---- test/setup.ts | 6 ++++-- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/assets.ts b/src/assets.ts index cb9e447..fff3ba5 100644 --- a/src/assets.ts +++ b/src/assets.ts @@ -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' @@ -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 () => { diff --git a/src/cache.ts b/src/cache.ts index 42aca07..a282bf6 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -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 (key: string, fetcher: () => Awaitable, options?: { diff --git a/test/setup.ts b/test/setup.ts index 63b3e2d..da147a6 100644 --- a/test/setup.ts +++ b/test/setup.ts @@ -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.') }