Files
eip4-web/vite.config.ts
zhengwr 690f4c755e fix(web): dev 反代默认目标 8084→8093,fresh checkout 开箱即通
.env.local(8093) 被 gitignore 排除,新克隆退回 .env/vite.config 的 8084(local profile)死口。
统一提交进库的默认值为 8093(dev profile),无需手建 .env.local 即可联调。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-20 09:31:18 +08:00

96 lines
3.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { defineConfig, loadEnv } from 'vite';
import react from '@vitejs/plugin-react';
import { resolve } from 'path';
// 开发期 dev server 把 /api 反代到后端(dev profile 默认 8093,端口被占时用 .env.local 覆盖)。
// 后端地址经 .env 的 VITE_BACKEND_TARGET 配置;生产由 Nginx 反代,前端只发相对 /api。
//
// F4-L1 多页应用(MPA):
// index.html → 主应用(amis sdk-local UMD)
// editor.html → 设计器(amis npm 包,与主应用隔离)
//
// amis 栈(amis/amis-editor/amis-editor-core/amis-ui/amis-core/amis-formula/i18n-runtime)
// 均为 webpack 产物,package.json 无 exports 字段,vite esbuild pre-bundle 会失败。
// 解法:optimizeDeps.exclude 排出这些包,让 vite 对它们走 native CJS → ESM 转换;
// resolve.alias 把 i18n-runtime 直接指向 lib/index.js(无 exports 字段的包需要手动指向)。
// CSS 由 editor.html <link> 直接引用 /node_modules/... 路径,server.fs.allow 放开目录。
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), '');
const backendTarget = env.VITE_BACKEND_TARGET || 'http://localhost:8093';
return {
plugins: [react()],
resolve: {
alias: {
// i18n-runtime 无 exports 字段,vite pre-bundle 无法找到入口;手动指向 lib/index.js
'i18n-runtime': resolve(__dirname, 'node_modules/i18n-runtime/lib/index.js'),
},
},
server: {
port: 5173,
proxy: {
'/api': {
target: backendTarget,
changeOrigin: true,
},
},
// 允许 vite dev server 服务 node_modules 下的静态文件(editor.html 的 CSS link)
fs: {
allow: ['..'],
},
},
// `npm run preview` 托管 dist 产物时也把 /api 反代到后端,便于本地验证生产构建
// (生产真部署由 Nginx 反代 /api;此处仅本地 prod-build 验证用,与 server.proxy 同口径)。
preview: {
proxy: {
'/api': {
target: backendTarget,
changeOrigin: true,
},
},
},
build: {
// 产物直接交 Nginx 托管;amis 本地 SDK 在 public/ 下原样拷贝,不参与打包。
outDir: 'dist',
sourcemap: false,
rollupOptions: {
input: {
main: resolve(__dirname, 'index.html'),
editor: resolve(__dirname, 'editor.html'),
},
},
},
optimizeDeps: {
// amis 系列包及其 CJS 传递依赖全部显式 include,让 vite esbuild 统一预构建为 ESM。
// 不用 exclude——exclude 会让传递依赖(如 remove-accents)跳过 CJS→ESM 转换,
// 导致 "does not provide an export named 'default'" 运行时错误。
include: [
'amis',
'amis-editor',
'amis-editor-core',
'amis-ui',
'amis-core',
'amis-formula',
'amis-theme-editor-helper',
'remove-accents',
'classnames',
'lodash',
'hoist-non-react-statics',
'react-is',
'prop-types',
'qs',
'file-saver',
'papaparse',
'ua-parser-js',
'immutability-helper',
'dayjs',
'moment',
'codemirror',
'markdown-it',
'jsbarcode',
'keycode',
'deep-diff',
],
},
};
});