You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.2 KiB

5 months ago
// @ts-expect-error connect-history-api-fallback 未提供类型声明
import history from 'connect-history-api-fallback'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { defineConfig, loadEnv } from 'vite'
5 months ago
import vue from '@vitejs/plugin-vue'
import { viteStaticCopy } from 'vite-plugin-static-copy'
import type { Plugin } from 'vite'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
function resolveBuildOutDir(rawPath: string | undefined): string {
const configured = rawPath?.trim()
if (!configured) {
return path.resolve(__dirname, 'dist/admin')
}
return path.isAbsolute(configured)
? configured
: path.resolve(__dirname, configured)
}
5 months ago
function spaHistoryFallback(): Plugin {
const middleware = history({ index: '/index.html' })
return {
name: 'spa-history-fallback',
configureServer(server) {
return () => {
server.middlewares.use(middleware)
}
},
configurePreviewServer(server) {
return () => {
server.middlewares.use(middleware)
}
},
}
}
// https://vite.dev/config/
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, __dirname, '')
const adminOutDir = resolveBuildOutDir(env.FRONTEND_BUILD_OUT_DIR)
return {
base: mode === 'production' ? '/admin/' : '/',
build: {
outDir: adminOutDir,
emptyOutDir: true,
},
plugins: [
vue(),
spaHistoryFallback(),
/** 勿在 dev 对 /frontend/prototype 挂 sirv:会与 import prototype.css 冲突(返回 text/css 导致动态 import .vue 失败) */
viteStaticCopy({
targets: [
{
src: 'frontend/prototype/**/*',
dest: 'frontend/prototype',
rename: { stripBase: 2 },
},
],
}),
],
server: {
open: '/',
proxy: {
'/api': {
// cxxfds-service;与本机 php artisan serve 端口一致(8000 常被其它 Laravel 占用时改用 8001)
target: 'http://127.0.0.1:8002',
changeOrigin: true,
5 months ago
},
},
},
preview: {
proxy: {
'/api': {
target: 'http://127.0.0.1:8002',
changeOrigin: true,
},
5 months ago
},
},
}
})