From ca347bd2231000addd65338ab27a5684e7f78fcf Mon Sep 17 00:00:00 2001 From: Zzc <1373857752@qq.com> Date: Thu, 6 Nov 2025 14:57:36 +0800 Subject: [PATCH] =?UTF-8?q?refactor(vite):=20=E4=B8=BA=E7=A7=BB=E5=8A=A8?= =?UTF-8?q?=E7=AB=AF=E5=92=8C=E5=B1=8F=E5=B9=95=E6=9E=84=E5=BB=BA=E5=A2=9E?= =?UTF-8?q?=E5=BC=BA=E5=9F=BA=E7=A1=80=E8=B7=AF=E5=BE=84=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 引入实用函数,动态地标准化和解析基础路径,允许通过 CLI 标志、环境变量或预设默认值进行配置。此更新在不改变核心功能的情况下,简化了跨不同环境的部署。 --- packages/mobile/vite.config.js | 107 +++++++++++++++++++-------- packages/screen/vite.config.js | 127 +++++++++++++++++++++++---------- 2 files changed, 166 insertions(+), 68 deletions(-) diff --git a/packages/mobile/vite.config.js b/packages/mobile/vite.config.js index e957a82..3043b4f 100644 --- a/packages/mobile/vite.config.js +++ b/packages/mobile/vite.config.js @@ -1,37 +1,86 @@ -import { defineConfig } from 'vite' +import { defineConfig, loadEnv } from 'vite' import vue from '@vitejs/plugin-vue' import { resolve } from 'path' import Components from 'unplugin-vue-components/vite' import { VantResolver } from 'unplugin-vue-components/resolvers' -export default defineConfig({ - base: process.env.NODE_ENV === 'production' ? '/bxztapp/' : '/', - plugins: [ - vue(), - Components({ - resolvers: [VantResolver()] - }) - ], - resolve: { - alias: { - '@': resolve(__dirname, 'src'), - '@shared': resolve(__dirname, '../shared') +const DEFAULT_BUILD_BASE = '/bxztapp/' + +const normalizeBasePath = (value) => { + if (!value || value === '/') { + return '/' + } + let base = value.trim() + if (!base.startsWith('/')) { + base = `/${base}` + } + if (!base.endsWith('/')) { + base = `${base}/` + } + return base +} + +const resolveCliBase = () => { + const argv = process.argv || [] + const directFlagIndex = argv.indexOf('--base') + if (directFlagIndex !== -1 && argv[directFlagIndex + 1]) { + return argv[directFlagIndex + 1] + } + const customFlagIndex = argv.indexOf('--basePath') + if (customFlagIndex !== -1 && argv[customFlagIndex + 1]) { + return argv[customFlagIndex + 1] + } + const equalArg = argv.find(arg => arg.startsWith('--base=')) + if (equalArg) { + return equalArg.split('=')[1] + } + const equalCustomArg = argv.find(arg => arg.startsWith('--basePath=')) + if (equalCustomArg) { + return equalCustomArg.split('=')[1] + } + return undefined +} + +export default defineConfig(({ command, mode }) => { + const env = loadEnv(mode, process.cwd(), '') + const baseCandidate = + command === 'build' + ? resolveCliBase() ?? + env.VITE_BASE_PATH ?? + env.BASE_PATH ?? + process.env.BASE_PATH ?? + DEFAULT_BUILD_BASE + : '/' + + return { + base: process.env.NODE_ENV === 'production' ? normalizeBasePath(baseCandidate) : '/', + plugins: [ + vue(), + Components({ + resolvers: [VantResolver()] + }) + ], + resolve: { + alias: { + '@': resolve(__dirname, 'src'), + '@shared': resolve(__dirname, '../shared') + } + }, + server: { + port: 8080, + host: '0.0.0.0', + open: true, + proxy: { + '/snow-ops-platform': { + target: 'http://8.137.54.85:8661/', + changeOrigin: true, + }, + } + }, + build: { + outDir: 'dist', + assetsDir: 'assets', + sourcemap: false } - }, - server: { - port: 8080, - host: '0.0.0.0', - open: true, - proxy: { - '/snow-ops-platform': { - target: 'http://8.137.54.85:8661/', - changeOrigin: true, - }, - } - }, - build: { - outDir: 'dist', - assetsDir: 'assets', - sourcemap: false } }) diff --git a/packages/screen/vite.config.js b/packages/screen/vite.config.js index 2eef6f6..b4fb833 100644 --- a/packages/screen/vite.config.js +++ b/packages/screen/vite.config.js @@ -1,48 +1,97 @@ -import { defineConfig } from 'vite' +import { defineConfig, loadEnv } from 'vite' import AutoImport from 'unplugin-auto-import/vite' import Components from 'unplugin-vue-components/vite' import { ElementPlusResolver } from 'unplugin-vue-components/resolvers' import vue from '@vitejs/plugin-vue' import { resolve } from 'path' -export default defineConfig({ - base: process.env.NODE_ENV === 'production' ? '/bxztpc/' : '/', - plugins: [ - vue(), - AutoImport({ - resolvers: [ElementPlusResolver()], - }), - Components({ - resolvers: [ElementPlusResolver()], - }), - ], - resolve: { - alias: { - '@': resolve(__dirname, 'src'), - '@shared': resolve(__dirname, '../shared') - } - }, - server: { - port: 3000, - open: true, - cors: true, - proxy: { - '/snow-ops-platform': { - target: 'http://8.137.54.85:8661/', - changeOrigin: true, - }, - } - }, - build: { - outDir: 'dist', - assetsDir: 'assets', - sourcemap: false, - minify: 'terser', - rollupOptions: { - output: { - chunkFileNames: 'js/[name]-[hash].js', - entryFileNames: 'js/[name]-[hash].js', - assetFileNames: '[ext]/[name]-[hash].[ext]' +const DEFAULT_BUILD_BASE = '/bxztpc/' + +const normalizeBasePath = (value) => { + if (!value || value === '/') { + return '/' + } + let base = value.trim() + if (!base.startsWith('/')) { + base = `/${base}` + } + if (!base.endsWith('/')) { + base = `${base}/` + } + return base +} + +const resolveCliBase = () => { + const argv = process.argv || [] + const directFlagIndex = argv.indexOf('--base') + if (directFlagIndex !== -1 && argv[directFlagIndex + 1]) { + return argv[directFlagIndex + 1] + } + const customFlagIndex = argv.indexOf('--basePath') + if (customFlagIndex !== -1 && argv[customFlagIndex + 1]) { + return argv[customFlagIndex + 1] + } + const equalArg = argv.find(arg => arg.startsWith('--base=')) + if (equalArg) { + return equalArg.split('=')[1] + } + const equalCustomArg = argv.find(arg => arg.startsWith('--basePath=')) + if (equalCustomArg) { + return equalCustomArg.split('=')[1] + } + return undefined +} + +export default defineConfig(({ command, mode }) => { + const env = loadEnv(mode, process.cwd(), '') + const baseCandidate = + command === 'build' + ? resolveCliBase() ?? + env.VITE_BASE_PATH ?? + env.BASE_PATH ?? + process.env.BASE_PATH ?? + DEFAULT_BUILD_BASE + : '/' + + return { + base: process.env.NODE_ENV === 'production' ? normalizeBasePath(baseCandidate) : '/', + plugins: [ + vue(), + AutoImport({ + resolvers: [ElementPlusResolver()], + }), + Components({ + resolvers: [ElementPlusResolver()], + }), + ], + resolve: { + alias: { + '@': resolve(__dirname, 'src'), + '@shared': resolve(__dirname, '../shared') + } + }, + server: { + port: 3000, + open: true, + cors: true, + proxy: { + '/snow-ops-platform': { + target: 'http://8.137.54.85:8661/', + changeOrigin: true, + }, + } + }, + build: { + outDir: 'dist', + assetsDir: 'assets', + sourcemap: false, + minify: 'terser', + rollupOptions: { + output: { + chunkFileNames: 'js/[name]-[hash].js', + entryFileNames: 'js/[name]-[hash].js', + assetFileNames: '[ext]/[name]-[hash].[ext]' + } } } }