-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
190 lines (176 loc) · 6.5 KB
/
build.js
File metadata and controls
190 lines (176 loc) · 6.5 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import esbuild from 'esbuild'
import {
copyFileSync,
cpSync,
createWriteStream,
existsSync,
mkdirSync,
realpathSync,
rmSync,
statSync,
} from 'fs'
import https from 'https'
import { createRequire } from 'module'
import { dirname, join, resolve } from 'path'
const require = createRequire(import.meta.url)
// Find the WASM file, trying local resolution first, then downloading from unpkg
async function findOrDownloadWasm(destPath) {
// Try to find locally via require.resolve
try {
const entryPath = require.resolve('@wasm-fmt/ruff_fmt')
let dir = dirname(entryPath)
while (dir !== dirname(dir)) {
if (existsSync(join(dir, 'package.json'))) {
const wasmPath = join(dir, 'ruff_fmt_bg.wasm')
// Check file actually exists and has content (not a broken symlink)
try {
const stats = statSync(wasmPath)
if (stats.isFile() && stats.size > 0) {
console.log(`Found ruff_fmt_bg.wasm locally at ${wasmPath}`)
copyFileSync(wasmPath, destPath)
return
}
} catch {
// File doesn't exist or is broken symlink
}
break
}
dir = dirname(dir)
}
} catch {
// Package not resolvable
}
// Fallback: download from unpkg
console.log('WASM not found locally, downloading from unpkg...')
const url = 'https://unpkg.com/@wasm-fmt/ruff_fmt@0.9.7/ruff_fmt_bg.wasm'
await new Promise((resolve, reject) => {
const file = createWriteStream(destPath)
https
.get(url, (response) => {
if (response.statusCode === 302 || response.statusCode === 301) {
// Follow redirect
https
.get(response.headers.location, (res) => {
res.pipe(file)
file.on('finish', () => {
file.close()
resolve()
})
})
.on('error', reject)
} else if (response.statusCode === 200) {
response.pipe(file)
file.on('finish', () => {
file.close()
resolve()
})
} else {
reject(new Error(`Failed to download WASM: HTTP ${response.statusCode}`))
}
})
.on('error', reject)
})
console.log('Downloaded ruff_fmt_bg.wasm from unpkg')
}
const production = process.argv.includes('--production')
const watch = process.argv.includes('--watch')
const projectRoot = resolve('.')
async function main() {
const ctx = await esbuild.context({
entryPoints: ['src/extension.ts'],
bundle: true,
format: 'cjs',
minify: production,
sourcemap: !production,
sourcesContent: false,
platform: 'node',
outfile: 'out/extension.cjs',
mainFields: ['module', 'main'],
loader: {
'.wasm': 'file',
},
external: ['vscode', 'prettier', 'prettier-plugin-pywire'],
logLevel: 'warning',
plugins: [
{
name: 'esbuild-problem-matcher',
setup(build) {
build.onStart(() => {
console.log('[watch] build started')
})
build.onEnd(async (result) => {
result.errors.forEach(({ text, location }) => {
console.error(`✘ [ERROR] ${text}`)
console.error(` ${location.file}:${location.line}:${location.column}:`)
})
console.log('[watch] build finished')
// Copy lsp_launcher.py to out/ after successful build
if (result.errors.length === 0) {
try {
const outDir = join(projectRoot, 'out')
mkdirSync(outDir, { recursive: true })
copyFileSync(
join(projectRoot, 'src/lsp_launcher.py'),
join(outDir, 'lsp_launcher.py')
)
console.log('Copied lsp_launcher.py to out/')
} catch (e) {
console.error('Failed to copy lsp_launcher.py:', e)
}
// Copy prettier modules to out/node_modules for runtime resolution
try {
const outNodeModulesDir = join(projectRoot, 'out', 'node_modules')
// Clear old copies to avoid stale files
if (existsSync(outNodeModulesDir)) {
rmSync(outNodeModulesDir, { recursive: true, force: true })
}
mkdirSync(outNodeModulesDir, { recursive: true })
const modulesToCopy = ['prettier', 'prettier-plugin-pywire']
for (const moduleName of modulesToCopy) {
let sourceDir = join(projectRoot, 'node_modules', moduleName)
// Special case for our workspace: if prettier-plugin-pywire is missing in node_modules
// (e.g. installed from Git but not built), check if it exists as a sibling in the workspace.
if (moduleName === 'prettier-plugin-pywire') {
const localSibling = join(projectRoot, '..', 'prettier-plugin-pywire')
// If local sibling exists and has a dist folder, prefer it
if (existsSync(join(localSibling, 'dist', 'index.cjs'))) {
console.log(`Using local sibling for ${moduleName}: ${localSibling}`)
sourceDir = localSibling
}
}
if (!existsSync(sourceDir)) {
console.warn(`Module not found for bundling: ${moduleName} at ${sourceDir}`)
continue
}
// Follow symlinks (pnpm uses symlinks)
const realSourceDir = realpathSync(sourceDir)
const targetDir = join(outNodeModulesDir, moduleName)
cpSync(realSourceDir, targetDir, { recursive: true, dereference: true })
console.log(`Copied ${moduleName} to out/node_modules/ from ${sourceDir}`)
}
// Copy ruff WASM file next to the plugin's CJS bundle
const pluginDistDir = join(outNodeModulesDir, 'prettier-plugin-pywire', 'dist')
mkdirSync(pluginDistDir, { recursive: true })
const wasmDest = join(pluginDistDir, 'ruff_fmt_bg.wasm')
await findOrDownloadWasm(wasmDest)
} catch (e) {
console.error('Failed to copy node_modules:', e)
process.exit(1)
}
}
})
},
},
],
})
if (watch) {
await ctx.watch()
} else {
await ctx.rebuild()
await ctx.dispose()
}
}
main().catch((e) => {
console.error(e)
process.exit(1)
})