From 06de43da395972a2c182a1f1dddad9fe0707bfb6 Mon Sep 17 00:00:00 2001 From: chenos Date: Sun, 17 Apr 2022 17:14:50 +0800 Subject: [PATCH] fix: getUmiConfig --- packages/app/client/.umirc.ts | 6 +-- .../files/packages/app/client/.umirc.ts | 2 +- packages/core/utils/umiConfig.d.ts | 17 +++++++++ packages/core/utils/umiConfig.js | 38 +++++++++++++++++++ 4 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 packages/core/utils/umiConfig.d.ts create mode 100644 packages/core/utils/umiConfig.js diff --git a/packages/app/client/.umirc.ts b/packages/app/client/.umirc.ts index 43b9fd055..5c58693ad 100644 --- a/packages/app/client/.umirc.ts +++ b/packages/app/client/.umirc.ts @@ -1,7 +1,9 @@ +import { getUmiConfig } from '@nocobase/utils/umiConfig'; import dotenv from 'dotenv'; import { resolve } from 'path'; import { defineConfig } from 'umi'; -import { getUmiConfig } from '../../core/utils/src/umiConfig'; + +const umiConfig = getUmiConfig(); dotenv.config({ path: resolve(__dirname, '../../../.env'), @@ -9,8 +11,6 @@ dotenv.config({ process.env.MFSU_AD = 'none'; -const umiConfig = getUmiConfig(); - export default defineConfig({ hash: true, define: { diff --git a/packages/core/create-nocobase-app/lib/resources/files/packages/app/client/.umirc.ts b/packages/core/create-nocobase-app/lib/resources/files/packages/app/client/.umirc.ts index 023182d4a..9149d5927 100644 --- a/packages/core/create-nocobase-app/lib/resources/files/packages/app/client/.umirc.ts +++ b/packages/core/create-nocobase-app/lib/resources/files/packages/app/client/.umirc.ts @@ -1,4 +1,4 @@ -import { getUmiConfig } from '@nocobase/utils'; +import { getUmiConfig } from '@nocobase/utils/umiConfig'; import dotenv from 'dotenv'; import { resolve } from 'path'; import { defineConfig } from 'umi'; diff --git a/packages/core/utils/umiConfig.d.ts b/packages/core/utils/umiConfig.d.ts new file mode 100644 index 000000000..f08337e72 --- /dev/null +++ b/packages/core/utils/umiConfig.d.ts @@ -0,0 +1,17 @@ +export declare function getUmiConfig(): { + define: { + 'process.env.SERVER_BASE_URL': string; + }; + proxy: { + [x: string]: { + target: string; + changeOrigin: boolean; + } | { + target: string; + changeOrigin: boolean; + pathRewrite: { + [x: string]: string; + }; + }; + }; +}; diff --git a/packages/core/utils/umiConfig.js b/packages/core/utils/umiConfig.js new file mode 100644 index 000000000..749ebb3c0 --- /dev/null +++ b/packages/core/utils/umiConfig.js @@ -0,0 +1,38 @@ +function getUmiConfig() { + const { SERVER_PORT, SERVER_BASE_URL } = process.env; + const SERVER_BASE_PATH = process.env.SERVER_BASE_PATH || '/api/'; + const PROXY_TARGET_URL = process.env.PROXY_TARGET_URL || `http://127.0.0.1:${SERVER_PORT}`; + const LOCAL_STORAGE_BASE_URL = process.env.LOCAL_STORAGE_BASE_URL || '/uploads'; + + function getLocalStorageProxy() { + if (LOCAL_STORAGE_BASE_URL.startsWith('http')) { + return {}; + } + + return { + [LOCAL_STORAGE_BASE_URL]: { + target: PROXY_TARGET_URL, + changeOrigin: true, + }, + }; + } + + return { + define: { + 'process.env.SERVER_BASE_URL': SERVER_BASE_URL || SERVER_BASE_PATH, + }, + // only proxy when using `umi dev` + // if the assets are built, will not proxy + proxy: { + [SERVER_BASE_PATH]: { + target: PROXY_TARGET_URL, + changeOrigin: true, + pathRewrite: { [`^${SERVER_BASE_PATH}`]: SERVER_BASE_PATH }, + }, + // for local storage + ...getLocalStorageProxy(), + }, + }; +} + +exports.getUmiConfig = getUmiConfig;