chore: add css build script
This commit is contained in:
parent
209865f57e
commit
be6b7641d2
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -9,3 +9,5 @@
|
||||||
_fresh/
|
_fresh/
|
||||||
# npm dependencies
|
# npm dependencies
|
||||||
node_modules/
|
node_modules/
|
||||||
|
|
||||||
|
.cache/
|
54
build.ts
Normal file
54
build.ts
Normal 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')
|
|
@ -7,7 +7,9 @@
|
||||||
"start": "deno run -A --watch=static/,routes/ dev.ts",
|
"start": "deno run -A --watch=static/,routes/ dev.ts",
|
||||||
"build": "deno run -A dev.ts build",
|
"build": "deno run -A dev.ts build",
|
||||||
"preview": "deno run -A main.ts",
|
"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": {
|
"fmt": {
|
||||||
"singleQuote": true,
|
"singleQuote": true,
|
||||||
|
@ -33,7 +35,8 @@
|
||||||
"@preact/signals-core": "https://esm.sh/*@preact/signals-core@1.5.0",
|
"@preact/signals-core": "https://esm.sh/*@preact/signals-core@1.5.0",
|
||||||
"$std/": "https://deno.land/std@0.208.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/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": {
|
"compilerOptions": {
|
||||||
"jsx": "react-jsx",
|
"jsx": "react-jsx",
|
||||||
|
|
Loading…
Reference in a new issue