72 lines
2.6 KiB
JavaScript
72 lines
2.6 KiB
JavaScript
/* 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',
|
||
// proxy-agent:ali-oss 底下的 urllib 只在显式开代理时才去 require 它,我们不开,也没装
|
||
external: ['vscode', 'proxy-agent'],
|
||
format: 'cjs',
|
||
platform: 'node',
|
||
// node 平台默认先认 package.json 的 main,改成先认 module(ESM 入口):jsonc-parser 这类包的 main 是 UMD 版,
|
||
// 运行时才 require 自家的 ./impl/*,esbuild 打不进包,装上就加载失败;ESM 版的 import 能整个打进来
|
||
mainFields: ['module', 'main'],
|
||
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',
|
||
// 包入口就在 wasm/(main 指向 wasm/tree-sitter.js),rust 的语法包和 python 的在同一层
|
||
'tree-sitter-rust.wasm',
|
||
]
|
||
|
||
// @vscode/tree-sitter-wasm 没有的语法包,从 tree-sitter-wasm(预编译、签过名的各家语法包)拿:语言名 → 它的 wasm;json 只给工作区索引读接口契约用
|
||
const EXTRA_GRAMMARS = ['dockerfile', 'json']
|
||
|
||
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)
|
||
})
|