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') try { //prevent Deno from exiting before bundle setTimeout(() => {}, 2_000) const cssImports = new Map() const { code, map } = await bundleAsync({ filename: './src/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.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 }, }, }) await Deno.writeTextFile( './static/dev/styles.css', new TextDecoder().decode(code), ) await Deno.writeTextFile( './static/dev/styles.map.css', new TextDecoder().decode(map ?? new Uint8Array()), ) } catch (error) { console.error(error) } console.log('building styles finish')