From 9286ec758d52d7342eb06f703c447682e374c5c8 Mon Sep 17 00:00:00 2001 From: Julien Oculi Date: Sun, 14 Jan 2024 21:10:16 +0100 Subject: [PATCH] refactor: move cache and src css --- build.ts | 20 +++++++++++--------- deno.json | 2 +- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/build.ts b/build.ts index 74a9c2b..da4564f 100644 --- a/build.ts +++ b/build.ts @@ -3,7 +3,7 @@ import { ensureDir } from '$std/fs/mod.ts' import { parse, resolve } from '$std/path/mod.ts' console.log('building styles starts') -await ensureDir('_fresh') +await ensureDir('_fresh/static') try { //prevent Deno from exiting before bundle @@ -12,7 +12,7 @@ try { const cssImports = new Map() const { code, map } = await bundleAsync({ - filename: './static/stylesheets/main.css', + filename: './src/stylesheets/main.css', minify: true, sourceMap: true, resolver: { @@ -29,22 +29,24 @@ try { //update cache for new remote const response = await fetch(specifier) - const file = await response.text() + const file = await response.arrayBuffer() + const hash = new Uint8Array(await crypto.subtle.digest('SHA-256', file)) + const filename = [...hash].map(value => value.toString(16).padStart(2, '0')).join('') - const filename = `_fresh/${crypto.randomUUID()}` - await Deno.writeTextFile(filename, file) - cssImports.set(specifier, filename) - return filename + const filepath = `_fresh/${filename}` + await Deno.writeFile(filepath, new Uint8Array(file)) + cssImports.set(specifier, filepath) + return filepath }, }, }) await Deno.writeTextFile( - './_fresh/styles.css', + './static/styles.css', new TextDecoder().decode(code), ) await Deno.writeTextFile( - './_fresh/styles.map.css', + './static/styles.map.css', new TextDecoder().decode(map!), ) } catch (error) { diff --git a/deno.json b/deno.json index 53c1301..685584a 100644 --- a/deno.json +++ b/deno.json @@ -8,7 +8,7 @@ "build": "deno run -A dev.ts build", "preview": "deno run -A main.ts", "update": "deno run -A -r https://fresh.deno.dev/update .", - "assets:watch": "deno run -A --watch=static/stylesheets/ build.ts", + "assets:watch": "deno run -A --watch=src/ build.ts", "assets": "deno run -A build.ts" }, "fmt": {