-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
136 lines (126 loc) · 4.82 KB
/
vite.config.ts
File metadata and controls
136 lines (126 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { defineConfig } from 'vite';
import { resolve, dirname } from 'path';
import { fileURLToPath } from 'url';
import react from '@vitejs/plugin-react-swc';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
export default defineConfig(({ mode }) => {
const isLibrary = mode === 'library';
if (isLibrary) {
// 库模式配置 - 只构建库代码,不包含开发预览代码
return {
plugins: [react()],
build: {
lib: {
entry: {
index: resolve(__dirname, 'src/index.ts'),
'html-editor': resolve(__dirname, 'src/html-editor.ts'),
},
name: 'EmailBuilder',
formats: ['es'],
},
// minify: 'terser',
// terserOptions: {
// compress: {
// drop_console: true,
// drop_debugger: true,
// pure_funcs: ['console.log', 'console.info', 'console.debug'],
// },
// },
minify: 'esbuild',
// esbuild 级别的压缩配置
esbuild: {
drop: ['console', 'debugger'],
},
// 启用代码分割(lib 模式下需要明确启用)
cssCodeSplit: true,
// 设置 chunk 大小警告限制(KB)
chunkSizeWarningLimit: 1000,
rollupOptions: {
external: (id) => {
// 明确列出需要外部化的依赖(优先匹配)
const explicitExternals = [
'react',
'react-dom',
'@mui/material',
'@mui/icons-material',
'@emotion/react',
'@emotion/styled',
'react-syntax-highlighter',
];
// 检查是否匹配明确列出的依赖
if (explicitExternals.some(dep => id === dep || id.startsWith(`${dep}/`))) {
return true;
}
// 外部化所有 node_modules 中的依赖(但排除明确列出的)
if (!id.startsWith('.') && !id.startsWith('/') && !id.includes('src/')) {
return true;
}
return false;
},
output: {
globals: {
react: 'React',
'react-dom': 'ReactDOM',
},
// 代码分割策略:将不同模块分离到独立的 chunk,减小单文件大小
// 注意:避免过度分割导致循环依赖,只分离真正独立的大型模块
manualChunks: (id) => {
// 1. CodeMirror 相关依赖分离(体积较大,仅在 HtmlEditor 中使用,完全独立)
if (
id.includes('@uiw/react-codemirror') ||
id.includes('@codemirror/') ||
id.includes('@uiw/codemirror-themes-all')
) {
return 'codemirror';
}
// 2. 示例模板分离(按需加载,完全独立)
if (id.includes('getConfiguration/sample/')) {
return 'samples';
}
// 注意:其他模块(blocks、helpers、App组件等)保留在主包中
// 因为它们之间存在相互依赖,分离会导致循环依赖
// 这样可以减小单文件大小,同时避免循环依赖问题
},
// 设置 chunk 文件命名格式
chunkFileNames: (chunkInfo) => {
// 为不同的 chunk 设置不同的命名规则
const facadeModuleId = chunkInfo.facadeModuleId
? chunkInfo.facadeModuleId.split('/').pop()?.replace(/\.[^/.]+$/, '')
: 'chunk';
return `chunks/${facadeModuleId || 'chunk'}-[hash].js`;
},
// 设置入口文件命名格式
entryFileNames: (chunkInfo) => {
// 主入口保持 index.js,html-editor 入口使用 html-editor.js
if (chunkInfo.name === 'html-editor') {
return 'html-editor.js';
}
return 'index.js';
},
},
},
},
};
}
// 开发/预览模式配置 - 使用 docs 文件夹作为开发预览
return {
plugins: [react()],
root: resolve(__dirname, 'docs'),
// 支持通过环境变量设置 base 路径(用于 GitHub Pages)
base: process.env.VITE_BASE_PATH || '/',
resolve: {
alias: {
// 使用本地源码进行调试
// 'monto-email-block-socials': resolve(__dirname, 'monto-email-block-socials/src'),
// 'monto-email-core': resolve(__dirname, 'monto-email-core/src'),
// 'monto-email-block-html': resolve(__dirname, 'block-html/src'),
// 'monto-email-block-columns-container': resolve(__dirname, 'block-columns-container/src'),
},
},
build: {
outDir: resolve(__dirname, 'docs-dist'),
emptyOutDir: true,
},
};
});