55 lines
1.3 KiB
TypeScript
55 lines
1.3 KiB
TypeScript
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')
|