From d9e0d8f77f29c00b14925f8398bc526682c9dd38 Mon Sep 17 00:00:00 2001
From: Ben Lin <maobin001@msn.com>
Date: 星期二, 23 七月 2024 16:56:48 +0800
Subject: [PATCH] 增加系统配置项VITE_GLOB_SYS_TYPE, WMS/MES

---
 internal/vite-config/src/config/application.ts |  109 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 109 insertions(+), 0 deletions(-)

diff --git a/internal/vite-config/src/config/application.ts b/internal/vite-config/src/config/application.ts
new file mode 100644
index 0000000..7ee5ef1
--- /dev/null
+++ b/internal/vite-config/src/config/application.ts
@@ -0,0 +1,109 @@
+import { resolve } from 'node:path';
+
+import dayjs from 'dayjs';
+import { readPackageJSON } from 'pkg-types';
+import { defineConfig, loadEnv, mergeConfig, type UserConfig } from 'vite';
+
+import { createPlugins } from '../plugins';
+import { generateModifyVars } from '../utils/modifyVars';
+import { commonConfig } from './common';
+
+interface DefineOptions {
+  overrides?: UserConfig;
+  options?: {
+    //
+  };
+}
+
+function defineApplicationConfig(defineOptions: DefineOptions = {}) {
+  const { overrides = {} } = defineOptions;
+
+  return defineConfig(async ({ command, mode }) => {
+    const root = process.cwd();
+    const isBuild = command === 'build';
+    const { VITE_PUBLIC_PATH, VITE_USE_MOCK, VITE_BUILD_COMPRESS, VITE_ENABLE_ANALYZE } = loadEnv(
+      mode,
+      root,
+    );
+
+    const defineData = await createDefineData(root);
+    const plugins = await createPlugins({
+      isBuild,
+      root,
+      enableAnalyze: VITE_ENABLE_ANALYZE === 'true',
+      enableMock: VITE_USE_MOCK === 'true',
+      compress: VITE_BUILD_COMPRESS,
+    });
+
+    const pathResolve = (pathname: string) => resolve(root, '.', pathname);
+
+    const applicationConfig: UserConfig = {
+      base: VITE_PUBLIC_PATH,
+      resolve: {
+        alias: [
+          {
+            find: 'vue-i18n',
+            replacement: 'vue-i18n/dist/vue-i18n.cjs.js',
+          },
+          // @/xxxx => src/xxxx
+          {
+            find: /@\//,
+            replacement: pathResolve('src') + '/',
+          },
+          // #/xxxx => types/xxxx
+          {
+            find: /#\//,
+            replacement: pathResolve('types') + '/',
+          },
+        ],
+      },
+      define: defineData,
+      build: {
+        target: 'es2015',
+        cssTarget: 'chrome80',
+        rollupOptions: {
+          output: {
+            // 鍏ュ彛鏂囦欢鍚�
+            entryFileNames: 'assets/entry/[name]-[hash].js',
+            manualChunks: {
+              vue: ['vue', 'pinia', 'vue-router'],
+              antd: ['ant-design-vue', '@ant-design/icons-vue'],
+            },
+          },
+        },
+      },
+      css: {
+        preprocessorOptions: {
+          less: {
+            modifyVars: generateModifyVars(),
+            javascriptEnabled: true,
+          },
+        },
+      },
+      plugins,
+    };
+
+    const mergedConfig = mergeConfig(commonConfig(mode), applicationConfig);
+
+    return mergeConfig(mergedConfig, overrides);
+  });
+}
+
+async function createDefineData(root: string) {
+  try {
+    const pkgJson = await readPackageJSON(root);
+    const { dependencies, devDependencies, name, version } = pkgJson;
+
+    const __APP_INFO__ = {
+      pkg: { dependencies, devDependencies, name, version },
+      lastBuildTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
+    };
+    return {
+      __APP_INFO__: JSON.stringify(__APP_INFO__),
+    };
+  } catch (error) {
+    return {};
+  }
+}
+
+export { defineApplicationConfig };

--
Gitblit v1.9.3