ykt-team-wxapp/vite.config.js
2026-01-20 19:36:49 +08:00

40 lines
1.2 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.

import {
loadEnv
} from "vite";
import uni from "@dcloudio/vite-plugin-uni";
export default (command, mode) => {
//uniapp中这个mode,不用命令行的添加--mode编译的话是undefined的
if (!mode) {
//这里是为了确保mode有值假如开发人员直接在开发工具选择编译到浏览器也能兼容到
if (process.env.NODE_ENV == "production") {
mode = "production";
} else {
mode = "development";
}
}
//这个就是第三步配置的uni-app的script,一一对应判断
switch (process.env.UNI_SCRIPT) {
case "dev":
mode = "development";
break;
case "localhost":
mode = "localhost";
break;
default:
mode = "production";
break;
}
const env = loadEnv(mode, __dirname, "MP_");
console.log('mode: ', mode);
console.log(process.env.UNI_SCRIPT)
console.log(`📦 正在编译 ${mode} 环境`);
return {
envPrefix: "MP_",
plugins: [uni()],
define: {
//根据vite的官方文档可以把define定义的变量名在项目编译时识别项目文件中的这个变量名直接替换成env配置我们把这个变量放到config.js文件中对env配置进行集中管理
__VITE_ENV__: JSON.stringify(env),
},
};
};