refactor(vite): 为移动端和屏幕构建增强基础路径处理
引入实用函数,动态地标准化和解析基础路径,允许通过 CLI 标志、环境变量或预设默认值进行配置。此更新在不改变核心功能的情况下,简化了跨不同环境的部署。
This commit is contained in:
parent
ce9614295d
commit
ca347bd223
@ -1,11 +1,59 @@
|
|||||||
import { defineConfig } from 'vite'
|
import { defineConfig, loadEnv } from 'vite'
|
||||||
import vue from '@vitejs/plugin-vue'
|
import vue from '@vitejs/plugin-vue'
|
||||||
import { resolve } from 'path'
|
import { resolve } from 'path'
|
||||||
import Components from 'unplugin-vue-components/vite'
|
import Components from 'unplugin-vue-components/vite'
|
||||||
import { VantResolver } from 'unplugin-vue-components/resolvers'
|
import { VantResolver } from 'unplugin-vue-components/resolvers'
|
||||||
|
|
||||||
export default defineConfig({
|
const DEFAULT_BUILD_BASE = '/bxztapp/'
|
||||||
base: process.env.NODE_ENV === 'production' ? '/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: [
|
plugins: [
|
||||||
vue(),
|
vue(),
|
||||||
Components({
|
Components({
|
||||||
@ -34,4 +82,5 @@ export default defineConfig({
|
|||||||
assetsDir: 'assets',
|
assetsDir: 'assets',
|
||||||
sourcemap: false
|
sourcemap: false
|
||||||
}
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@ -1,12 +1,60 @@
|
|||||||
import { defineConfig } from 'vite'
|
import { defineConfig, loadEnv } from 'vite'
|
||||||
import AutoImport from 'unplugin-auto-import/vite'
|
import AutoImport from 'unplugin-auto-import/vite'
|
||||||
import Components from 'unplugin-vue-components/vite'
|
import Components from 'unplugin-vue-components/vite'
|
||||||
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
|
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
|
||||||
import vue from '@vitejs/plugin-vue'
|
import vue from '@vitejs/plugin-vue'
|
||||||
import { resolve } from 'path'
|
import { resolve } from 'path'
|
||||||
|
|
||||||
export default defineConfig({
|
const DEFAULT_BUILD_BASE = '/bxztpc/'
|
||||||
base: process.env.NODE_ENV === 'production' ? '/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: [
|
plugins: [
|
||||||
vue(),
|
vue(),
|
||||||
AutoImport({
|
AutoImport({
|
||||||
@ -46,4 +94,5 @@ export default defineConfig({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user