From 1dd9e97a43186ad9f3767faea2fd87dd772e3833 Mon Sep 17 00:00:00 2001 From: bunny <1319900154@qq.com> Date: Tue, 28 May 2024 13:06:49 +0800 Subject: [PATCH] =?UTF-8?q?fixbug:=20=F0=9F=90=9B=20=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E4=BB=A3=E7=90=86=E6=97=A0=E6=B3=95=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env | 10 ++++++++-- .env.development | 10 ++++++++-- .env.production | 10 ++++++++-- build/buildEnv.ts | 2 +- build/info.ts | 4 ++-- build/plugins.ts | 3 +-- build/server.ts | 4 ++-- build/utils.ts | 38 +++++++++++++++++++++++++++++++++++--- vite.config.ts | 17 +++++++++-------- 9 files changed, 74 insertions(+), 24 deletions(-) diff --git a/.env b/.env index acad4d5..c164e22 100644 --- a/.env +++ b/.env @@ -1,2 +1,8 @@ -# 请求api地址 -BUNNY_APP_URL=http://localhost:8088/xxx +# 线上环境接口地址 +VITE_APP_URL="http://localhost:8800" +# 线上环境接口地址 +VITE_APP_MOCK_URL="http://localhost:8800" +# 端口号 +VITE_APP_PORT=6262 +# 是否使用CDN加速 +VITE_CDN=true \ No newline at end of file diff --git a/.env.development b/.env.development index 8c22770..c164e22 100644 --- a/.env.development +++ b/.env.development @@ -1,2 +1,8 @@ -# 请求api地址 -BUNNY_APP_URL=http://localhost:8088/development +# 线上环境接口地址 +VITE_APP_URL="http://localhost:8800" +# 线上环境接口地址 +VITE_APP_MOCK_URL="http://localhost:8800" +# 端口号 +VITE_APP_PORT=6262 +# 是否使用CDN加速 +VITE_CDN=true \ No newline at end of file diff --git a/.env.production b/.env.production index 81a365b..c164e22 100644 --- a/.env.production +++ b/.env.production @@ -1,2 +1,8 @@ -# 请求api地址 -BUNNY_APP_URL=http://localhost:8088/prod +# 线上环境接口地址 +VITE_APP_URL="http://localhost:8800" +# 线上环境接口地址 +VITE_APP_MOCK_URL="http://localhost:8800" +# 端口号 +VITE_APP_PORT=6262 +# 是否使用CDN加速 +VITE_CDN=true \ No newline at end of file diff --git a/build/buildEnv.ts b/build/buildEnv.ts index 60a91a8..041ca41 100644 --- a/build/buildEnv.ts +++ b/build/buildEnv.ts @@ -36,7 +36,7 @@ export const buildEnvironment = () => { entryFileNames: 'js/[name]-[hash].js', // 用于指定入口文件的输出文件名格式 assetFileNames: 'assets/[ext]/[name]-[hash].[ext]', // 用于指定静态资源的输出文件名格式 // ? 配置文件生成方式 - manualChunks: id => { + manualChunks: (id, meta) => { // 如果是包含在包中则打包成 vendor if (id.includes('node_modules')) { return 'vendor'; diff --git a/build/info.ts b/build/info.ts index 7dfeab5..75d477c 100644 --- a/build/info.ts +++ b/build/info.ts @@ -10,9 +10,9 @@ dayjs.extend(duration); const welcomeMessage = gradientString('cyan', 'magenta').multiline( `您好! 欢迎使用 bunny-admin 后台管理 项目开发访问地址如下: -http://localhost:8202/ +http://localhost:6262/ 项目生产访问地址如下: -http://localhost:8202/`, +http://localhost:6262/`, ); const boxenOptions: BoxenOptions = { diff --git a/build/plugins.ts b/build/plugins.ts index 7889d0c..596396d 100644 --- a/build/plugins.ts +++ b/build/plugins.ts @@ -8,7 +8,6 @@ import { viteMockServe } from 'vite-plugin-mock'; import { viteBuildInfo } from './info'; export function getPluginsList(): PluginOption[] { - const lifecycle = process.env.npm_lifecycle_event; return [ vue(), legacy({ @@ -37,7 +36,7 @@ export function getPluginsList(): PluginOption[] { vueJsx(), compression(), viteBuildInfo(), - cdn, + process.env.VITE_CDN ? cdn : null, viteMockServe({ mockPath: 'src/mock' }), ]; } diff --git a/build/server.ts b/build/server.ts index 502b954..fd73674 100644 --- a/build/server.ts +++ b/build/server.ts @@ -8,12 +8,12 @@ export const serverOptions = () => { cors: true, proxy: { '/api': { - target: process.env.BUNNY_APP_URL, + target: process.env.VITE_APP_URL, changeOrigin: true, rewrite: (path: string) => path.replace(/^\/api/, '/api'), }, '/mock': { - target: process.env.BUNNY_APP_URL, + target: process.env.VITE_APP_MOCK_URL, changeOrigin: true, rewrite: (path: string) => path.replace(/^\/mock/, '/mock'), }, diff --git a/build/utils.ts b/build/utils.ts index ff5cfcf..d6af4a0 100644 --- a/build/utils.ts +++ b/build/utils.ts @@ -1,12 +1,15 @@ import { readdir, stat } from 'node:fs'; -import { formatBytes, sum } from '@pureadmin/utils'; +import { formatBytes, Recordable, sum } from '@pureadmin/utils'; import { fileURLToPath } from 'node:url'; import { dirname, resolve } from 'node:path'; +/** 启动`node`进程时所在工作目录的绝对路径 */ +const root: string = process.cwd(); + const fileListTotal: number[] = []; /** 获取指定文件夹中所有文件的总大小 */ -const getPackageSize = options => { +const getPackageSize = (options: any) => { const { folder = 'dist', callback, format = true } = options; readdir(folder, (err, files: string[]) => { if (err) throw err; @@ -52,4 +55,33 @@ const pathResolve = (dir = '.', metaUrl = import.meta.url) => { // 不在 build 目录内,返回解析后的绝对路径 return resolvedPath; }; -export { getPackageSize, pathResolve }; + +/** 处理环境变量 */ +const wrapperEnv = (envConf: Recordable): any => { + // TODO 默认值 + const ret: any = { + VITE_APP_PORT: 6262, + VITE_APP_URL: '', + VITE_APP_MOCK_URL: '', + VITE_CDN: false, + }; + + for (const envName of Object.keys(envConf)) { + let realName = envConf[envName].replace(/\\n/g, '\n'); + realName = realName === 'true' ? true : realName === 'false' ? false : realName; + + if (envName === 'VITE_PORT') { + realName = Number(realName); + } + ret[envName] = realName; + if (typeof realName === 'string') { + process.env[envName] = realName; + } else if (typeof realName === 'object') { + process.env[envName] = JSON.stringify(realName); + } + } + + return ret; +}; + +export { getPackageSize, pathResolve, root, wrapperEnv }; diff --git a/vite.config.ts b/vite.config.ts index 279e030..3279764 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,14 +1,15 @@ import { resolve } from 'path'; -import { defineConfig, UserConfig } from 'vite'; -import { buildEnvironment } from './build/buildEnv'; -import { exclude, include } from './build/optimize'; +import { ConfigEnv, UserConfigExport } from 'vite'; import { getPluginsList } from './build/plugins'; +import { buildEnvironment } from './build/buildEnv'; import { serverOptions } from './build/server'; +import { exclude, include } from './build/optimize'; import { pathResolve } from './build/utils.ts'; -export default defineConfig( - (): UserConfig => ({ - envPrefix: 'BUNNY', +export default ({ mode }: ConfigEnv): UserConfigExport => { + // const { VITE_APP_URL } = wrapperEnv(loadEnv(mode, root)); + + return { resolve: { alias: { '@': resolve(__dirname, './src'), @@ -27,5 +28,5 @@ export default defineConfig( }, // 配置构建过程的选项,例如是否生成压缩文件和源映射 build: buildEnvironment(), - }), -); + }; +};