-
-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
这里面如何区分dev和build环境 #32
Comments
vite的环境变量 |
试了下直接不能运行,import.meta.env.DEV应该是在运行时里面获取的,manifest.config.ts好像拿不到 |
麻烦再试下 |
process.env.NODE_ENV虽然是可以区分出来是否是正式环境,但是好像实际打包后并不生效,认的是编译后的/src/manifest.json里面的base路径 |
我自己测了一下,发现 你可以连续运行两遍就会发现, |
我用官方的模板测试了下,发现存在一样的问题。 根据uniapp的文档https://uniapp.dcloud.net.cn/collocation/vite-config.html,
// vite.config.js
import { defineConfig } from "vite";
import uni from "@dcloudio/vite-plugin-uni";
import fs from "node:fs";
import path from "node:path";
import JSON5 from "json5";
const manifestPath = path.resolve(__dirname, "src/manifest.json");
const manifestContent = fs.readFileSync(manifestPath, "utf8");
try {
const manifestJson = JSON5.parse(manifestContent);
manifestJson.h5 = {
...manifestJson.h5,
router: {
...manifestJson.h5?.router,
base: process.env.NODE_ENV === "production" ? "/prod" : "/dev",
},
};
fs.writeFileSync(manifestPath, JSON.stringify(manifestJson, null, 2), "utf8");
} catch (error) {
console.error("Failed to parse manifest.json");
}
// https://vitejs.dev/config/
export default defineConfig({
plugins: [uni()],
}); 这个应该是uni的问题。 |
我能想到的解决方案是在执行 // scripts/manifest.ts
import fs from 'node:fs'
import path from 'node:path'
import manifestConfig from '../manifest.config'
fs.writeFileSync(
path.resolve(__dirname, '../src/manifest.json'),
JSON.stringify(manifestConfig, null, 2),
) // package.json
{
"scripts": {
"dev": "tsx scripts/manifest.ts && uni",
"build": "cross-env NODE_ENV=production tsx scripts/manifest.ts && uni build",
}
} |
可以打印啊 确实有两遍的问题,所以现在编译发布都必须编译2次,不然env配置不生效 |
@wtto00 要是配置方式可以改为像 vite 一样支持函数式定义就好了,可以拿到当前 build mode 然后载入不同的 env 文件实现不同的配置项 |
uni 在 vite 启动前就依赖 manifest.json,这个插件能做到的很有限,除非找到或 uni 提供 cli 级别的钩子
|
对问题的清晰和简明的描述
比如h5里面的base路径,假设我要区分dev和build,这时候就改如何区分
推荐的解决方案
可以增加一个变量用来判断当前是dev还是build
替代方案
No response
额外上下文
No response
检查
The text was updated successfully, but these errors were encountered: