Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): add runtime config #35

Merged
merged 1 commit into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading