Skip to content

Commit

Permalink
fix: don't cache embedded css in dev (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
garrappachc authored Feb 6, 2025
1 parent e70e505 commit 6528823
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/html/embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ import lightenDarken from 'postcss-lighten-darken'
import autoprefixer from 'autoprefixer'
import { environment } from '../environment'
import mime from 'mime'
import { memoize } from 'es-toolkit'

const srcDir = resolve(import.meta.dirname, '..')
const cache = new Map<string, string>()

export async function embed(fileName: string): Promise<string> {
if (cache.has(fileName)) {
return cache.get(fileName)!
}
// For production, we memoize the result of the embed function to avoid rebuilding the same file multiple times
export const embed = environment.NODE_ENV === 'production' ? memoize(doEmbed) : doEmbed

async function doEmbed(fileName: string): Promise<string> {
logger.debug(`building ${fileName}...`)
const css = await readFile(fileName)
const { name, ext, dir } = parse(fileName)
Expand All @@ -38,6 +37,5 @@ export async function embed(fileName: string): Promise<string> {
})
).css
logger.debug({ type: mime.getType(fileName), length: style.length }, `${fileName} built`)
cache.set(fileName, style)
return style
}

0 comments on commit 6528823

Please sign in to comment.