website/build.ts

57 lines
1.5 KiB
TypeScript
Raw Normal View History

2024-01-14 20:50:44 +01:00
import { bundleAsync } from 'lightningcss'
import { ensureDir } from '$std/fs/mod.ts'
import { parse, resolve } from '$std/path/mod.ts'
console.log('building styles starts')
await ensureDir('_fresh')
2024-01-14 20:50:44 +01:00
try {
//prevent Deno from exiting before bundle
setTimeout(() => {}, 2_000)
const cssImports = new Map()
const { code, map } = await bundleAsync({
2024-01-14 21:10:16 +01:00
filename: './src/stylesheets/main.css',
2024-01-14 20:50:44 +01:00
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)
2024-01-14 21:10:16 +01:00
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 filepath = `_fresh/${filename}`
await Deno.writeFile(filepath, new Uint8Array(file))
cssImports.set(specifier, filepath)
return filepath
2024-01-14 20:50:44 +01:00
},
},
})
await Deno.writeTextFile(
'./static/dev/styles.css',
2024-01-14 20:50:44 +01:00
new TextDecoder().decode(code),
)
await Deno.writeTextFile(
'./static/dev/styles.map.css',
new TextDecoder().decode(map ?? new Uint8Array()),
2024-01-14 20:50:44 +01:00
)
} catch (error) {
console.error(error)
}
console.log('building styles finish')