-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.js
40 lines (27 loc) · 986 Bytes
/
build.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import fs from 'fs/promises'
import { transform } from 'esbuild'
import ts from 'typescript'
import tsconfig from './tsconfig.json' assert { type: 'json' }
console.log('Building...')
console.log('Starting transpile and minify...')
const input = await fs.readFile('Themes.ts')
const snippet = await transform(input, {
target: 'es6',
format: 'esm',
loader: 'ts',
minify: true,
})
console.log('Starting type generation...')
let types
const host = ts.createCompilerHost(tsconfig.compilerOptions)
host.writeFile = (fileName, contents) => (types = contents)
ts.createProgram(['Themes.ts'], tsconfig.compilerOptions, host).emit()
console.log('Writing output file...')
const template = await fs.readFile('Themes.astro.template')
const output = template
.toString()
.replace('{types}', types.trim())
.replace('{snippet}', snippet.code.trim())
await fs.mkdir('build', { recursive: true })
await fs.writeFile('build/Themes.astro', output)
console.log('Build complete!')