From be6b7641d2b4bf07730b8e3ac4a1943cee6af099 Mon Sep 17 00:00:00 2001 From: Julien Oculi Date: Sun, 14 Jan 2024 20:50:44 +0100 Subject: [PATCH] chore: add css build script --- .gitignore | 2 ++ build.ts | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ deno.json | 7 +++++-- 3 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 build.ts diff --git a/.gitignore b/.gitignore index 5b4bdef..39ca461 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,5 @@ _fresh/ # npm dependencies node_modules/ + +.cache/ \ No newline at end of file diff --git a/build.ts b/build.ts new file mode 100644 index 0000000..e5b7c4c --- /dev/null +++ b/build.ts @@ -0,0 +1,54 @@ +import { bundleAsync } from 'lightningcss' +import { ensureDir } from '$std/fs/mod.ts' +import { parse, resolve } from '$std/path/mod.ts' + +console.log('building styles starts') + +try { + //prevent Deno from exiting before bundle + setTimeout(() => {}, 2_000) + + const cssImports = new Map() + + const { code, map } = await bundleAsync({ + filename: './static/stylesheets/main.css', + minify: true, + sourceMap: true, + resolver: { + read(path) { + return Deno.readTextFile(path) + }, + async resolve(specifier, from) { + //resolve local files normally + if (!specifier.startsWith('https://')) { + return resolve(parse(from).dir, specifier) + } + //use cache for remote + if (cssImports.has(specifier)) return cssImports.get(specifier) + + //update cache for new remote + const response = await fetch(specifier) + const file = await response.text() + + await ensureDir('.cache') + const filename = `.cache/${crypto.randomUUID()}.css` + await Deno.writeTextFile(filename, file) + cssImports.set(specifier, filename) + return filename + }, + }, + }) + + await Deno.writeTextFile( + './static/styles.css', + new TextDecoder().decode(code), + ) + await Deno.writeTextFile( + './static/styles.map.css', + new TextDecoder().decode(map!), + ) +} catch (error) { + console.error(error) +} + +console.log('building styles finish') diff --git a/deno.json b/deno.json index 5dc8606..53c1301 100644 --- a/deno.json +++ b/deno.json @@ -7,7 +7,9 @@ "start": "deno run -A --watch=static/,routes/ dev.ts", "build": "deno run -A dev.ts build", "preview": "deno run -A main.ts", - "update": "deno run -A -r https://fresh.deno.dev/update ." + "update": "deno run -A -r https://fresh.deno.dev/update .", + "assets:watch": "deno run -A --watch=static/stylesheets/ build.ts", + "assets": "deno run -A build.ts" }, "fmt": { "singleQuote": true, @@ -33,7 +35,8 @@ "@preact/signals-core": "https://esm.sh/*@preact/signals-core@1.5.0", "$std/": "https://deno.land/std@0.208.0/", "univoq": "https://deno.land/x/univoq@0.1.0/mod.ts", - "@univoq/": "https://deno.land/x/univoq@0.1.0/" + "@univoq/": "https://deno.land/x/univoq@0.1.0/", + "lightningcss": "npm:lightningcss@1.22.1" }, "compilerOptions": { "jsx": "react-jsx",