From c5ec2bede55b3584d0aad18c31c8fec5b4dfffef Mon Sep 17 00:00:00 2001 From: SaarHV Date: Wed, 30 Nov 2022 17:05:27 +0800 Subject: [PATCH] Update to use configuration to compile and run this repo --- .env | 8 ++++++++ .env.development | 2 ++ .env.production | 2 ++ build/utils.ts | 31 +++++++++++++++++++++++++++++++ src/main.ts | 2 +- vite.config.ts | 32 +++++++++++++++++++++----------- 6 files changed, 65 insertions(+), 12 deletions(-) create mode 100644 .env create mode 100644 .env.development create mode 100644 .env.production create mode 100644 build/utils.ts diff --git a/.env b/.env new file mode 100644 index 0000000..40921d8 --- /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 0000000..c8acfc0 --- /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 0000000..83d35c7 --- /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 0000000..a703028 --- /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 f530a2d..4063e31 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 a45c494..d4f4cdc 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 -- Gitee