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 直接引用 /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: ['..'], }, }, 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', ], }, }; });