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.

102 lines
2.8 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' })
1 month ago
const skipFrontendThenHistory = (
req: { url?: string },
res: unknown,
next: (err?: unknown) => void,
) => {
/** 勿把 Laravel public/frontend 原型页回退成 SPA,否则打开为空白壳 */
if ((req.url || '').startsWith('/frontend')) {
next()
return
}
middleware(req, res, next)
}
5 months ago
return {
name: 'spa-history-fallback',
configureServer(server) {
return () => {
1 month ago
server.middlewares.use(skipFrontendThenHistory)
5 months ago
}
},
configurePreviewServer(server) {
return () => {
1 month ago
server.middlewares.use(skipFrontendThenHistory)
5 months ago
}
},
}
}
// https://vite.dev/config/
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, __dirname, '')
const adminOutDir = resolveBuildOutDir(env.FRONTEND_BUILD_OUT_DIR)
4 weeks ago
const laravelProxyTarget = (env.LARAVEL_DEV_URL || 'http://127.0.0.1:8002').replace(/\/$/, '')
const laravelDevProxy = {
'/api': {
target: laravelProxyTarget,
changeOrigin: true,
},
/** 登录页快捷入口等静态原型(layout / pages),需代理到 backend/public/frontend */
'/frontend': {
target: laravelProxyTarget,
changeOrigin: true,
},
'/storage': {
target: laravelProxyTarget,
changeOrigin: true,
},
} as const
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: '/',
1 month ago
proxy: { ...laravelDevProxy },
5 months ago
},
preview: {
1 month ago
proxy: { ...laravelDevProxy },
5 months ago
},
}
})