-
Notifications
You must be signed in to change notification settings - Fork 19
/
esbuild.js
129 lines (114 loc) · 4.56 KB
/
esbuild.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
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
/* eslint-disable no-useless-escape */
// esbuild.js
import { execSync } from "child_process"
import { build, context } from "esbuild"
import fs from "fs"
const stripJSONComments = (data) => {
var re = new RegExp("\/\/(.*)", "g")
return data.toString().replace(re, "")
}
const tsc = JSON.parse(stripJSONComments(fs.readFileSync("./tsconfig.json")))
const pkg = JSON.parse(fs.readFileSync("./package.json"))
const watch = process.argv.includes("--watch")
const dev = process.argv.includes("--dev") || process.env.NODE_ENV === "development"
const nodeMajor = 18 // process.versions.node.split(".")[0]
const nodeMinor = 18 // process.versions.node.split(".")[1]
if (nodeMajor < 14 || (nodeMajor === 14 && nodeMinor < 17)) {
console.error("\u001b[31mNode 14.17 or higher is required to run this project!\u001b[37m")
process.exit(1)
}
let target = ["node18.18", "es2020"]
if (nodeMajor === 14 && nodeMinor >= 17) {
target = [`node${nodeMajor}.${nodeMinor}`, "es2020"]
} else if ((nodeMajor === 15 && nodeMinor >= 14) || (nodeMajor >= 16 && nodeMajor < 17)) {
target = [`node${nodeMajor}.${nodeMinor}`, "es2021"]
} else if ((nodeMajor === 17 && nodeMinor >= 9) || (nodeMajor >= 18 && nodeMajor <= 20)) {
target = [`node${nodeMajor}.${nodeMinor}`, "es2022"]
}
tsc.compilerOptions.target = target[1]
// write tsconfig.json to a temp file
fs.writeFileSync("./tsconfig_esbuild.json", JSON.stringify(tsc, null, 4))
console.log(`\u001b[36mBuilding for Node ${target[0]}, ECMAScript ${target[1]}...\u001b[37m`)
const banner = "/* eslint-disable linebreak-style */\n" +
"/* \n" +
" ____ _ ____ _ _____ _ \n" +
" / ___|___ _ __ ___ ___ | | ___ / ___|_ _(_) |_ _|__ ___ | |___ \n" +
" | | / _ \\| '_ \\/ __|/ _ \\| |/ _ \\ | | _| | | | | | |/ _ \\ / _ \\| / __| \n" +
" | |__| (_) | | | \\__ \\ (_) | | __/ | |_| | |_| | | | | (_) | (_) | \\__ \\ \n" +
" \\____\\___/|_| |_|___/\\___/|_|\\___| \\____|\\__,_|_| |_|\\___/ \\___/|_|___/ \n" +
" \n" +
` v${pkg.version} \n\n\n` +
` ${pkg.description} \n\n` +
` Author: ${pkg.author}\n` +
` License: ${pkg.license}\n` +
` Repository: ${pkg.repository.url}\n\n` +
" This program is free software: you can redistribute it and/or modify\n\n" +
` Build: ${new Date().toUTCString()} for Node ${target[0]}, ECMAScript ${target[1]}\n*/\n`
const buildOptions = {
bundle: true,
platform: "node",
target: target,
minifySyntax: dev ? true : false,
minify: dev ? false : true,
sourcemap: true,
format: "esm",
color: true,
metafile: dev ? true : false,
tsconfig: "./tsconfig_esbuild.json",
outExtension: { ".js": ".mjs" },
banner: {
js: banner
},
outdir: "dist/esm",
entryPoints: ["src/ConsoleGui.ts"],
plugins: [
{
name: "TypeScriptDeclarationsPlugin",
setup(build) {
build.onEnd((result) => {
if (result.errors.length > 0) return
execSync("npx tsc --emitDeclarationOnly --declarationDir ./dist/types -p ./tsconfig_esbuild.json")
})
}
},
{
name: "rebuildListener",
setup(build) {
let count = 0
build.onEnd(result => {
if (count++ === 0) console.log("First build:", result)
else console.log("Subsequent build:", result)
})
},
}
]
}
const buildOptionsCjs = {
...buildOptions,
format: "cjs",
outExtension: { ".js": ".cjs" },
outdir: "dist/cjs",
plugins: []
}
if (dev) {
const ctxEsm = await context(buildOptions)
const ctxCjs = await context(buildOptionsCjs)
if (watch) {
await ctxEsm.watch().then(() => {
console.log("\u001b[36mWatching ESM...\u001b[37m")
})
await ctxCjs.watch().then(() => {
console.log("\u001b[36mWatching CJS...\u001b[37m")
})
}
} else {
// ESM Build
await build(buildOptions)
console.log("\u001b[36mESM Build succeeded!\u001b[37m")
// CJS Build
await build(buildOptionsCjs)
console.log("\u001b[36mCJS Build succeeded!\u001b[37m")
}
process.on("exit", () => {
fs.unlinkSync("./tsconfig_esbuild.json")
})