diff --git a/.env b/.env new file mode 100644 index 0000000000000000000000000000000000000000..40921d8de361e7fe9c9bd23e35ae2a22e6fddfa9 --- /dev/null +++ b/.env @@ -0,0 +1,8 @@ +# port +VITE_PORT = 16801 + +# Automatically open browser when executing 'npm run dev' +VITE_OPEN = false + +# Configure the online environment path +VITE_PUBLIC_PATH = /dsms-ui/ diff --git a/.env.development b/.env.development new file mode 100644 index 0000000000000000000000000000000000000000..c8acfc075800158e8d5faea3e5245f45a2f0a0a5 --- /dev/null +++ b/.env.development @@ -0,0 +1,2 @@ +# public path +VITE_PUBLIC_PATH = 'http://localhost:xxxx/' diff --git a/.env.production b/.env.production new file mode 100644 index 0000000000000000000000000000000000000000..83d35c7aa98c6def9708549986c37a3347a2ef5a --- /dev/null +++ b/.env.production @@ -0,0 +1,2 @@ +# public path +VITE_PUBLIC_PATH = /https://xxx.xxx.xxx:xxxx/ diff --git a/build/utils.ts b/build/utils.ts new file mode 100644 index 0000000000000000000000000000000000000000..a7030280e0ad5c73341fb105e4c0333ba25516dc --- /dev/null +++ b/build/utils.ts @@ -0,0 +1,31 @@ +import dotenv from 'dotenv'; + +export interface ViteEnv { + VITE_PORT: number; + VITE_OPEN: boolean; + VITE_PUBLIC_PATH: string; +} + +export function loadEnv(): ViteEnv { + const env = process.env.NODE_ENV; + const ret: any = {}; + const envList = [`.env.${env}.local`, `.env.${env}`, '.env.local', '.env', ,] + envList.forEach((e) => { + dotenv.config({ + path: e, + }); + }); + for (const envName of Object.keys(process.env)) { + let realName = (process.env as any)[envName].replace(/\\n/g, '\n'); + realName = realName === 'true' ? true : realName === 'false' ? false : realName; + if (envName === 'VITE_PORT') { + realName = Number(realName); + } + if (envName === 'VITE_OPEN') { + realName = Boolean(realName); + } + ret[envName] = realName; + process.env[envName] = realName; + } + return ret; +} \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index f530a2de5cf2c9ef5f4d2bb0f0dc726a7b7b3de9..4063e31709a83b15ba7e8dabbe05d0bebe6311dd 100644 --- a/src/main.ts +++ b/src/main.ts @@ -2,7 +2,7 @@ import { createApp } from 'vue' import App from './App.vue' import ElementPlus from 'element-plus'; -import '/@assets/style/base/index.scss'; +import '/@/assets/style/base/index.scss'; // import 'element-plus/lib/theme-chalk/index.css' const app = createApp(App) diff --git a/vite.config.ts b/vite.config.ts index a45c4945d28761ec160444acf61d4d60f3b17fc4..d4f4cdcf5f2ea4402a14219ebecd076a4f7c03bc 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,17 +1,27 @@ import type { UserConfig } from 'vite' -const path = require('path') +import { resolve } from 'path' +import { loadEnv } from './build/utils' + +const pathResolve = (dir: string): any => { + return resolve(__dirname, '.', dir) +} + +const alias: Record = { + '/@/': pathResolve('src'), +} + +const { VITE_PORT, VITE_PUBLIC_PATH, VITE_OPEN } = loadEnv() + +const root: string = process.cwd() const viteConfig: UserConfig = { - port: 16801, - hostname: '0.0.0.0', - open: false, - alias: { - '/@/': path.resolve(__dirname, './src'), - '/@assets/': path.resolve(__dirname, './src/assets'), - '/@views/': path.resolve(__dirname, './src/views'), - '/@components/': path.resolve(__dirname, './src/components'), - '/@utils/': path.resolve(__dirname, './src/utils') - } + root, + alias, + outDir: 'dist', + minify: 'esbuild', + port: VITE_PORT, + open: VITE_OPEN, + base: process.env.NODE_ENV === "production" ? "./" : VITE_PUBLIC_PATH, } export default viteConfig