chore: add css build script

This commit is contained in:
Julien Oculi 2024-01-14 20:50:44 +01:00
parent 209865f57e
commit be6b7641d2
3 changed files with 61 additions and 2 deletions

2
.gitignore vendored
View file

@ -9,3 +9,5 @@
_fresh/
# npm dependencies
node_modules/
.cache/

54
build.ts Normal file
View file

@ -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')

View file

@ -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",