罗广辉
2025-10-21 6f9be933391c2877bf41290635a07b9857e0e78c
feat: 打包环境调整
7 files modified
29 ■■■■■ changed files
build/config/proxy.js 8 ●●●●● patch | view | raw | blame | history
build/plugins/index.js 1 ●●●● patch | view | raw | blame | history
env/.env.production 2 ●●● patch | view | raw | blame | history
src/config/env.js 2 ●●● patch | view | raw | blame | history
src/main.js 1 ●●●● patch | view | raw | blame | history
src/utils/common/index.js 6 ●●●●● patch | view | raw | blame | history
vite.config.js 9 ●●●● patch | view | raw | blame | history
build/config/proxy.js
@@ -8,13 +8,15 @@
 * Copyright 2025 OBKoro1, All Rights Reserved.
 * 2025-09-28 09:31:16
 */
export const createViteProxy = (env) => {
  const { VITE_APP_PROXY, VITE_API_PREFIX, VITE_API_BASE_URL } = env
import configEnv from "../../src/config/env.js";
export const createViteProxy = (env,envName) => {
  const { VITE_APP_PROXY, VITE_API_PREFIX } = env
  // 不使用代理直接返回
  if (!JSON.parse(VITE_APP_PROXY)) return undefined
  const proxy = {
    [VITE_API_PREFIX]: {
      target: VITE_API_BASE_URL,
      target: configEnv[envName].VITE_API_BASE_URL,
      changeOrigin: true,
      rewrite: path => path.replace(new RegExp(`^${VITE_API_PREFIX}`), ''),
    },
build/plugins/index.js
@@ -6,7 +6,6 @@
import { AutoRegistryComponents } from './component'
import { MCPPlugin } from './mcp'
import { ConfigUnoCSSPlugin } from './unocss'
export default function createVitePlugins (isBuild) {
  const vitePlugins = [
    // UnoCSS配置
env/.env.production
@@ -2,4 +2,4 @@
VITE_APP_ENV=production
# 删除console
VITE_DROP_CONSOLE=true
VITE_DROP_CONSOLE=false
src/config/env.js
@@ -8,7 +8,7 @@
}
const test = {
  VITE_APP_ENV:'development',
  VITE_APP_ENV:'test',
  VITE_APP_WEBVIEW_URL: 'https://wrj.shuixiongit.com/drone-app-web-view/#/webViewWrapper',
  VITE_API_BASE_URL: 'https://wrj.shuixiongit.com/api',
}
src/main.js
@@ -16,7 +16,6 @@
import 'leaflet/dist/leaflet.css'
import {getEnvObj} from "@/utils/index.js";
console.log('process.env.ENV_NAME',)
console.log('当前环境变量',getEnvObj())
export function createApp () {
  const app = createSSRApp(App)
src/utils/common/index.js
@@ -11,7 +11,7 @@
// 小程序更新检测
import { useUserStore } from "@/store/index.js"
import configEnv from "@/config/env.js";
import process from "node:process"
import process from "node:process";
export function mpUpdate () {
  const updateManager = uni.getUpdateManager()
@@ -42,11 +42,13 @@
}
export function getEnvObj() {
  return configEnv?.[process?.env?.ENV_NAME] || {}
  const envName = JSON.parse(__APP_ENV__.UNI_CUSTOM_DEFINE)?.ENV_NAME
  return configEnv[envName] || {}
}
export function getWebViewUrl (targetUrl, otherParams) {
  const userStore = useUserStore()
  const url = getEnvObj().VITE_APP_WEBVIEW_URL
  console.log(getEnvObj(),'getEnvObj()')
  // 1. 处理用户参数
  const userParams = userStore?.userInfo ? JSON.stringify(userStore.userInfo) : '{}'
  // 2. 构建查询参数字符串
vite.config.js
@@ -3,6 +3,7 @@
import { defineConfig, loadEnv } from "vite"
import { createViteProxy } from "./build/config/index"
import createVitePlugins from "./build/plugins/index"
import configEnv from "./src/config/env.js";
// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
@@ -10,13 +11,17 @@
  console.log("command, mode -> ", command, mode)
  const { UNI_PLATFORM,UNI_CUSTOM_DEFINE } = process.env
  const ENV_NAME = UNI_CUSTOM_DEFINE && JSON.parse(UNI_CUSTOM_DEFINE)?.ENV_NAME
  console.log("平台 -> ", UNI_PLATFORM) // 得到 mp-weixin, h5, app 等
  console.log("环境 -> ", UNI_CUSTOM_DEFINE && JSON.parse(UNI_CUSTOM_DEFINE)?.ENV_NAME) // 得到 development,test 等
  console.log("环境 -> ", ENV_NAME) // 得到 development,test 等
  const env = loadEnv(mode, fileURLToPath(new URL("./env", import.meta.url)))
  // console.log("环境变量 env -> ", env)
  const isBuild = process.env.NODE_ENV === "production"
  return {
    define: {
      __APP_ENV__: process.env,
    },
    // 自定义env目录
    envDir: "./env",
    resolve: {
@@ -32,7 +37,7 @@
      hmr: true,
      host: true,
      open: true,
      proxy: createViteProxy(env)
      proxy: createViteProxy(env,ENV_NAME)
    },
    // 设置scss的api类型为modern-compiler
    css: {