-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.mjs
More file actions
58 lines (52 loc) · 1.64 KB
/
build.mjs
File metadata and controls
58 lines (52 loc) · 1.64 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
import { build } from 'esbuild'
import { copyFileSync, existsSync, mkdirSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
const distDir = fileURLToPath(new URL('./dist', import.meta.url))
if (!existsSync(distDir)) {
mkdirSync(distDir, { recursive: true })
}
const commonOptions = {
entryPoints: ['src/index.ts'],
bundle: true,
platform: 'node',
sourcemap: true,
target: 'node18',
external: [
'prettier',
// 'cosmiconfig',
// '@iarna/toml',
// '@wasm-fmt/ruff_fmt',
],
}
// Build ESM version
await build({
...commonOptions,
format: 'esm',
outfile: fileURLToPath(new URL('./dist/index.js', import.meta.url)),
})
// Build CJS version for VS Code extension compatibility
// Inject import.meta.url polyfill for WASM loaders that use it
const importMetaUrlShim = `
var import_meta_url = typeof document === 'undefined'
? require('url').pathToFileURL(__filename).href
: (document.currentScript && document.currentScript.src || new URL('index.cjs', document.baseURI).href);
`
await build({
...commonOptions,
format: 'cjs',
outfile: fileURLToPath(new URL('./dist/index.cjs', import.meta.url)),
banner: { js: importMetaUrlShim },
define: {
'import.meta.url': 'import_meta_url',
},
})
console.log('Build complete: ESM and CJS')
// Copy WASM file
const wasmSrc = fileURLToPath(new URL('./node_modules/@wasm-fmt/ruff_fmt/ruff_fmt_bg.wasm', import.meta.url))
const wasmDest = fileURLToPath(new URL('./dist/ruff_fmt_bg.wasm', import.meta.url))
if (existsSync(wasmSrc)) {
copyFileSync(wasmSrc, wasmDest)
console.log('Copied WASM file to dist')
} else {
console.warn('Warning: WASM file not found at', wasmSrc)
}