-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.mjs
More file actions
75 lines (66 loc) · 2.28 KB
/
Copy pathbuild.mjs
File metadata and controls
75 lines (66 loc) · 2.28 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { transformAsync } from "@babel/core"
import presetTypeScript from "@babel/preset-typescript"
import presetSolid from "babel-preset-solid"
import { execFileSync } from "node:child_process"
import { mkdir, readdir, readFile, rm, writeFile } from "node:fs/promises"
import { createRequire } from "node:module"
import { dirname, extname, join, relative, resolve } from "node:path"
import { fileURLToPath } from "node:url"
const require = createRequire(import.meta.url)
const root = resolve(dirname(fileURLToPath(import.meta.url)), "..")
const sourceRoot = join(root, "src")
const outputRoot = join(root, "dist")
async function sourceFiles(directory) {
const entries = await readdir(directory, { withFileTypes: true })
const nested = await Promise.all(
entries.map(async (entry) => {
const path = join(directory, entry.name)
if (entry.isDirectory()) return sourceFiles(path)
if (!entry.isFile() || entry.name.endsWith(".d.ts")) return []
return [".ts", ".tsx"].includes(extname(entry.name)) ? [path] : []
}),
)
return nested.flat()
}
async function transformSource(path) {
const isTsx = extname(path) === ".tsx"
const presets = [
...(isTsx
? [
[
presetSolid,
{ moduleName: "@opentui/solid", generate: "universal" },
],
]
: []),
[presetTypeScript, { allExtensions: true, isTSX: isTsx }],
]
const result = await transformAsync(await readFile(path, "utf8"), {
filename: path,
babelrc: false,
configFile: false,
presets,
sourceMaps: false,
})
if (typeof result?.code !== "string") {
throw new Error(`Babel produced no JavaScript for ${relative(root, path)}`)
}
const outputPath = join(
outputRoot,
`${relative(sourceRoot, path).replace(/\.tsx?$/, "")}.js`,
)
await mkdir(dirname(outputPath), { recursive: true })
await writeFile(outputPath, `${result.code}\n`, "utf8")
}
async function build() {
await rm(outputRoot, { recursive: true, force: true })
const tsc = require.resolve("typescript/bin/tsc")
execFileSync(
process.execPath,
[tsc, "-p", join(root, "tsconfig.json"), "--emitDeclarationOnly"],
{ cwd: root, stdio: "inherit" },
)
const files = await sourceFiles(sourceRoot)
await Promise.all(files.map(transformSource))
}
await build()