diff --git a/plugins/css_bundler/src/builder.ts b/plugins/css_bundler/src/builder.ts index 7a9c902..399ddea 100644 --- a/plugins/css_bundler/src/builder.ts +++ b/plugins/css_bundler/src/builder.ts @@ -1,6 +1,6 @@ import { join, parse, resolve, toFileUrl } from '$std/path/mod.ts' import { bundleAsync } from 'lightningcss' -import { Logger, cssImports, uInt8ArrayConcat } from './helpers.ts' +import { cssImports, cssUrls, Logger, uInt8ArrayConcat } from './helpers.ts' export async function builder( { filename, dev, assetDir, remote }: { @@ -91,17 +91,23 @@ export async function builder( cache(url, remote, assetDir) } return { loc, url } - } - } + }, + }, }) return { code, map } } async function cache(url: string, base: string, assetDir: string) { + const filepath = join(assetDir, url.split('?')[0]) const fullUrl = new URL(url, base) + + if (cssUrls.has(fullUrl.pathname)) { + return + } + + cssUrls.set(fullUrl.pathname, filepath) const response = await fetch(fullUrl) const file = await response.arrayBuffer() - const filepath = join(assetDir, url.split('?')[0]) await Deno.writeFile(filepath, new Uint8Array(file)) -} \ No newline at end of file +} diff --git a/plugins/css_bundler/src/helpers.ts b/plugins/css_bundler/src/helpers.ts index 9f55cda..9647874 100644 --- a/plugins/css_bundler/src/helpers.ts +++ b/plugins/css_bundler/src/helpers.ts @@ -26,6 +26,7 @@ export async function hashFile(file: ArrayBuffer): Promise { } export const cssImports: Map = new Map() +export const cssUrls: Map = new Map() export function uInt8ArrayConcat(a: Uint8Array, b: Uint8Array) { const dest = new Uint8Array(a.length + b.length) diff --git a/plugins/css_bundler/src/middleware.ts b/plugins/css_bundler/src/middleware.ts index cb23944..19d2e53 100644 --- a/plugins/css_bundler/src/middleware.ts +++ b/plugins/css_bundler/src/middleware.ts @@ -43,8 +43,8 @@ export function cssHandler(sourceDir: string): MiddlewareHandler { return new Response(file, { headers: { - 'Content-Type': contentType(ext) ?? 'text/plain; charset=utf-8' - } + 'Content-Type': contentType(ext) ?? 'text/plain; charset=utf-8', + }, }) } catch { return resp