Skip to content

Commit efbe9cb

Browse files
committed
feat(core): add runtime config
1 parent 11a7cc0 commit efbe9cb

File tree

5 files changed

+11
-7
lines changed

5 files changed

+11
-7
lines changed

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
77
<meta name="color-scheme" content="light dark" />
88
<title>%VITE_APP_TITLE%</title>
9+
<script src="/runtime.config.js"></script>
910
</head>
1011
<body>
1112
<div id="app"></div>

public/runtime.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
window.config = {
22
VITE_BUILD_PREFIX: '',
3+
VITE_SERVICE_BASE_URL: '',
4+
VITE_OTHER_SERVICE_BASE_URL: '',
35
}

src/locales/langs/en-us.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ const local: App.I18n.Schema = {
221221
projectNews: {
222222
title: 'Project News',
223223
moreNews: 'More News',
224-
desc1: 'Loic Duong created the open source project vue-naive-admin on May 28, 2021!',
224+
desc1: 'Loic Duong created the open source project vue-naive-admin on May 28, 2024!',
225225
desc2: 'Loic Duong submitted a bug to vue-naive-admin, the multi-tab bar will not adapt.',
226226
desc3: 'Loic Duong is ready to do sufficient preparation for the release of vue-naive-admin!',
227227
desc4: 'Loic Duong is busy writing project documentation for vue-naive-admin!',

src/utils/env.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
export function getEnvVariable(name: Env.Runtime, env?: Env.ImportMeta): CommonType.NullableString {
2-
return window.config?.[name] || (env ? env[name] : import.meta.env[name])
1+
export function getEnvVariable(name: Env.Runtime, env?: Env.ImportMeta): string {
2+
return typeof window === 'undefined'
3+
? env?.[name] || import.meta.env[name]
4+
: window.config?.[name] || env?.[name] || import.meta.env[name]
35
}

src/utils/service.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
import json5 from 'json5'
2+
import { getEnvVariable } from './env'
23

34
/**
45
* Create service config by current env
56
*
67
* @param env The current env
78
*/
89
export function createServiceConfig(env: Env.ImportMeta) {
9-
const { VITE_SERVICE_BASE_URL, VITE_OTHER_SERVICE_BASE_URL } = env
10-
1110
let other = {} as Record<App.Service.OtherBaseURLKey, string>
1211
try {
13-
other = json5.parse(VITE_OTHER_SERVICE_BASE_URL)
12+
other = json5.parse(getEnvVariable('VITE_OTHER_SERVICE_BASE_URL', env))
1413
}
1514
catch {
1615
console.error('VITE_OTHER_SERVICE_BASE_URL is not a valid json5 string')
1716
}
1817

1918
const httpConfig: App.Service.SimpleServiceConfig = {
20-
baseURL: VITE_SERVICE_BASE_URL,
19+
baseURL: getEnvVariable('VITE_SERVICE_BASE_URL', env),
2120
other,
2221
}
2322

0 commit comments

Comments
 (0)