website/build.ts

55 lines
1.3 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({
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()
const filename = `_fresh/${crypto.randomUUID()}`
2024-01-14 20:50:44 +01:00
await Deno.writeTextFile(filename, file)
cssImports.set(specifier, filename)
return filename
},
},
})
await Deno.writeTextFile(
'./_fresh/styles.css',
2024-01-14 20:50:44 +01:00
new TextDecoder().decode(code),
)
await Deno.writeTextFile(
'./_fresh/styles.map.css',
2024-01-14 20:50:44 +01:00
new TextDecoder().decode(map!),
)
} catch (error) {
console.error(error)
}
console.log('building styles finish')