Browse Source

feat(env): 添加环境变量配置并优化 Vite 配置

- 新增 env.config.ts 文件,定义不同环境下的配置
- 更新 vite.config.ts,使用环境变量配置
- 修改 package.json,调整预览命令以使用开发环境
- 更新 tsconfig.node.json,包含 env.d.ts 文件
- 定义 EnvConfig 接口,规范环境变量配置
jx-test
lizw-2015 5 months ago
parent
commit
5889513eea
  1. 22
      env.config.ts
  2. 10
      env.d.ts
  3. 2
      package.json
  4. 3
      tsconfig.node.json
  5. 24
      vite.config.ts

22
env.config.ts

@ -0,0 +1,22 @@
import lodash from "lodash";
const defEnv: EnvConfig = {
serverHost: '0.0.0.0',
serverPort: 7791,
lccApiTarget: 'http://127.0.0.1:7779',
};
const allEnv: Record<string, Partial<EnvConfig>> = {
development: {},
production: {
lccApiTarget: 'http://127.0.0.1:8001',
serverPort: 3001,
},
};
for (const key in allEnv) {
const config = allEnv[key];
allEnv[key] = lodash.defaultsDeep(config, defEnv);
}
export default allEnv as Record<string, EnvConfig>;

10
env.d.ts

@ -1 +1,11 @@
/// <reference types="vite/client" /> /// <reference types="vite/client" />
/** 项目环境配置 */
interface EnvConfig {
/** vite 调试服务 bind host */
serverHost: string | boolean;
/** vite 调试端口 */
serverPort: number;
/** lcc api服务地址 */
lccApiTarget: string;
}

2
package.json

@ -7,7 +7,7 @@
"init": "pnpm i", "init": "pnpm i",
"dev": "vite", "dev": "vite",
"build": "vite build", "build": "vite build",
"preview": "vite preview", "preview": "vite preview -m development",
"build-check": "run-p type-check \"build-only {@}\" --", "build-check": "run-p type-check \"build-only {@}\" --",
"type-check": "vue-tsc --build", "type-check": "vue-tsc --build",
"format": "prettier --write src/" "format": "prettier --write src/"

3
tsconfig.node.json

@ -6,7 +6,8 @@
"cypress.config.*", "cypress.config.*",
"nightwatch.conf.*", "nightwatch.conf.*",
"playwright.config.*", "playwright.config.*",
"eslint.config.*" "eslint.config.*",
"env.d.ts"
], ],
"compilerOptions": { "compilerOptions": {
"noEmit": true, "noEmit": true,

24
vite.config.ts

@ -1,11 +1,17 @@
import { fileURLToPath, URL } from 'node:url' import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite' import { defineConfig, UserConfig } from 'vite'
import vue from '@vitejs/plugin-vue' import vue from '@vitejs/plugin-vue'
// import vueJsx from '@vitejs/plugin-vue-jsx' // import vueJsx from '@vitejs/plugin-vue-jsx'
// import vueDevTools from 'vite-plugin-vue-devtools' // import vueDevTools from 'vite-plugin-vue-devtools'
import allEnv from "./env.config";
export default defineConfig({ export default defineConfig(env => {
const isProduction = env.mode === 'production';
const isBuild = env.command === 'build';
const mode = env.mode;
const envConfig = allEnv[mode];
const config: UserConfig = {
plugins: [ plugins: [
vue(), vue(),
// vueJsx(), // vueJsx(),
@ -17,16 +23,22 @@ export default defineConfig({
} }
}, },
server: { server: {
host: '0.0.0.0', host: envConfig.serverHost,
port: 7791, port: envConfig.serverPort,
open: false, open: false,
proxy: { proxy: {
'^/api/.*': { '^/api/.*': {
target: "http://127.0.0.1:7779", target: envConfig.lccApiTarget,
changeOrigin: false, changeOrigin: false,
}, },
}, },
}, },
preview: {
host: envConfig.serverHost,
port: envConfig.serverPort,
strictPort: true,
allowedHosts: true,
},
optimizeDeps: { optimizeDeps: {
include: [ include: [
'lodash', 'axios', 'three', 'dat.gui', 'lodash', 'axios', 'three', 'dat.gui',
@ -154,4 +166,6 @@ export default defineConfig({
], ],
}, },
} }
};
return config;
}) })

Loading…
Cancel
Save