From 6d419210c649212ea5a614f5da99062f66815fa8 Mon Sep 17 00:00:00 2001 From: cruldra Date: Tue, 13 Oct 2026 11:36:24 +1100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(vscode):=20=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=200.2.270=20=E8=A3=85=E4=B8=8A=E5=90=8E=E6=BF=80?= =?UTF-8?q?=E6=B4=BB=E5=A4=B1=E8=B4=A5=EF=BC=9Ajsonc-parser=20=E6=94=B9?= =?UTF-8?q?=E6=89=93=20ESM=20=E7=89=88=EF=BC=8C=E6=89=93=E5=8C=85=E6=97=B6?= =?UTF-8?q?=E5=85=88=E8=AF=95=E5=8A=A0=E8=BD=BD=E4=BA=A7=E7=89=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vscode/check-bundle.mjs | 33 +++++++++++++++++++++++++++++++++ vscode/esbuild.mjs | 3 +++ vscode/package.json | 2 +- 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 vscode/check-bundle.mjs diff --git a/vscode/check-bundle.mjs b/vscode/check-bundle.mjs new file mode 100644 index 0000000..38a13a8 --- /dev/null +++ b/vscode/check-bundle.mjs @@ -0,0 +1,33 @@ +/* eslint-disable no-console -- 构建脚本,向终端打印检查结果是它的正当输出 */ +// 检查打包产物能被扩展宿主加载:给一个桩 vscode 模块,require 产物,顶层代码一抛错就以非零退出。 +// 单元测试跑的是源码,看不见打包带来的问题(比如依赖被打成运行时还要 require 自家文件的版本),只有加载产物才能发现。 +// 用法:node check-bundle.mjs [产物路径,默认 dist/index.cjs] +import Module, { createRequire } from 'node:module' +import path from 'node:path' +import process from 'node:process' + +const bundle = path.resolve(process.argv[2] ?? 'dist/index.cjs') + +// 桩:取任何属性、调用、new 都得到它自己,够顶层代码(建 EventEmitter、继承 TreeItem 之类)跑完; +// 被当成值用时(拿枚举成员当对象的键)转成 0,跟真的枚举值一样是个数。底下垫一个类,调用和 new 才都能被截住 +const vscodeStub = new Proxy(class {}, { + get: (_, key) => key === Symbol.toPrimitive ? () => 0 : vscodeStub, + apply: () => vscodeStub, + construct: () => vscodeStub, +}) + +// 扩展宿主也是这样把 vscode 交给扩展的:截住模块加载,请求的是 vscode 就给 API 对象 +const load = Module._load +Module._load = function (request, ...rest) { + return request === 'vscode' ? vscodeStub : load.call(this, request, ...rest) +} + +try { + createRequire(import.meta.url)(bundle) + console.log(`[check-bundle] ${bundle} 加载正常`) +} +catch (err) { + console.error(`[check-bundle] ${bundle} 加载失败:`) + console.error(err) + process.exit(1) +} diff --git a/vscode/esbuild.mjs b/vscode/esbuild.mjs index b473633..0c4a926 100644 --- a/vscode/esbuild.mjs +++ b/vscode/esbuild.mjs @@ -18,6 +18,9 @@ const options = { 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, diff --git a/vscode/package.json b/vscode/package.json index 3e27ba3..fe9a13b 100644 --- a/vscode/package.json +++ b/vscode/package.json @@ -2988,7 +2988,7 @@ } }, "scripts": { - "build": "pnpm --filter webview-ui build && node esbuild.mjs --production", + "build": "pnpm --filter webview-ui build && node esbuild.mjs --production && node check-bundle.mjs", "dev": "concurrently \"pnpm --filter webview-ui dev\" \"node esbuild.mjs --watch\"", "typecheck": "tsc --noEmit && pnpm --filter webview-ui typecheck", "lint": "eslint .",