Skip to content

Commit 90db929

Browse files
committed
引入 vite-plugin-env-parse
1 parent 364b025 commit 90db929

File tree

5 files changed

+50
-4
lines changed

5 files changed

+50
-4
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
"vite-plugin-archiver": "^0.1.1",
9292
"vite-plugin-banner": "^0.8.0",
9393
"vite-plugin-compression2": "^1.3.3",
94+
"vite-plugin-env-parse": "^1.0.15",
9495
"vite-plugin-fake-server": "^2.2.0",
9596
"vite-plugin-pages": "^0.32.4",
9697
"vite-plugin-svg-icons": "^2.0.1",

pnpm-lock.yaml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/api/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import axios from 'axios'
55
import Message from 'vue-m-message'
66

77
const api = axios.create({
8-
baseURL: (import.meta.env.DEV && import.meta.env.VITE_OPEN_PROXY === 'true') ? '/proxy/' : import.meta.env.VITE_APP_API_BASEURL,
8+
baseURL: (import.meta.env.DEV && import.meta.env.VITE_OPEN_PROXY) ? '/proxy/' : import.meta.env.VITE_APP_API_BASEURL,
99
timeout: 1000 * 60,
1010
responseType: 'json',
1111
})

src/types/env.d.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/// <reference types="vite/client" />
2+
interface ImportMetaEnv {
3+
// Auto generate by env-parse
4+
/**
5+
* 应用配置面板
6+
*/
7+
readonly VITE_APP_SETTING: boolean
8+
/**
9+
* 页面标题
10+
*/
11+
readonly VITE_APP_TITLE: string
12+
/**
13+
* 接口请求地址,会设置到 axios 的 baseURL 参数上
14+
*/
15+
readonly VITE_APP_API_BASEURL: string
16+
/**
17+
* 调试工具,可设置 eruda 或 vconsole,如果不需要开启则留空
18+
*/
19+
readonly VITE_APP_DEBUG_TOOL: string
20+
/**
21+
* 是否开启代理
22+
*/
23+
readonly VITE_OPEN_PROXY: boolean
24+
/**
25+
* 是否开启开发者工具
26+
*/
27+
readonly VITE_OPEN_DEVTOOLS: boolean
28+
}

vite/plugins.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ import { loadEnv } from 'vite'
1616
import Archiver from 'vite-plugin-archiver'
1717
import banner from 'vite-plugin-banner'
1818
import { compression } from 'vite-plugin-compression2'
19+
import { envParse, parseLoadedEnv } from 'vite-plugin-env-parse'
1920
import { vitePluginFakeServer } from 'vite-plugin-fake-server'
2021
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
2122
import VueDevTools from 'vite-plugin-vue-devtools'
2223

2324
export default function createVitePlugins(mode: string, isBuild = false) {
24-
const viteEnv = loadEnv(mode, process.cwd())
25+
const viteEnv = parseLoadedEnv(loadEnv(mode, process.cwd()))
2526
const vitePlugins: (PluginOption | PluginOption[])[] = [
2627
VueRouter({
2728
routesFolder: './src/views',
@@ -38,7 +39,11 @@ export default function createVitePlugins(mode: string, isBuild = false) {
3839
}),
3940

4041
// https://github.com/vuejs/devtools-next
41-
viteEnv.VITE_OPEN_DEVTOOLS === 'true' && VueDevTools(),
42+
viteEnv.VITE_OPEN_DEVTOOLS && VueDevTools(),
43+
44+
envParse({
45+
dtsPath: 'src/types/env.d.ts',
46+
}),
4247

4348
// https://github.com/unplugin/unplugin-auto-import
4449
autoImport({
@@ -81,7 +86,7 @@ export default function createVitePlugins(mode: string, isBuild = false) {
8186
logger: !isBuild,
8287
include: 'src/mock',
8388
infixName: false,
84-
enableProd: isBuild && viteEnv.VITE_BUILD_MOCK === 'true',
89+
enableProd: isBuild && viteEnv.VITE_BUILD_MOCK,
8590
}),
8691

8792
// https://github.com/nonzzz/vite-plugin-compression

0 commit comments

Comments
 (0)