diff --git a/vue-admin-wonderful-next/.env.development b/vue-admin-wonderful-next/.env.development new file mode 100644 index 0000000..e9356fc --- /dev/null +++ b/vue-admin-wonderful-next/.env.development @@ -0,0 +1,2 @@ +# public path +VITE_PUBLIC_PATH = 'vue-admin-wonderful-next-preview' \ No newline at end of file diff --git a/vue-admin-wonderful-next/.env.production b/vue-admin-wonderful-next/.env.production new file mode 100644 index 0000000..97aad4f --- /dev/null +++ b/vue-admin-wonderful-next/.env.production @@ -0,0 +1,2 @@ +# public path +VITE_PUBLIC_PATH = '' \ No newline at end of file diff --git a/vue-admin-wonderful-next/build/utils.ts b/vue-admin-wonderful-next/build/utils.ts new file mode 100644 index 0000000..a703028 --- /dev/null +++ b/vue-admin-wonderful-next/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/vue-admin-wonderful-next/vite.config.ts b/vue-admin-wonderful-next/vite.config.ts index 624c02e..d4f4cdc 100644 --- a/vue-admin-wonderful-next/vite.config.ts +++ b/vue-admin-wonderful-next/vite.config.ts @@ -1,15 +1,27 @@ -import type { UserConfig } from 'vite'; -import path from 'path'; +import type { UserConfig } from 'vite' +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 = { - sourcemap: false, - base: '../', - port: 8080, - hostname: 'localhost', - open: false, - alias: { - '/@/': path.resolve(__dirname, './src') - }, + root, + alias, + outDir: 'dist', + minify: 'esbuild', + port: VITE_PORT, + open: VITE_OPEN, + base: process.env.NODE_ENV === "production" ? "./" : VITE_PUBLIC_PATH, } export default viteConfig