@@ -30,7 +30,6 @@ import commonjs from '@rollup/plugin-commonjs';
3030import alias from '@rollup/plugin-alias' ;
3131import path from 'path' ;
3232import { fileURLToPath } from 'url' ;
33- import fs from 'fs' ;
3433
3534const terserPlugin = ( options = { } ) =>
3635 terser ( {
@@ -47,8 +46,8 @@ const terserPlugin = (options = {}) =>
4746// 自定义插件:拦截 core/HooksConfig 导入,重定向到 stream 版本
4847const 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+ / i m p o r t \s + P r e v i e w e r B u b b l e \s + f r o m \s + [ ' " ] \. \/ t o o l b a r s \/ P r e v i e w e r B u b b l e [ ' " ] ; ? \s * / ,
93+ '// PreviewerBubble removed in stream build\n' ,
94+ ) ;
95+
96+ // 将 $initPreviewerBubble 方法改为空实现
97+ modifiedCode = modifiedCode . replace (
98+ / \$ i n i t P r e v i e w e r B u b b l e \( \) \s * \{ [ ^ } ] * t h i s \. p r e v i e w e r B u b b l e \s * = \s * n e w \s + P r e v i e w e r B u b b l e \( [ ^ ) ] * \) ; ? \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
68113const 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 : [ / n o d e _ m o d u l e s / , / s r c [ \\ / ] l i b s / ] ,
87- exclude : [ / n o d e _ m o d u l e s [ \\ / ] ( l o d a s h - e s | d 3 - .* [ \\ / ] s r c | d 3 [ \\ / ] s r c | d a g r e - d 3 - e s ) / ] ,
131+ exclude : [ / n o d e _ m o d u l e s [ \\ / ] ( l o d a s h - e s | d 3 - .* [ \\ / ] s r c | d 3 [ \\ / ] s r c | d a g r e - d 3 - e s | c o d e m i r r o r ) / ] ,
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
97141const umdPlugins = [ ...basePlugins , terserPlugin ( ) ] ;
@@ -122,6 +166,7 @@ const esmOutputConfig = {
122166 compact : true ,
123167 interop : 'auto' ,
124168 inlineDynamicImports : false ,
169+ preserveEntrySignatures : 'allow-extension' ,
125170} ;
126171
127172const streamExternal = [
@@ -130,6 +175,8 @@ const streamExternal = [
130175 'codemirror' ,
131176 / ^ c o d e m i r r o r \/ .* / ,
132177 'echarts' ,
178+ 'prismjs' ,
179+ / ^ p r i s m j s \/ c o m p o n e n t s \/ .* / ,
133180] ;
134181
135182/**
@@ -138,7 +185,10 @@ const streamExternal = [
138185 */
139186const 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 */
161212const 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} ;
0 commit comments