-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathesbuild.mjs
More file actions
162 lines (146 loc) · 4.31 KB
/
Copy pathesbuild.mjs
File metadata and controls
162 lines (146 loc) · 4.31 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import * as esbuild from "esbuild"
import * as fs from "fs"
import * as path from "path"
import { fileURLToPath } from "url"
import process from "node:process"
import * as console from "node:console"
import { copyPaths, copyWasms, copyLocales, setupLocaleWatcher } from "@roo-code/build"
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
async function removeDirWithRetries(dirPath, retries = 5, retryDelayMs = 200) {
for (let attempt = 0; attempt <= retries; attempt++) {
try {
await fs.promises.rm(dirPath, { recursive: true, force: true })
return
} catch (error) {
const isRetryable = error?.code === "ENOTEMPTY" || error?.code === "EBUSY" || error?.code === "EPERM"
const isLastAttempt = attempt === retries
if (!isRetryable || isLastAttempt) {
throw error
}
await new Promise((resolve) => globalThis.setTimeout(resolve, retryDelayMs * (attempt + 1)))
}
}
}
async function main() {
const name = "extension"
const production = process.argv.includes("--production")
const watch = process.argv.includes("--watch")
const minify = production
const sourcemap = true // Always generate source maps for error handling.
/**
* @type {import('esbuild').BuildOptions}
*/
const buildOptions = {
bundle: true,
minify,
sourcemap,
logLevel: "silent",
format: "cjs",
sourcesContent: false,
platform: "node",
define: {
"process.env.PKG_RELEASE_CHANNEL": JSON.stringify(process.env.PKG_RELEASE_CHANNEL || "stable"),
"process.env.POSTHOG_API_KEY": JSON.stringify(process.env.POSTHOG_API_KEY || ""),
},
}
const srcDir = __dirname
const buildDir = __dirname
const distDir = path.join(buildDir, "dist")
if (fs.existsSync(distDir)) {
console.log(`[${name}] Cleaning dist directory: ${distDir}`)
await removeDirWithRetries(distDir)
}
/**
* @type {import('esbuild').Plugin[]}
*/
const plugins = [
{
name: "copyFiles",
setup(build) {
build.onEnd(() => {
copyPaths(
[
["../README.md", "README.md"],
["../CHANGELOG.md", "CHANGELOG.md"],
["../LICENSE", "LICENSE"],
["../.env", ".env", { optional: true }],
["node_modules/vscode-material-icons/generated", "assets/vscode-material-icons"],
["../webview-ui/audio", "webview-ui/audio"],
["assets/marketplace", "dist/assets/marketplace"],
],
srcDir,
buildDir,
)
})
},
},
{
name: "copyWasms",
setup(build) {
build.onEnd(() => copyWasms(srcDir, distDir))
},
},
{
name: "copyLocales",
setup(build) {
build.onEnd(() => copyLocales(srcDir, distDir))
},
},
{
name: "esbuild-problem-matcher",
setup(build) {
build.onStart(() => console.log("[esbuild-problem-matcher#onStart]"))
build.onEnd((result) => {
result.errors.forEach(({ text, location }) => {
console.error(`✘ [ERROR] ${text}`)
if (location && location.file) {
console.error(` ${location.file}:${location.line}:${location.column}:`)
}
})
console.log("[esbuild-problem-matcher#onEnd]")
})
},
},
]
/**
* @type {import('esbuild').BuildOptions}
*/
const extensionConfig = {
...buildOptions,
plugins,
entryPoints: ["extension.ts"],
outfile: "dist/extension.js",
// global-agent must be external because it dynamically patches Node.js http/https modules
// which breaks when bundled. It needs access to the actual Node.js module instances.
// undici must be bundled because our VSIX is packaged with `--no-dependencies`.
external: ["vscode", "esbuild", "global-agent", "@vscode/ripgrep"],
}
/**
* @type {import('esbuild').BuildOptions}
*/
const workerConfig = {
...buildOptions,
entryPoints: ["workers/countTokens.ts"],
outdir: "dist/workers",
}
const [extensionCtx, workerCtx] = await Promise.all([
esbuild.context(extensionConfig),
esbuild.context(workerConfig),
])
if (watch) {
await Promise.all([extensionCtx.watch(), workerCtx.watch()])
copyLocales(srcDir, distDir)
setupLocaleWatcher(srcDir, distDir)
} else {
// Run sequentially on rebuild to avoid Windows EBUSY races when both
// onEnd hooks copy the same asset directories concurrently.
await extensionCtx.rebuild()
await workerCtx.rebuild()
await Promise.all([extensionCtx.dispose(), workerCtx.dispose()])
}
}
main().catch((e) => {
console.error(e)
process.exit(1)
})