-
Notifications
You must be signed in to change notification settings - Fork 24
/
esbuild.ts
129 lines (117 loc) · 3.1 KB
/
esbuild.ts
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import {
buildSync,
Loader,
transformSync,
CommonOptions,
TransformOptions,
BuildOptions,
} from "esbuild"
import fs from "fs"
import path from "path"
import { PackageJson } from "type-fest"
import cache from "./cache"
export type TranspileOptions = {
type: "bundle" | "transform"
debug: boolean
esbuild?: CommonOptions & TransformOptions & BuildOptions
}
const defaultOptions: TranspileOptions = { type: "bundle", debug: false }
const commonOptions: CommonOptions = {
format: "cjs",
logLevel: "error",
target: [`node${process.version.slice(1)}`],
minify: false,
sourcemap: "inline",
}
const pkgPath = path.resolve(".", "package.json")
let externals: string[] = []
if (fs.existsSync(pkgPath)) {
const pkg = JSON.parse(
fs.readFileSync(pkgPath, { encoding: "utf-8" })
) as PackageJson
externals = [
...Object.keys(pkg.dependencies ?? {}),
...Object.keys(pkg.devDependencies ?? {}),
]
}
export const loaders: Record<string, Loader> = {
".js": "js",
".mjs": "js",
".cjs": "js",
".jsx": "jsx",
".ts": "ts",
".tsx": "tsx",
// ".css": "css",
".json": "json",
// ".txt": "text",
}
export function supports(filename: string) {
if (filename.includes("node_modules")) return false
return path.extname(filename) in loaders
}
function _transform(
code: string,
filename: string,
options: TranspileOptions
): string {
const loaders = getLoaders(options)
const ret = transformSync(code, {
...commonOptions,
...(options.esbuild as TransformOptions | undefined),
loader: loaders[path.extname(filename)],
sourcefile: filename,
})
return ret.code
}
function getLoaders(options: TranspileOptions) {
const ret = { ...loaders }
if (typeof options.esbuild?.loader == "object") {
for (const [e, l] of Object.entries(options.esbuild.loader))
ret[e] = l as Loader
}
return ret
}
function _bundle(
code: string,
filename: string,
options: TranspileOptions
): string {
const ext = path.extname(filename)
const loaders = getLoaders(options)
return buildSync({
...commonOptions,
platform: "node",
...(options.esbuild as BuildOptions | undefined),
loader: loaders,
bundle: true,
stdin: {
sourcefile: filename,
contents: code,
resolveDir: path.dirname(filename),
loader: loaders[ext],
},
external: [...externals, ...(options?.esbuild?.external ?? [])],
write: false,
})
.outputFiles.map((f) => f.text)
.join("\n")
}
export function transpile(
code: string,
filename: string,
_options?: Partial<TranspileOptions>
): string {
const options: TranspileOptions = { ...defaultOptions, ..._options }
if (options.type == "bundle") {
// eslint-disable-next-line no-console
if (options.debug) console.log(`📦 ${filename}`)
return _bundle(code, filename, options)
} else if (options.type == "transform") {
return cache.get(filename, () => {
// eslint-disable-next-line no-console
if (options.debug) console.log(`📦 ${filename}`)
return _transform(code, filename, options)
})
}
throw new Error(`Invalid transpilation option ${options.type}`)
}