/** * 修补 uview-plus 自带的 tsconfig.json(位于 node_modules,`npm install` 会重建)。 * * 背景 * uview-plus 3.8.125 的 tsconfig.json 里写的是 `"moduleResolution": "Node"`。 * TypeScript 6.0 起 node10 解析模式被标记为“已弃用”,会直接报错: * error TS5107: Option 'moduleResolution=node10' is deprecated and will stop * functioning in TypeScript 7.0. Specify compilerOption * '"ignoreDeprecations": "6.0"' to silence this error. * * 在 VS Code 里点进 uview-plus 的组件源码(node_modules/uview-plus/**,例如 * Ctrl+点击模板里的 )时,TS 语言服务会把该目录下的 tsconfig.json * 当成一个“配置项目”加载,于是这条错误就出现在 Problems 面板里。 * * 它**不影响** uni-app / 微信小程序的构建(构建走 Vite + uni 编译,不用这个 tsconfig), * 只是编辑器里的噪音。 * * 做法 * 按 TS 官方提示,往该文件补一个 `"ignoreDeprecations": "6.0"`。 * 仅在本地安装的 TypeScript 主版本 >= 6 时执行 —— TS 5.x 不认识 "6.0" 这个值, * 会反过来报“无效的 ignoreDeprecations”,所以低版本一律跳过。 * * 本脚本由 package.json 的 postinstall 自动调用,也可手动执行: * node scripts/patch-uview-plus-tsconfig.mjs */ import { existsSync, readFileSync, writeFileSync } from 'node:fs'; import { createRequire } from 'node:module'; import { dirname, join, resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; const require = createRequire(import.meta.url); const projectRoot = resolve(dirname(fileURLToPath(import.meta.url)), '..'); const target = join(projectRoot, 'node_modules', 'uview-plus', 'tsconfig.json'); const tag = '[patch-uview-plus]'; const IGNORE_VALUE = '6.0'; /** 读取本地安装的 TypeScript 主版本号,取不到返回 NaN。 */ function readTypeScriptMajor() { try { const version = require('typescript/package.json').version; return Number.parseInt(String(version).split('.')[0], 10); } catch { return Number.NaN; } } function main() { if (!existsSync(target)) { console.log(`${tag} 未找到 ${target},跳过(可能尚未安装依赖)。`); return; } const major = readTypeScriptMajor(); if (!(major >= 6)) { console.log(`${tag} TypeScript 主版本 ${Number.isNaN(major) ? '未知' : major} < 6,无需处理,跳过。`); return; } let json; try { json = JSON.parse(readFileSync(target, 'utf8')); } catch (error) { console.warn(`${tag} 解析失败,跳过:${error.message}`); return; } const options = json.compilerOptions ?? (json.compilerOptions = {}); if (options.ignoreDeprecations === IGNORE_VALUE) { console.log(`${tag} 已修补过,跳过。`); return; } options.ignoreDeprecations = IGNORE_VALUE; writeFileSync(target, `${JSON.stringify(json, null, 2)}\n`, 'utf8'); console.log(`${tag} 已为 node_modules/uview-plus/tsconfig.json 添加 "ignoreDeprecations": "${IGNORE_VALUE}"。`); } main();