Skip to content

Commit

Permalink
chore(projects): use json5 resolve env VITE_OTHER_SERVICE_BASE_URL & …
Browse files Browse the repository at this point in the history
…fix proxy enable
  • Loading branch information
honghuangdc committed Sep 8, 2024
1 parent 4679b17 commit f8b29c4
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 7 deletions.
6 changes: 3 additions & 3 deletions build/config/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { createServiceConfig } from '../../src/utils/service';
* Set http proxy
*
* @param env - The current env
* @param isDev - Is development environment
* @param enable - If enable http proxy
*/
export function createViteProxy(env: Env.ImportMeta, isDev: boolean) {
const isEnableHttpProxy = isDev && env.VITE_HTTP_PROXY === 'Y';
export function createViteProxy(env: Env.ImportMeta, enable: boolean) {
const isEnableHttpProxy = enable && env.VITE_HTTP_PROXY === 'Y';

if (!isEnableHttpProxy) return undefined;

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"clipboard": "2.0.11",
"dayjs": "1.11.13",
"echarts": "5.5.1",
"json5": "2.2.3",
"nprogress": "0.2.0",
"pinia": "2.2.2",
"tailwind-merge": "2.5.2",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions src/utils/service.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json5 from 'json5';

/**
* Create service config by current env
*
Expand All @@ -8,10 +10,10 @@ export function createServiceConfig(env: Env.ImportMeta) {

let other = {} as Record<App.Service.OtherBaseURLKey, string>;
try {
other = JSON.parse(VITE_OTHER_SERVICE_BASE_URL);
} catch (error) {
other = json5.parse(VITE_OTHER_SERVICE_BASE_URL);
} catch {
// eslint-disable-next-line no-console
console.error('VITE_OTHER_SERVICE_BASE_URL is not a valid JSON string');
console.error('VITE_OTHER_SERVICE_BASE_URL is not a valid json5 string');
}

const httpConfig: App.Service.SimpleServiceConfig = {
Expand Down
4 changes: 3 additions & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export default defineConfig(configEnv => {

const buildTime = getBuildTime();

const enableProxy = configEnv.command === 'serve' && !configEnv.isPreview;

return {
base: viteEnv.VITE_BASE_URL,
resolve: {
Expand All @@ -32,7 +34,7 @@ export default defineConfig(configEnv => {
host: '0.0.0.0',
port: 9527,
open: true,
proxy: createViteProxy(viteEnv, configEnv.command === 'serve'),
proxy: createViteProxy(viteEnv, enableProxy),
fs: {
cachedChecks: false
}
Expand Down

0 comments on commit f8b29c4

Please sign in to comment.