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 Aug 14, 2024
1 parent 26c93df commit b16a963
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 6 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 @@ -85,6 +85,7 @@
"@vitejs/plugin-vue-jsx": "4.0.0",
"eslint": "9.8.0",
"eslint-plugin-vue": "9.27.0",
"json5": "2.2.3",
"lint-staged": "15.2.7",
"sass": "1.77.8",
"simple-git-hooks": "2.11.1",
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.

6 changes: 4 additions & 2 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);
other = json5.parse(VITE_OTHER_SERVICE_BASE_URL);
} catch (error) {
// 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 b16a963

Please sign in to comment.