Files
superwork/vscode/esbuild.mjs
T

66 lines
2.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* eslint-disable no-console -- 构建脚本,向终端打印进度是它的正当输出 */
import { copyFileSync, mkdirSync } from 'node:fs'
import { createRequire } from 'node:module'
import path from 'node:path'
import process from 'node:process'
import * as esbuild from 'esbuild'
import { getWasmPath } from 'tree-sitter-wasm'
const production = process.argv.includes('--production')
const watch = process.argv.includes('--watch')
/** @type {import('esbuild').BuildOptions} */
const options = {
entryPoints: ['src/extension.ts'],
bundle: true,
outfile: 'dist/index.cjs',
external: ['vscode'],
format: 'cjs',
platform: 'node',
target: 'node20',
sourcemap: !production,
minify: production,
logLevel: 'info',
}
// 代码阅读的解析运行时(web-tree-sitter,由 esbuild 打进 index.cjs)和它要加载的 wasm:
// 运行时按 __dirname 找 dist/tree-sitter/,语法包只带用到的几种语言
const TREE_SITTER_WASM = [
'tree-sitter.wasm',
'tree-sitter-typescript.wasm',
'tree-sitter-tsx.wasm',
'tree-sitter-javascript.wasm',
'tree-sitter-python.wasm',
'tree-sitter-bash.wasm',
]
// @vscode/tree-sitter-wasm 没有的语法包,从 tree-sitter-wasm(预编译、签过名的各家语法包)拿:语言名 → 它的 wasm
const EXTRA_GRAMMARS = ['dockerfile']
function copyTreeSitterWasm() {
const from = path.dirname(createRequire(import.meta.url).resolve('@vscode/tree-sitter-wasm'))
const to = 'dist/tree-sitter'
mkdirSync(to, { recursive: true })
for (const file of TREE_SITTER_WASM)
copyFileSync(path.join(from, file), path.join(to, file))
for (const language of EXTRA_GRAMMARS)
copyFileSync(getWasmPath(language), path.join(to, `tree-sitter-${language}.wasm`))
}
async function main() {
copyTreeSitterWasm()
if (watch) {
const ctx = await esbuild.context(options)
await ctx.watch()
console.log('[esbuild] watching...')
}
else {
await esbuild.build(options)
}
}
main().catch((err) => {
console.error(err)
process.exit(1)
})