Files
eip4-web/vite.config.ts
zhengwr 37298186eb chore(build): add preview proxy for local production-build verification
验证「设计器生产构建是否裂」时需要本地托管 dist 并能访问后端。`vite preview`
默认不带 /api 反代,补 preview.proxy(与 server.proxy 同口径,走 .env.local 的
VITE_BACKEND_TARGET)→ 可本地跑 `npm run preview` 验证 prod 构建。

结论(实测):生产构建本就安全——editor.html 源里 3 个 /node_modules CSS 直链在
vite build 时被解析+打包进 dist/assets/editor-*.css(2.3MB,含完整 amis-editor/
amis-ui 样式,1742×.ae- / 12155×.cxd-),dist/editor.html 零 /node_modules 残留;
preview 托管 dist 实测编辑器三栏完整带样式渲染(panelCount 123)、高度约束修复
生效(aeEditor 831px/clippedLayers 0)、schema 经 proxy 加载。memory「生产可能裂」
顾虑证伪。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-19 22:54:46 +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 反代到后端(默认 8084,端口被占时改 8090)。
// 后端地址经 .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:8084';
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',
],
},
};
});