@@ -62,6 +62,7 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) =>
6262
6363 const ir = next . ir as IContainerInfo ;
6464 const rootScope = Scope . createRootScope ( ) ;
65+ const { tolerateEvalErrors = true , evalErrorsHandler = '' } = next . contextData ;
6566
6667 // Rax 构建到小程序的时候,不能给组件起起别名,得直接引用,故这里将所有的别名替换掉
6768 // 先收集下所有的 alias 的映射
@@ -86,7 +87,9 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) =>
8687 // 3. 通过 this.xxx 能拿到的东西太多了,而且自定义的 methods 可能会无意间破坏 Rax 框架或小程序框架在页面 this 上的东东
8788 const customHandlers : HandlerSet < string > = {
8889 expression ( input : JSExpression , scope : IScope ) {
89- return transformJsExpr ( generateExpression ( input , scope ) , scope ) ;
90+ return transformJsExpr ( generateExpression ( input , scope ) , scope , {
91+ dontWrapEval : ! tolerateEvalErrors ,
92+ } ) ;
9093 } ,
9194 function ( input , scope : IScope ) {
9295 return transformThis2Context ( input . value || 'null' , scope ) ;
@@ -138,34 +141,72 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) =>
138141 type : ChunkType . STRING ,
139142 fileType : cfg . fileType ,
140143 name : COMMON_CHUNK_NAME . CustomContent ,
141- content : `
142-
144+ content : [
145+ tolerateEvalErrors &&
146+ `
143147 function __$$eval(expr) {
144148 try {
145149 return expr();
146- } catch (err) {
147- try {
148- if (window.handleEvalError) {
149- window.handleEvalError('Failed to evaluate: ', expr, err);
150- }
151- } catch (e) {}
150+ } catch (error) {
151+ ${ evalErrorsHandler }
152152 }
153153 }
154154
155155 function __$$evalArray(expr) {
156156 const res = __$$eval(expr);
157157 return Array.isArray(res) ? res : [];
158158 }
159-
159+ ` ,
160+ `
160161 function __$$createChildContext(oldContext, ext) {
161162 return Object.assign({}, oldContext, ext);
162163 }
163-
164164 ` ,
165+ ]
166+ . filter ( Boolean )
167+ . join ( '\n' ) ,
165168 linkAfter : [ COMMON_CHUNK_NAME . FileExport ] ,
166169 } ) ;
167170
168171 return next ;
172+
173+ function generateRaxLoopCtrl (
174+ nodeItem : NodeSchema ,
175+ scope : IScope ,
176+ config ?: NodeGeneratorConfig ,
177+ next ?: NodePlugin ,
178+ ) : CodePiece [ ] {
179+ if ( nodeItem . loop ) {
180+ const loopItemName = nodeItem . loopArgs ?. [ 0 ] || 'item' ;
181+ const loopIndexName = nodeItem . loopArgs ?. [ 1 ] || 'index' ;
182+ const subScope = scope . createSubScope ( [ loopItemName , loopIndexName ] ) ;
183+ const pieces : CodePiece [ ] = next ? next ( nodeItem , subScope , config ) : [ ] ;
184+
185+ const loopDataExpr = tolerateEvalErrors
186+ ? `__$$evalArray(() => (${ transformThis2Context (
187+ generateCompositeType ( nodeItem . loop , scope , { handlers : config ?. handlers } ) ,
188+ scope ,
189+ ) } ))`
190+ : `(${ transformThis2Context (
191+ generateCompositeType ( nodeItem . loop , scope , { handlers : config ?. handlers } ) ,
192+ scope ,
193+ ) } )`;
194+
195+ pieces . unshift ( {
196+ value : `${ loopDataExpr } .map((${ loopItemName } , ${ loopIndexName } ) => ((__$$context) => (` ,
197+ type : PIECE_TYPE . BEFORE ,
198+ } ) ;
199+
200+ pieces . push ( {
201+ value : `))(__$$createChildContext(__$$context, { ${ loopItemName } , ${ loopIndexName } })))` ,
202+ type : PIECE_TYPE . AFTER ,
203+ } ) ;
204+
205+ return pieces ;
206+ }
207+
208+ return next ? next ( nodeItem , scope , config ) : [ ] ;
209+ }
169210 } ;
170211
171212 return plugin ;
@@ -189,39 +230,6 @@ function isImportAliasDefineChunk(chunk: ICodeChunk): chunk is ICodeChunk & {
189230 ) ;
190231}
191232
192- function generateRaxLoopCtrl (
193- nodeItem : NodeSchema ,
194- scope : IScope ,
195- config ?: NodeGeneratorConfig ,
196- next ?: NodePlugin ,
197- ) : CodePiece [ ] {
198- if ( nodeItem . loop ) {
199- const loopItemName = nodeItem . loopArgs ?. [ 0 ] || 'item' ;
200- const loopIndexName = nodeItem . loopArgs ?. [ 1 ] || 'index' ;
201- const subScope = scope . createSubScope ( [ loopItemName , loopIndexName ] ) ;
202- const pieces : CodePiece [ ] = next ? next ( nodeItem , subScope , config ) : [ ] ;
203-
204- const loopDataExpr = `__$$evalArray(() => (${ transformThis2Context (
205- generateCompositeType ( nodeItem . loop , scope , { handlers : config ?. handlers } ) ,
206- scope ,
207- ) } ))`;
208-
209- pieces . unshift ( {
210- value : `${ loopDataExpr } .map((${ loopItemName } , ${ loopIndexName } ) => ((__$$context) => (` ,
211- type : PIECE_TYPE . BEFORE ,
212- } ) ;
213-
214- pieces . push ( {
215- value : `))(__$$createChildContext(__$$context, { ${ loopItemName } , ${ loopIndexName } })))` ,
216- type : PIECE_TYPE . AFTER ,
217- } ) ;
218-
219- return pieces ;
220- }
221-
222- return next ? next ( nodeItem , scope , config ) : [ ] ;
223- }
224-
225233function generateNodeAttrForRax (
226234 this : { cfg : PluginConfig } ,
227235 attrData : { attrName : string ; attrValue : CompositeValue } ,
0 commit comments