Skip to content

Commit aa1b29d

Browse files
committed
fix: 修复构建配置以避免静态属性被错误优化
1 parent 291a5fc commit aa1b29d

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

packages/cherry-markdown/build/build.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ const umdConfig = {
4343
name: 'Cherry',
4444
sourcemap: true,
4545
compact: true,
46-
inlineDynamicImports: true,
46+
// 禁用 inlineDynamicImports 以避免类定义外的代码被 tree shaking 优化掉
47+
// 这对于静态属性(如 Cherry.constants)的保留是必要的
48+
inlineDynamicImports: false,
4749
},
4850
plugins: umdPlugins,
4951
cache: true, // 启用缓存加速重新构建
@@ -70,6 +72,8 @@ const esmConfig = {
7072
compact: true,
7173
interop: 'auto',
7274
inlineDynamicImports: false,
75+
// 保留导出符号的完整性,确保静态属性不被优化掉
76+
preserveEntrySignatures: 'strict',
7377
// 性能优化:自动代码分割
7478
manualChunks(id) {
7579
// 将 node_modules 中的依赖分离到 vendor chunk

packages/cherry-markdown/build/rollup.base.config.js

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,27 +49,34 @@ const options = {
4949
},
5050
// 性能优化:启用缓存加速二次构建
5151
cache: true,
52-
52+
5353
// 并行处理优化
5454
maxParallelFileOps: 20,
55-
55+
5656
// Tree-shake 配置 - 平衡优化和兼容性
5757
treeshake: {
58-
// 只标记明确的副作用模块
58+
// 标记模块副作用
5959
moduleSideEffects: (id) => {
6060
// CSS 文件有副作用
6161
if (id.endsWith('.css')) return true;
62-
// 对第三方库保持保守,避免错误 tree-shake
63-
if (id.includes('node_modules/')) {
64-
// crypto-js、mermaid、echarts 等库在 tree-shake 时会出问题
65-
if (id.includes('crypto-js') || id.includes('mermaid') || id.includes('echarts')) {
66-
return true;
67-
}
68-
// prismjs 及其组件有副作用(将语言注册到 Prism.languages)
69-
if (id.includes('prismjs')) {
70-
return true;
71-
}
62+
63+
// 源代码模块默认有副作用,避免错误 tree-shake
64+
// 源代码不在 node_modules 中
65+
if (!id.includes('node_modules')) {
66+
return true;
67+
}
68+
69+
// 对特定第三方库保持保守,避免错误 tree-shake
70+
// crypto-js、mermaid、echarts 等库在 tree-shake 时会出问题
71+
if (id.includes('crypto-js') || id.includes('mermaid') || id.includes('echarts')) {
72+
return true;
7273
}
74+
// prismjs 及其组件有副作用(将语言注册到 Prism.languages)
75+
if (id.includes('prismjs')) {
76+
return true;
77+
}
78+
79+
// 其他 node_modules 可以安全地 tree-shake
7380
return false;
7481
},
7582
// 禁用属性读取副作用检查,确保 Prism.languages 赋值不被优化掉
@@ -119,13 +126,13 @@ const options = {
119126

120127
// 优化:忽略动态 require
121128
ignoreDynamicRequires: true,
122-
129+
123130
// 性能优化:严格模式检查
124131
strictRequires: 'auto',
125-
132+
126133
// 默认导出模式
127134
defaultIsModuleExports: 'auto',
128-
135+
129136
// 要求语义
130137
requireReturnsDefault: 'auto',
131138
}),

0 commit comments

Comments
 (0)