diff --git a/.env.example b/.env.example index c49561a..d754c1e 100644 --- a/.env.example +++ b/.env.example @@ -1,4 +1,10 @@ -# 复制为 .env 或 .env.local 后生效(须以 VITE_ 开头才会注入构建) +# 复制为 .env.local 后生效;.env.local 已加入 .gitignore,适合放本机个性化配置。 +# 注意:只有 VITE_ 开头的变量会注入浏览器;FRONTEND_BUILD_OUT_DIR 只供 vite.config.ts 读取。 +# +# 管理端生产构建输出目录;可写相对 frontend/ 的路径或绝对路径。 +# 不设置时默认输出到 frontend/dist/admin,避免误写到某个固定 backend 仓库。 +# 例:FRONTEND_BUILD_OUT_DIR=../backend/public/admin +# FRONTEND_BUILD_OUT_DIR= # # 留空则开发时走当前站点同源 + Vite 代理 /api(推荐) # VITE_API_BASE= diff --git a/.gitignore b/.gitignore index a547bf3..5cbc626 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,8 @@ lerna-debug.log* node_modules dist dist-ssr +.env.local +.env.*.local *.local # Editor directories and files diff --git a/vite.config.ts b/vite.config.ts index 90eb773..9c724fa 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -2,13 +2,23 @@ import history from 'connect-history-api-fallback' import path from 'node:path' import { fileURLToPath } from 'node:url' -import { defineConfig } from 'vite' +import { defineConfig, loadEnv } from 'vite' 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)) -const laravelPublicAdmin = path.resolve(__dirname, '../xxfcyds2026-backend/public/admin') + +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) +} function spaHistoryFallback(): Plugin { const middleware = history({ index: '/index.html' }) @@ -28,42 +38,47 @@ function spaHistoryFallback(): Plugin { } // https://vite.dev/config/ -export default defineConfig(({ mode }) => ({ - base: mode === 'production' ? '/admin/' : '/', - build: { - outDir: laravelPublicAdmin, - 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 }, +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, }, - ], - }), - ], - server: { - open: '/', - proxy: { - '/api': { - // cxxfds-service;与本机 php artisan serve 端口一致(8000 常被其它 Laravel 占用时改用 8001) - target: 'http://127.0.0.1:8002', - changeOrigin: true, }, }, - }, - preview: { - proxy: { - '/api': { - target: 'http://127.0.0.1:8002', - changeOrigin: true, + preview: { + proxy: { + '/api': { + target: 'http://127.0.0.1:8002', + changeOrigin: true, + }, }, }, - }, -})) + } +})