ykt-wxapp/vite.config.js
2026-02-09 18:05:46 +08:00

43 lines
1.3 KiB
JavaScript
Raw Permalink 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;
case "ip":
mode = "ip";
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),
},
};
};