pnpm之monorepo项目, vite版本冲突, 导致vite.config.ts ts警告处理

在 monorepo 项目架构下,不同的仓库可能安装的 vite 版本是不同的, 有一些历史的老项目, 随着业务的变化,也有新的项目使用 最新的 vite@7.x 等初始化项目

比如, 我们可能在下面的代码片段中会出现 如下的 ts警告

ts 复制代码
const loadEnv = (mode: string) => {
    const envFiles = ["./env/.env", `./env/.env.${mode}`];

    const env: any = {};
    envFiles.forEach(envFile => {
        const loadedEnv = dotenv.config({ path: envFile, override: true });
        if (loadedEnv.error) {
            console.error(`环境变量文件加载失败 ${envFile}`);
        } else {
            Object.assign(env, loadedEnv.parsed || {});
        }
    });

    return env;
};

export default defineConfig(({ mode }: { mode: string }) => {
    console.log('当前环境:', mode);

    const globalEnv = loadEnv(mode)
    
     return {
      ...
     }
     
});

mode 的类型就会报错。

less 复制代码
没有与此调用匹配的重载。  
最后一个重载给出了以下错误。  
类型"({ mode }: ConfigEnv) => { define: { "process.env": Record<string, string>; }; plugins: (PluginOption | Plugin<any> | Plugin<any>[])[]; base: string; ... 4 more ...; css: { ...; }; }"的参数不能赋给类型"UserConfigExport"的参数。  
不能将类型"({ mode }: ConfigEnv) => { define: { "process.env": Record<string, string>; }; plugins: (PluginOption | Plugin<any> | Plugin<any>[])[]; base: string; ... 4 more ...; css: { ...; }; }"分配给类型"UserConfigFnObject"。  
调用签名返回类型 "{ define: { "process.env": Record<string, string>; }; plugins: (PluginOption | Plugin<any> | Plugin<any>[])[]; base: string; resolve: { alias: { "@": string; }; }; build: { ...; }; esbuild: { ...; }; server: { ...; }; css: { ...; }; }" 和 "UserConfig" 不兼容。  
在这些类型中,"plugins" 的类型不兼容。  
不能将类型"(PluginOption | Plugin<any> | Plugin<any>[])[]"分配给类型"PluginOption[]"。  
不能将类型"PluginOption | Plugin<any> | Plugin<any>[]"分配给类型"PluginOption"。  
不能将类型"Plugin<any>"分配给类型"PluginOption"。  
不能将类型"import("D:/work/test-web/node_modules/.pnpm/[vite@5.4.1]()9_@[types+node@24._c928461cc00da890efe4f1ccdfab0111]()/node_modules/vite/dist/node/index").Plugin<any>"分配给类型"import("D:/work/test-web/node_modules/.pnpm/[vite@6.3]().5_@[types+node@24.0_3221e9d8a7877bfd9756da51f493fce4]()/node_modules/vite/dist/node/index").Plugin<any>"。  
属性"apply"的类型不兼容。  
不能将类型""build" | "serve" | ((this: void, config: import("D:/work/test-web/node_modules/.pnpm/[vite@5.4.1]()9_@[types+node@24._c928461cc00da890efe4f1ccdfab0111]()/node_modules/vite/dist/node/index").UserConfig, env: import("D:/work/test-web/node_modules/.pnpm/[vite@5.4.1]()9_@[types+node@24._c928461cc00da89]()..."分配给类型""build" | "serve" | ((this: void, config: import("D:/work/test-web/node_modules/.pnpm/[vite@6.3]().5_@[types+node@24.0_3221e9d8a7877bfd9756da51f493fce4]()/node_modules/vite/dist/node/index").UserConfig, env: import("D:/work/test-web/node_modules/.pnpm/[vite@6.3]().5_@[types+node@24.0_3221e9d8a7877bf]()..."。  
不能将类型"(this: void, config: UserConfig, env: ConfigEnv) => boolean"分配给类型""build" | "serve" | ((this: void, config: UserConfig, env: ConfigEnv) => boolean)"。  
不能将类型"(this: void, config: import("D:/work/test-web/node_modules/.pnpm/[vite@5.4.1]()9_@[types+node@24._c928461cc00da890efe4f1ccdfab0111]()/node_modules/vite/dist/node/index").UserConfig, env: import("D:/work/test-web/node_modules/.pnpm/[vite@5.4.1]()9_@[types+node@24._c928461cc00da890efe4f1ccdfab0111]()/nod..."分配给类型"(this: void, config: import("D:/work/test-web/node_modules/.pnpm/[vite@6.3]().5_@[types+node@24.0_3221e9d8a7877bfd9756da51f493fce4]()/node_modules/vite/dist/node/index").UserConfig, env: import("D:/work/test-web/node_modules/.pnpm/[vite@6.3]().5_@[types+node@24.0_3221e9d8a7877bfd9756da51f493fce4]()/nod..."。  
参数"config"和"config" 的类型不兼容。  
不能将类型"import("D:/work/test-web/node_modules/.pnpm/[vite@6.3]().5_@[types+node@24.0_3221e9d8a7877bfd9756da51f493fce4]()/node_modules/vite/dist/node/index").UserConfig"分配给类型"import("D:/work/test-web/node_modules/.pnpm/[vite@5.4.1]()9_@[types+node@24._c928461cc00da890efe4f1ccdfab0111]()/node_modules/vite/dist/node/index").UserConfig"。  
属性"plugins"的类型不兼容。  
不能将类型"import("D:/work/test-web/node_modules/.pnpm/[vite@6.3]().5_@[types+node@24.0_3221e9d8a7877bfd9756da51f493fce4]()/node_modules/vite/dist/node/index").PluginOption[]"分配给类型"import("D:/work/test-web/node_modules/.pnpm/[vite@5.4.1]()9_@[types+node@24._c928461cc00da890efe4f1ccdfab0111]()/node_modules/vite/dist/node/index").PluginOption[]"。  
不能将类型"import("D:/work/test-web/node_modules/.pnpm/[vite@6.3]().5_@[types+node@24.0_3221e9d8a7877bfd9756da51f493fce4]()/node_modules/vite/dist/node/index").PluginOption"分配给类型"import("D:/work/test-web/node_modules/.pnpm/[vite@5.4.1]()9_@[types+node@24._c928461cc00da890efe4f1ccdfab0111]()/node_modules/vite/dist/node/index").PluginOption"。  
不能将类型"Plugin<any>"分配给类型"PluginOption"。  
不能将类型"import("D:/work/test-web/node_modules/.pnpm/[vite@6.3]().5_@[types+node@24.0_3221e9d8a7877bfd9756da51f493fce4]()/node_modules/vite/dist/node/index").Plugin<any>"分配给类型"import("D:/work/test-web/node_modules/.pnpm/[vite@5.4.1]()9_@[types+node@24._c928461cc00da890efe4f1ccdfab0111]()/node_modules/vite/dist/node/index").Plugin<any>"。  
属性"apply"的类型不兼容。  
不能将类型""build" | "serve" | ((this: void, config: import("D:/work/test-web/node_modules/.pnpm/[vite@6.3]().5_@[types+node@24.0_3221e9d8a7877bfd9756da51f493fce4]()/node_modules/vite/dist/node/index").UserConfig, env: import("D:/work/test-web/node_modules/.pnpm/[vite@6.3]().5_@[types+node@24.0_3221e9d8a7877bf]()..."分配给类型""build" | "serve" | ((this: void, config: import("D:/work/test-web/node_modules/.pnpm/[vite@5.4.1]()9_@[types+node@24._c928461cc00da890efe4f1ccdfab0111]()/node_modules/vite/dist/node/index").UserConfig, env: import("D:/work/test-web/node_modules/.pnpm/[vite@5.4.1]()9_@[types+node@24._c928461cc00da89]()..."。  
不能将类型"(this: void, config: UserConfig, env: ConfigEnv) => boolean"分配给类型""build" | "serve" | ((this: void, config: UserConfig, env: ConfigEnv) => boolean)"。  
不能将类型"(this: void, config: import("D:/work/test-web/node_modules/.pnpm/[vite@6.3]().5_@[types+node@24.0_3221e9d8a7877bfd9756da51f493fce4]()/node_modules/vite/dist/node/index").UserConfig, env: import("D:/work/test-web/node_modules/.pnpm/[vite@6.3]().5_@[types+node@24.0_3221e9d8a7877bfd9756da51f493fce4]()/nod..."分配给类型"(this: void, config: import("D:/work/test-web/node_modules/.pnpm/[vite@5.4.1]()9_@[types+node@24._c928461cc00da890efe4f1ccdfab0111]()/node_modules/vite/dist/node/index").UserConfig, env: import("D:/work/test-web/node_modules/.pnpm/[vite@5.4.1]()9_@[types+node@24._c928461cc00da890efe4f1ccdfab0111]()/nod..."。  
参数"config"和"config" 的类型不兼容。  
不能将类型"import("D:/work/test-web/node_modules/.pnpm/[vite@5.4.1]()9_@[types+node@24._c928461cc00da890efe4f1ccdfab0111]()/node_modules/vite/dist/node/index").UserConfig"分配给类型"import("D:/work/test-web/node_modules/.pnpm/[vite@6.3]().5_@[types+node@24.0_3221e9d8a7877bfd9756da51f493fce4]()/node_modules/vite/dist/node/index").UserConfig"。  
属性"plugins"的类型不兼容。  
不能将类型"import("D:/work/test-web/node_modules/.pnpm/[vite@5.4.1]()9_@[types+node@24._c928461cc00da890efe4f1ccdfab0111]()/node_modules/vite/dist/node/index").PluginOption[]"分配给类型"import("D:/work/test-web/node_modules/.pnpm/[vite@6.3]().5_@[types+node@24.0_3221e9d8a7877bfd9756da51f493fce4]()/node_modules/vite/dist/node/index").PluginOption[]"。  
不能将类型"import("D:/work/test-web/node_modules/.pnpm/[vite@5.4.1]()9_@[types+node@24._c928461cc00da890efe4f1ccdfab0111]()/node_modules/vite/dist/node/index").PluginOption"分配给类型"import("D:/work/test-web/node_modules/.pnpm/[vite@6.3]().5_@[types+node@24.0_3221e9d8a7877bfd9756da51f493fce4]()/node_modules/vite/dist/node/index").PluginOption"。  
不能将类型"Promise<false | Plugin<any> | PluginOption[]>"分配给类型"PluginOption"。  
不能将类型"Promise<false | import("D:/work/test-web/node_modules/.pnpm/[vite@5.4.1]()9_@[types+node@24._c928461cc00da890efe4f1ccdfab0111]()/node_modules/vite/dist/node/index").Plugin<any> | import("D:/work/test-web/node_modules/.pnpm/[vite@5.4.1]()9_@[types+node@24._c928461cc00da890efe4f1ccdfab0111]()/node_module..."分配给类型"Promise<false | import("D:/work/test-web/node_modules/.pnpm/[vite@6.3]().5_@[types+node@24.0_3221e9d8a7877bfd9756da51f493fce4]()/node_modules/vite/dist/node/index").Plugin<any> | import("D:/work/test-web/node_modules/.pnpm/[vite@6.3]().5_@[types+node@24.0_3221e9d8a7877bfd9756da51f493fce4]()/node_module..."。  
不能将类型"false | import("D:/work/test-web/node_modules/.pnpm/[vite@5.4.1]()9_@[types+node@24._c928461cc00da890efe4f1ccdfab0111]()/node_modules/vite/dist/node/index").Plugin<any> | import("D:/work/test-web/node_modules/.pnpm/[vite@5.4.1]()9_@[types+node@24._c928461cc00da890efe4f1ccdfab0111]()/node_modules/vite/d..."分配给类型"false | import("D:/work/test-web/node_modules/.pnpm/[vite@6.3]().5_@[types+node@24.0_3221e9d8a7877bfd9756da51f493fce4]()/node_modules/vite/dist/node/index").Plugin<any> | import("D:/work/test-web/node_modules/.pnpm/[vite@6.3]().5_@[types+node@24.0_3221e9d8a7877bfd9756da51f493fce4]()/node_modules/vite/d..."。  
不能将类型"Plugin<any>"分配给类型"false | Plugin<any> | PluginOption[]"。

原因

项目中出现了 vite 5.x 和 vite 6.x 不同的版本。

简单的查看一下,看是否可以做适当的升级,或者将 vite 统一提升到 root 层。 子包中 不使用 vite 依赖。

相关推荐
β添砖java11 分钟前
vivo响应式官网
前端·css·html·1024程序员节
web打印社区5 小时前
使用React如何静默打印页面:完整的前端打印解决方案
前端·javascript·vue.js·react.js·pdf·1024程序员节
喜欢踢足球的老罗5 小时前
[特殊字符] PM2 入门实战:从 0 到线上托管 React SPA
前端·react.js·前端框架
小光学长5 小时前
基于Vue的课程达成度分析系统t84pzgwk(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
前端·数据库·vue.js
Baklib梅梅7 小时前
探码科技再获“专精特新”认定:Baklib引领AI内容管理新方向
前端·ruby on rails·前端框架·ruby
南方以南_7 小时前
Chrome开发者工具
前端·chrome
YiHanXii7 小时前
this 输出题
前端·javascript·1024程序员节
楊无好7 小时前
React中ref
前端·react.js
程琬清君7 小时前
vue3 confirm倒计时
前端·1024程序员节
歪歪1007 小时前
在C#中详细介绍一下Visual Studio中如何使用数据可视化工具
开发语言·前端·c#·visual studio code·visual studio·1024程序员节