Skip to content

Commit

Permalink
feat(core): add runtime config
Browse files Browse the repository at this point in the history
  • Loading branch information
loicduong committed Sep 8, 2024
1 parent 11a7cc0 commit efbe9cb
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 7 deletions.
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="color-scheme" content="light dark" />
<title>%VITE_APP_TITLE%</title>
<script src="/runtime.config.js"></script>
</head>
<body>
<div id="app"></div>
Expand Down
2 changes: 2 additions & 0 deletions public/runtime.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
window.config = {
VITE_BUILD_PREFIX: '',
VITE_SERVICE_BASE_URL: '',
VITE_OTHER_SERVICE_BASE_URL: '',
}
2 changes: 1 addition & 1 deletion src/locales/langs/en-us.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ const local: App.I18n.Schema = {
projectNews: {
title: 'Project News',
moreNews: 'More News',
desc1: 'Loic Duong created the open source project vue-naive-admin on May 28, 2021!',
desc1: 'Loic Duong created the open source project vue-naive-admin on May 28, 2024!',
desc2: 'Loic Duong submitted a bug to vue-naive-admin, the multi-tab bar will not adapt.',
desc3: 'Loic Duong is ready to do sufficient preparation for the release of vue-naive-admin!',
desc4: 'Loic Duong is busy writing project documentation for vue-naive-admin!',
Expand Down
6 changes: 4 additions & 2 deletions src/utils/env.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export function getEnvVariable(name: Env.Runtime, env?: Env.ImportMeta): CommonType.NullableString {
return window.config?.[name] || (env ? env[name] : import.meta.env[name])
export function getEnvVariable(name: Env.Runtime, env?: Env.ImportMeta): string {
return typeof window === 'undefined'
? env?.[name] || import.meta.env[name]
: window.config?.[name] || env?.[name] || import.meta.env[name]
}
7 changes: 3 additions & 4 deletions src/utils/service.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import json5 from 'json5'
import { getEnvVariable } from './env'

/**
* Create service config by current env
*
* @param env The current env
*/
export function createServiceConfig(env: Env.ImportMeta) {
const { VITE_SERVICE_BASE_URL, VITE_OTHER_SERVICE_BASE_URL } = env

let other = {} as Record<App.Service.OtherBaseURLKey, string>
try {
other = json5.parse(VITE_OTHER_SERVICE_BASE_URL)
other = json5.parse(getEnvVariable('VITE_OTHER_SERVICE_BASE_URL', env))
}
catch {
console.error('VITE_OTHER_SERVICE_BASE_URL is not a valid json5 string')
}

const httpConfig: App.Service.SimpleServiceConfig = {
baseURL: VITE_SERVICE_BASE_URL,
baseURL: getEnvVariable('VITE_SERVICE_BASE_URL', env),
other,
}

Expand Down

0 comments on commit efbe9cb

Please sign in to comment.