This is an automated email from the ASF dual-hosted git repository.

leezng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/inlong.git


The following commit(s) were added to refs/heads/master by this push:
     new 3b4646088 [INLONG-7327][Dashboard] Support using different env 
variable (#7329)
3b4646088 is described below

commit 3b4646088b11ed6c3f0c80e41a87ecf893817074
Author: Daniel <lee...@apache.org>
AuthorDate: Tue Feb 7 17:21:42 2023 +0800

    [INLONG-7327][Dashboard] Support using different env variable (#7329)
---
 inlong-dashboard/craco.config.ts                   | 10 ++++++
 inlong-dashboard/package-lock.json                 | 12 ++++++--
 inlong-dashboard/package.json                      |  1 +
 .../conf.ts => scripts/vite-plugin-resolve-env.ts} | 36 +++++++++++++++++-----
 inlong-dashboard/src/configs/default/conf.ts       |  4 ++-
 inlong-dashboard/src/configs/default/index.ts      |  4 ++-
 inlong-dashboard/src/configs/locales/conf.ts       |  4 +--
 inlong-dashboard/src/configs/menus/conf.ts         |  4 +--
 inlong-dashboard/src/configs/pagination/conf.ts    |  4 +--
 inlong-dashboard/src/configs/routes/conf.ts        |  4 +--
 inlong-dashboard/vite.config.ts                    |  7 +++++
 11 files changed, 69 insertions(+), 21 deletions(-)

diff --git a/inlong-dashboard/craco.config.ts b/inlong-dashboard/craco.config.ts
index 360ac5b9f..dddaa85e5 100644
--- a/inlong-dashboard/craco.config.ts
+++ b/inlong-dashboard/craco.config.ts
@@ -21,6 +21,9 @@ import CracoLess from 'craco-less';
 import { CracoAliasPlugin } from 'react-app-alias';
 import AntdDayjsWebpackPlugin from 'antd-dayjs-webpack-plugin';
 import type { CracoConfig } from 'craco__craco';
+import dotenv from 'dotenv';
+
+dotenv.config();
 
 const config: CracoConfig = {
   plugins: [
@@ -50,6 +53,13 @@ const config: CracoConfig = {
     plugins: {
       add: [new AntdDayjsWebpackPlugin()],
     },
+    configure: webpackConfig => {
+      if (webpackConfig.resolve?.extensions && process.env.INLONG_ENV) {
+        const tsIndex = webpackConfig.resolve.extensions.findIndex(item => 
item === '.ts');
+        webpackConfig.resolve.extensions.splice(tsIndex, 0, 
`.${process.env.INLONG_ENV}.ts`);
+      }
+      return webpackConfig;
+    },
   },
   babel: {
     plugins: [['import', { libraryName: 'antd', libraryDirectory: 'lib', 
style: true }]],
diff --git a/inlong-dashboard/package-lock.json 
b/inlong-dashboard/package-lock.json
index 6cbafa050..60ae9f553 100644
--- a/inlong-dashboard/package-lock.json
+++ b/inlong-dashboard/package-lock.json
@@ -5961,9 +5961,9 @@
       }
     },
     "dotenv": {
-      "version": "10.0.0",
-      "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-10.0.0.tgz";,
-      "integrity": 
"sha512-rlBi9d8jpv9Sf1klPjNfFAuWDjKLwTIJJ/VxtoTwIR6hnZxcEOQCZg2oIL3MWBYw5GpUDKOEnND7LXTbIpQ03Q==",
+      "version": "16.0.3",
+      "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.3.tgz";,
+      "integrity": 
"sha512-7GO6HghkA5fYG9TYnNxi14/7K9f5occMlp3zXAuSxn7CKCxt9xbNWG7yF8hTCSUchlfWSe3uLmlPfigevRItzQ==",
       "dev": true
     },
     "dotenv-expand": {
@@ -13182,6 +13182,12 @@
         "workbox-webpack-plugin": "^6.4.1"
       },
       "dependencies": {
+        "dotenv": {
+          "version": "10.0.0",
+          "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-10.0.0.tgz";,
+          "integrity": 
"sha512-rlBi9d8jpv9Sf1klPjNfFAuWDjKLwTIJJ/VxtoTwIR6hnZxcEOQCZg2oIL3MWBYw5GpUDKOEnND7LXTbIpQ03Q==",
+          "dev": true
+        },
         "react-refresh": {
           "version": "0.11.0",
           "resolved": 
"https://registry.npmjs.org/react-refresh/-/react-refresh-0.11.0.tgz";,
diff --git a/inlong-dashboard/package.json b/inlong-dashboard/package.json
index cfcfee779..19cfd24f5 100644
--- a/inlong-dashboard/package.json
+++ b/inlong-dashboard/package.json
@@ -76,6 +76,7 @@
     "antd-dayjs-webpack-plugin": "^1.0.6",
     "babel-plugin-import": "^1.13.5",
     "craco-less": "^2.1.0-alpha.0",
+    "dotenv": "^16.0.3",
     "eslint": "^8.22.0",
     "eslint-config-prettier": "^8.5.0",
     "eslint-plugin-prettier": "^4.2.1",
diff --git a/inlong-dashboard/src/configs/pagination/conf.ts 
b/inlong-dashboard/scripts/vite-plugin-resolve-env.ts
similarity index 53%
copy from inlong-dashboard/src/configs/pagination/conf.ts
copy to inlong-dashboard/scripts/vite-plugin-resolve-env.ts
index c1efd9d4e..f49e7132a 100644
--- a/inlong-dashboard/src/configs/pagination/conf.ts
+++ b/inlong-dashboard/scripts/vite-plugin-resolve-env.ts
@@ -17,15 +17,35 @@
  * under the License.
  */
 
-import i18n from '@/i18n';
+import { Plugin } from 'vite';
+import path from 'path';
+import fs from 'fs';
 
-const showTotal = (total: number) => i18n.t('configs.pagination.Total', { 
total });
+export type PluginOptions = {
+  env?: string;
+};
+
+const ResolveEnvPlugin = (options?: PluginOptions): Plugin => {
+  const env = options?.env;
+
+  return {
+    name: 'vite-plugin-resolve-env',
+    enforce: 'pre',
+    resolveId(id, importer) {
+      if (importer && env) {
+        const dir = path.dirname(importer);
+        const newId = path.resolve(dir, `${id}.${env}`);
 
-const paginationConf = {
-  showQuickJumper: true,
-  showSizeChanger: true,
-  pageSizeOptions: ['10', '20', '50', '100'],
-  showTotal,
+        if (newId.indexOf('/src/configs') === -1) return;
+        try {
+          fs.accessSync(`${newId}.ts`, fs.constants.R_OK);
+          return newId;
+        } catch (err) {
+          return;
+        }
+      }
+    },
+  };
 };
 
-export default paginationConf;
+export default ResolveEnvPlugin;
diff --git a/inlong-dashboard/src/configs/default/conf.ts 
b/inlong-dashboard/src/configs/default/conf.ts
index bfa2e8024..8fefba699 100644
--- a/inlong-dashboard/src/configs/default/conf.ts
+++ b/inlong-dashboard/src/configs/default/conf.ts
@@ -21,7 +21,7 @@ import i18n from '@/i18n';
 import Provider from '@/components/Provider';
 import Layout from '@/components/Layout';
 
-export const config = {
+const conf = {
   title: '',
   logo: '/logo.svg',
   loginUrl: `${window.location.origin}/#/${i18n?.language || ''}/login`,
@@ -29,3 +29,5 @@ export const config = {
   AppLoading: PageLoading,
   AppLayout: Layout,
 };
+
+export default conf;
diff --git a/inlong-dashboard/src/configs/default/index.ts 
b/inlong-dashboard/src/configs/default/index.ts
index bee53ba95..d664f2601 100644
--- a/inlong-dashboard/src/configs/default/index.ts
+++ b/inlong-dashboard/src/configs/default/index.ts
@@ -17,4 +17,6 @@
  * under the License.
  */
 
-export { config } from './conf';
+import conf from './conf';
+
+export const config = conf;
diff --git a/inlong-dashboard/src/configs/locales/conf.ts 
b/inlong-dashboard/src/configs/locales/conf.ts
index 494391caa..a804ff931 100644
--- a/inlong-dashboard/src/configs/locales/conf.ts
+++ b/inlong-dashboard/src/configs/locales/conf.ts
@@ -19,7 +19,7 @@
 
 import type { LocalesType } from '.';
 
-const localesConf: LocalesType = {
+const conf: LocalesType = {
   cn: {
     label: '简体中文',
     uiComponentPath: 'zh_CN',
@@ -32,4 +32,4 @@ const localesConf: LocalesType = {
   },
 };
 
-export default localesConf;
+export default conf;
diff --git a/inlong-dashboard/src/configs/menus/conf.ts 
b/inlong-dashboard/src/configs/menus/conf.ts
index 35349ce02..390a6ed01 100644
--- a/inlong-dashboard/src/configs/menus/conf.ts
+++ b/inlong-dashboard/src/configs/menus/conf.ts
@@ -20,7 +20,7 @@
 import i18n from '@/i18n';
 import type { MenuItemType } from '.';
 
-const menusTree: MenuItemType[] = [
+const conf: MenuItemType[] = [
   {
     path: '/group',
     name: i18n.t('configs.menus.Groups'),
@@ -66,4 +66,4 @@ const menusTree: MenuItemType[] = [
   },
 ];
 
-export default menusTree;
+export default conf;
diff --git a/inlong-dashboard/src/configs/pagination/conf.ts 
b/inlong-dashboard/src/configs/pagination/conf.ts
index c1efd9d4e..3de654e87 100644
--- a/inlong-dashboard/src/configs/pagination/conf.ts
+++ b/inlong-dashboard/src/configs/pagination/conf.ts
@@ -21,11 +21,11 @@ import i18n from '@/i18n';
 
 const showTotal = (total: number) => i18n.t('configs.pagination.Total', { 
total });
 
-const paginationConf = {
+const conf = {
   showQuickJumper: true,
   showSizeChanger: true,
   pageSizeOptions: ['10', '20', '50', '100'],
   showTotal,
 };
 
-export default paginationConf;
+export default conf;
diff --git a/inlong-dashboard/src/configs/routes/conf.ts 
b/inlong-dashboard/src/configs/routes/conf.ts
index 54fdd32d0..0b76a36f7 100644
--- a/inlong-dashboard/src/configs/routes/conf.ts
+++ b/inlong-dashboard/src/configs/routes/conf.ts
@@ -19,7 +19,7 @@
 
 import type { RouteProps } from '.';
 
-const routes: RouteProps[] = [
+const conf: RouteProps[] = [
   {
     path: '/login',
     component: () => import('@/pages/Login'),
@@ -102,4 +102,4 @@ const routes: RouteProps[] = [
   },
 ];
 
-export default routes;
+export default conf;
diff --git a/inlong-dashboard/vite.config.ts b/inlong-dashboard/vite.config.ts
index e6c18472a..a1a9339d0 100644
--- a/inlong-dashboard/vite.config.ts
+++ b/inlong-dashboard/vite.config.ts
@@ -25,6 +25,10 @@ import vitePluginImp from 'vite-plugin-imp';
 import dynamicImport from 'vite-plugin-dynamic-import';
 import svgr from 'vite-plugin-svgr';
 import eslintPlugin from 'vite-plugin-eslint';
+import ResolveEnvPlugin from './scripts/vite-plugin-resolve-env';
+import dotenv from 'dotenv';
+
+dotenv.config();
 
 export default defineConfig({
   resolve: {
@@ -43,6 +47,9 @@ export default defineConfig({
     },
   },
   plugins: [
+    ResolveEnvPlugin({
+      env: process.env.INLONG_ENV || undefined,
+    }),
     react(),
     tsConfigPaths(),
     vitePluginImp({

Reply via email to