Skip to content

Commit 45f80fc

Browse files
committed
chore: 更新流式构建配置,禁用 PreviewerBubble 以避免引入 codemirror
1 parent 2105e52 commit 45f80fc

File tree

3 files changed

+76
-18
lines changed

3 files changed

+76
-18
lines changed

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

Lines changed: 73 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import commonjs from '@rollup/plugin-commonjs';
3030
import alias from '@rollup/plugin-alias';
3131
import path from 'path';
3232
import { fileURLToPath } from 'url';
33-
import fs from 'fs';
3433

3534
const terserPlugin = (options = {}) =>
3635
terser({
@@ -47,8 +46,8 @@ const terserPlugin = (options = {}) =>
4746
// 自定义插件:拦截 core/HooksConfig 导入,重定向到 stream 版本
4847
const hooksConfigInterceptPlugin = {
4948
name: 'hooks-config-intercept',
50-
51-
resolveId(source, importer) {
49+
50+
resolveId(source) {
5251
// 拦截 HooksConfig 导入
5352
if (source === './core/HooksConfig' || source.endsWith('/core/HooksConfig')) {
5453
console.log('[hooks-config-intercept] Redirecting HooksConfig to stream version');
@@ -61,37 +60,82 @@ const hooksConfigInterceptPlugin = {
6160
},
6261
};
6362

63+
// 自定义插件:强制标记 codemirror 相关模块为外部依赖
64+
const codemirrorExternalPlugin = {
65+
name: 'codemirror-external',
66+
67+
resolveId(source) {
68+
// 拦截所有 codemirror 相关的导入,强制标记为外部依赖
69+
if (source === 'codemirror' || source.startsWith('codemirror/')) {
70+
console.log(`[codemirror-external] Marking "${source}" as external`);
71+
// 直接返回外部依赖标记,确保 rollup 不会尝试 bundle 这个模块
72+
return { id: source, external: true };
73+
}
74+
return null;
75+
},
76+
};
77+
78+
// 自定义插件:移除 PreviewerBubble 相关代码,避免引入 codemirror
79+
const removePreviewerBubblePlugin = {
80+
name: 'remove-previewer-bubble',
81+
82+
transform(code, id) {
83+
// 只处理 Previewer.js 文件
84+
if (!id.endsWith('Previewer.js')) {
85+
return null;
86+
}
87+
88+
console.log('[remove-previewer-bubble] Removing PreviewerBubble from Previewer.js');
89+
90+
// 移除 PreviewerBubble 的导入语句
91+
let modifiedCode = code.replace(
92+
/import\s+PreviewerBubble\s+from\s+['"]\.\/toolbars\/PreviewerBubble['"];?\s*/,
93+
'// PreviewerBubble removed in stream build\n',
94+
);
95+
96+
// 将 $initPreviewerBubble 方法改为空实现
97+
modifiedCode = modifiedCode.replace(
98+
/\$initPreviewerBubble\(\)\s*\{[^}]*this\.previewerBubble\s*=\s*new\s+PreviewerBubble\([^)]*\);?\s*\}/,
99+
'$initPreviewerBubble() {\n // PreviewerBubble disabled in stream build\n }',
100+
);
101+
102+
return {
103+
code: modifiedCode,
104+
map: null,
105+
};
106+
},
107+
};
108+
64109
// 明确列出需要的插件
65-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
66-
const srcPath = path.resolve(__dirname, '../src');
110+
const dirname = path.dirname(fileURLToPath(import.meta.url));
111+
const srcPath = path.resolve(dirname, '../src');
67112

68113
const basePlugins = [
69-
hooksConfigInterceptPlugin, // 优先级最高,拦截 HooksConfig 导入
114+
codemirrorExternalPlugin, // 最高优先级:强制标记 codemirror 为外部依赖
115+
hooksConfigInterceptPlugin, // 拦截 HooksConfig 导入,重定向到 stream 版本
116+
removePreviewerBubblePlugin, // 移除 PreviewerBubble,避免引入 codemirror
70117
baseConfig.plugins.find((p) => p.name === 'json'),
71118
baseConfig.plugins.find((p) => p.name === 'replace'),
72119
// 添加 alias 插件以解析 @/ 别名
73120
alias({
74-
entries: [
75-
{ find: '@', replacement: srcPath },
76-
],
121+
entries: [{ find: '@', replacement: srcPath }],
77122
}),
78123
resolve({
79124
browser: true,
80125
preferBuiltins: false,
81-
mainFields: ['module', 'browser', 'main'],
126+
mainFields: ['module', 'jsnext:main', 'browser', 'main'],
82127
exportConditions: ['default', 'module'],
83-
resolveOnly: [],
84128
}),
85129
commonjs({
86130
include: [/node_modules/, /src[\\/]libs/],
87-
exclude: [/node_modules[\\/](lodash-es|d3-.*[\\/]src|d3[\\/]src|dagre-d3-es)/],
131+
exclude: [/node_modules[\\/](lodash-es|d3-.*[\\/]src|d3[\\/]src|dagre-d3-es|codemirror)/],
88132
extensions: ['.js'],
89133
ignoreGlobal: false,
90134
sourceMap: false,
91135
ignoreDynamicRequires: true,
92136
}),
93137
baseConfig.plugins.find((p) => p.name === 'babel'),
94-
baseConfig.plugins.find((p) => p.name === 'dist-types'),
138+
// Stream 构建不使用 dist-types 插件,避免生成不准确的类型声明
95139
].filter(Boolean);
96140

97141
const umdPlugins = [...basePlugins, terserPlugin()];
@@ -122,6 +166,7 @@ const esmOutputConfig = {
122166
compact: true,
123167
interop: 'auto',
124168
inlineDynamicImports: false,
169+
preserveEntrySignatures: 'allow-extension',
125170
};
126171

127172
const streamExternal = [
@@ -130,6 +175,8 @@ const streamExternal = [
130175
'codemirror',
131176
/^codemirror\/.*/,
132177
'echarts',
178+
'prismjs',
179+
/^prismjs\/components\/.*/,
133180
];
134181

135182
/**
@@ -138,7 +185,10 @@ const streamExternal = [
138185
*/
139186
const umdConfig = {
140187
input: 'src/index.stream.js',
141-
output: umdOutputConfig,
188+
output: {
189+
...umdOutputConfig,
190+
inlineDynamicImports: true, // 内联动态导入,避免生成额外的chunk
191+
},
142192
plugins: umdPlugins,
143193
external: streamExternal,
144194
cache: true, // 启用缓存加速重新构建
@@ -147,6 +197,7 @@ const umdConfig = {
147197
moduleSideEffects: 'no-external',
148198
propertyReadSideEffects: false,
149199
tryCatchDeoptimization: false,
200+
preset: 'recommended',
150201
},
151202
onwarn: baseConfig.onwarn,
152203
};
@@ -155,22 +206,27 @@ const umdConfig = {
155206
* ESM 配置 - 用于模块化导入,支持 tree-shaking
156207
* 特点:
157208
* - 保留 ES6 模块语法,让消费者的打包工具进行 tree-shaking
158-
* - inlineDynamicImports: false 允许代码分割,支持更好的 tree-shaking
209+
* - inlineDynamicImports: true 暂时内联动态导入,确保构建成功
159210
* - 对应用消费者的打包工具配置提出的要求更低
160211
*/
161212
const esmConfig = {
162213
input: 'src/index.stream.js',
163-
output: esmOutputConfig,
214+
output: {
215+
...esmOutputConfig,
216+
inlineDynamicImports: true, // 内联动态导入,避免生成额外的chunk
217+
},
164218
plugins: esmPlugins,
165219
external: streamExternal,
166220
cache: true, // 启用缓存加速重新构建
167221
maxParallelFileOps: 20, // 并行处理优化
168222
treeshake: {
169223
// 关键:'no-external' 意味着只对非外部模块进行副作用分析
170-
// 这样 Suggester.js 中的 codemirror 导入会被正确处理
224+
// 这样 codemirror 导入会被正确处理为外部依赖,不会被 bundle
171225
moduleSideEffects: 'no-external',
172226
propertyReadSideEffects: false,
173227
tryCatchDeoptimization: false,
228+
// 确保删除未使用的代码
229+
preset: 'recommended',
174230
},
175231
onwarn: baseConfig.onwarn,
176232
};

packages/cherry-markdown/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "cherry-markdown",
33
"license": "Apache-2.0",
4-
"version": "0.10.3-pkg-11",
4+
"version": "0.10.3-pkg-12",
55
"type": "module",
66
"description": "a new markdown editor",
77
"repository": {

packages/cherry-markdown/src/CherryStream.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ export default class CherryStream extends CherryStatic {
7777
this.options.isPreviewOnly = true;
7878
this.options.editor.defaultModel = 'previewOnly';
7979
this.options.toolbars.showToolbar = false;
80+
// Stream 版本禁用 PreviewerBubble,避免引入 codemirror
81+
this.options.previewer.enablePreviewerBubble = false;
8082

8183
this.locales = locales;
8284
if (this.options.locales) {

0 commit comments

Comments
 (0)