GeezerVison is showing github.com — fix: 🐛 解决小程序环境没有 window, 而 rax 出码中却默认在 __$eval 中用到 window 的问题 · puchenglin/lowcode-engine@ce531ae
File treeExpand file treeCollapse file tree Open diff view settings demo01/expected/demo-project/src/pages/Home demo02/expected/demo-project/src/pages/Home demo03/expected/demo-project/src/pages demo04/expected/demo-project/src/pages/Home demo05/expected/demo-project/src/pages/Home demo06-jsslot/expected/demo-project/src/pages/Home demo07-newline-in-props/expected/demo-project/src/pages/Home demo08-jsslot-with-multiple-children/expected/demo-project/src/pages/Home demo09-jsslot-with-conditional-children/expected/demo-project/src/pages/Home demo10-jsslot-with-loop-children/expected/demo-project/src/pages/Home demo11-utils-name-alias/expected/demo-project/src/pages/Aaaa demo12-refs/expected/demo-project/src/pages/Home demo13-datasource-prop/expected/demo-project/src/pages/Example
demo1/expected/demo-project/src/pages/Test demo2-utils-name-alias/expected/demo-project/src/pages/Aaaa demo2/expected/demo-project/src/pages/Test demo3/expected/demo-project/src/pages/Test demo4/expected/demo-project/src/pages/Test demo5/expected/demo-project/src/pages/Test demo6-literal-condition/expected/demo-project/src/pages/Test demo7-literal-condition2/expected/demo-project/src/pages/Test demo8-datasource-prop/expected/demo-project/src/pages/Example demo9-datasource-engine/expected/demo-project/src/components/Index demo_10-jsslot/expected/demo-project/src/pages/Test demo_11-jsslot-2/expected/demo-project/src/pages/Test
plugins/jsx/__snapshots__
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/generator/ProjectBuilder.ts +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -280,6 +280,8 @@ export class ProjectBuilder implements IProjectBuilder { | 280 | 280 | postProcessors: this.postProcessors, | 281 | 281 | contextData: { | 282 | 282 | inStrictMode: this.inStrictMode, | | 283 | + tolerateEvalErrors: true, | | 284 | + evalErrorsHandler: '', | 283 | 285 | ...this.extraContextData, | 284 | 286 | }, | 285 | 287 | ...options, | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/plugins/component/rax/jsx.ts +52-44Lines changed: 52 additions & 44 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -62,6 +62,7 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) => | 62 | 62 |
| 63 | 63 | const ir = next.ir as IContainerInfo; | 64 | 64 | const rootScope = Scope.createRootScope(); | | 65 | + const { tolerateEvalErrors = true, evalErrorsHandler = '' } = next.contextData; | 65 | 66 |
| 66 | 67 | // Rax 构建到小程序的时候,不能给组件起起别名,得直接引用,故这里将所有的别名替换掉 | 67 | 68 | // 先收集下所有的 alias 的映射 | @@ -86,7 +87,9 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) => | 86 | 87 | // 3. 通过 this.xxx 能拿到的东西太多了,而且自定义的 methods 可能会无意间破坏 Rax 框架或小程序框架在页面 this 上的东东 | 87 | 88 | const customHandlers: HandlerSet<string> = { | 88 | 89 | expression(input: JSExpression, scope: IScope) { | 89 | | - return transformJsExpr(generateExpression(input, scope), scope); | | 90 | + return transformJsExpr(generateExpression(input, scope), scope, { | | 91 | + dontWrapEval: !tolerateEvalErrors, | | 92 | + }); | 90 | 93 | }, | 91 | 94 | function(input, scope: IScope) { | 92 | 95 | return transformThis2Context(input.value || 'null', scope); | @@ -138,34 +141,72 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) => | 138 | 141 | type: ChunkType.STRING, | 139 | 142 | fileType: cfg.fileType, | 140 | 143 | name: COMMON_CHUNK_NAME.CustomContent, | 141 | | - content: ` | 142 | | -
| | 144 | + content: [ | | 145 | + tolerateEvalErrors && | | 146 | + ` | 143 | 147 | function __$$eval(expr) { | 144 | 148 | try { | 145 | 149 | 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} | 152 | 152 | } | 153 | 153 | } | 154 | 154 |
| 155 | 155 | function __$$evalArray(expr) { | 156 | 156 | const res = __$$eval(expr); | 157 | 157 | return Array.isArray(res) ? res : []; | 158 | 158 | } | 159 | | -
| | 159 | + `, | | 160 | + ` | 160 | 161 | function __$$createChildContext(oldContext, ext) { | 161 | 162 | return Object.assign({}, oldContext, ext); | 162 | 163 | } | 163 | | -
| 164 | 164 | `, | | 165 | + ] | | 166 | + .filter(Boolean) | | 167 | + .join('\n'), | 165 | 168 | linkAfter: [COMMON_CHUNK_NAME.FileExport], | 166 | 169 | }); | 167 | 170 |
| 168 | 171 | 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 | + } | 169 | 210 | }; | 170 | 211 |
| 171 | 212 | return plugin; | @@ -189,39 +230,6 @@ function isImportAliasDefineChunk(chunk: ICodeChunk): chunk is ICodeChunk & { | 189 | 230 | ); | 190 | 231 | } | 191 | 232 |
| 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 | | - | 225 | 233 | function generateNodeAttrForRax( | 226 | 234 | this: { cfg: PluginConfig }, | 227 | 235 | attrData: { attrName: string; attrValue: CompositeValue }, | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/plugins/component/react/jsx.ts +23-2Lines changed: 23 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -42,12 +42,14 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) => | 42 | 42 | ...pre, | 43 | 43 | }; | 44 | 44 |
| | 45 | + const { tolerateEvalErrors = true, evalErrorsHandler = '' } = next.contextData; | | 46 | + | 45 | 47 | // 这里会将内部的一些子上下文的访问(this.xxx)转换为 __$$context.xxx 的形式 | 46 | 48 | // 与 Rax 所不同的是,这里不会将最顶层的 this 转换掉 | 47 | 49 | const customHandlers: HandlerSet<string> = { | 48 | 50 | expression(input: JSExpression, scope: IScope) { | 49 | 51 | return transformJsExpr(generateExpression(input, scope), scope, { | 50 | | - dontWrapEval: true, | | 52 | + dontWrapEval: !tolerateEvalErrors, | 51 | 53 | dontTransformThis2ContextAtRootScope: true, | 52 | 54 | }); | 53 | 55 | }, | @@ -111,7 +113,23 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) => | 111 | 113 | type: ChunkType.STRING, | 112 | 114 | fileType: cfg.fileType, | 113 | 115 | name: COMMON_CHUNK_NAME.CustomContent, | 114 | | - content: ` | | 116 | + content: [ | | 117 | + tolerateEvalErrors && | | 118 | + ` | | 119 | + function __$$eval(expr) { | | 120 | + try { | | 121 | + return expr(); | | 122 | + } catch (error) { | | 123 | + ${evalErrorsHandler} | | 124 | + } | | 125 | + } | | 126 | +
| | 127 | + function __$$evalArray(expr) { | | 128 | + const res = __$$eval(expr); | | 129 | + return Array.isArray(res) ? res : []; | | 130 | + } | | 131 | + `, | | 132 | + ` | 115 | 133 | function __$$createChildContext(oldContext, ext) { | 116 | 134 | const childContext = { | 117 | 135 | ...oldContext, | @@ -121,6 +139,9 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) => | 121 | 139 | return childContext; | 122 | 140 | } | 123 | 141 | `, | | 142 | + ] | | 143 | + .filter(Boolean) | | 144 | + .join('\n'), | 124 | 145 | linkAfter: [COMMON_CHUNK_NAME.FileExport], | 125 | 146 | }); | 126 | 147 | return next; | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/solutions/icejs.ts +3-4Lines changed: 3 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -import { IProjectBuilder } from '../types'; | | 1 | +import { IProjectBuilder, IProjectBuilderOptions } from '../types'; | 2 | 2 |
| 3 | 3 | import { createProjectBuilder } from '../generator/ProjectBuilder'; | 4 | 4 |
| @@ -22,15 +22,14 @@ import icejs from '../plugins/project/framework/icejs'; | 22 | 22 |
| 23 | 23 | import { prettier } from '../postprocessor'; | 24 | 24 |
| 25 | | -export type IceJsProjectBuilderOptions = { | 26 | | - inStrictMode?: boolean; | 27 | | -}; | | 25 | +export interface IceJsProjectBuilderOptions extends IProjectBuilderOptions {} | 28 | 26 |
| 29 | 27 | export default function createIceJsProjectBuilder( | 30 | 28 | options?: IceJsProjectBuilderOptions, | 31 | 29 | ): IProjectBuilder { | 32 | 30 | return createProjectBuilder({ | 33 | 31 | inStrictMode: options?.inStrictMode, | | 32 | + extraContextData: { ...options }, | 34 | 33 | template: icejs.template, | 35 | 34 | plugins: { | 36 | 35 | components: [ | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/solutions/rax-app.ts +8-2Lines changed: 8 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -import { IProjectBuilder } from '../types'; | | 1 | +import { IProjectBuilder, IProjectBuilderOptions } from '../types'; | 2 | 2 |
| 3 | 3 | import { createProjectBuilder } from '../generator/ProjectBuilder'; | 4 | 4 |
| @@ -22,8 +22,14 @@ import raxApp from '../plugins/project/framework/rax'; | 22 | 22 | import { prettier } from '../postprocessor'; | 23 | 23 | import { RaxFrameworkOptions } from '../plugins/project/framework/rax/types/RaxFrameworkOptions'; | 24 | 24 |
| 25 | | -export default function createRaxProjectBuilder(options?: RaxFrameworkOptions): IProjectBuilder { | | 25 | +export interface RaxProjectBuilderOptions extends IProjectBuilderOptions, RaxFrameworkOptions {} | | 26 | + | | 27 | +export default function createRaxProjectBuilder( | | 28 | + options?: RaxProjectBuilderOptions, | | 29 | +): IProjectBuilder { | 26 | 30 | return createProjectBuilder({ | | 31 | + inStrictMode: options?.inStrictMode, | | 32 | + extraContextData: { ...options }, | 27 | 33 | template: raxApp.template, | 28 | 34 | plugins: { | 29 | 35 | components: [ | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/types/core.ts +28-4Lines changed: 28 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -63,10 +63,7 @@ export interface ICodeStruct extends IBaseCodeStruct { | 63 | 63 | } | 64 | 64 |
| 65 | 65 | /** 上下文数据,用来在插件之间共享一些数据 */ | 66 | | -export interface IContextData { | 67 | | - /** 是否处于严格模式 */ | 68 | | - inStrictMode?: boolean; | 69 | | - | | 66 | +export interface IContextData extends IProjectBuilderOptions { | 70 | 67 | /** 是否使用了 Ref 的 API (this.$/this.$$) */ | 71 | 68 | useRefApi?: boolean; | 72 | 69 |
| @@ -139,6 +136,33 @@ export interface IProjectPlugins { | 139 | 136 | [slotName: string]: BuilderComponentPlugin[]; | 140 | 137 | } | 141 | 138 |
| | 139 | +export interface IProjectBuilderOptions { | | 140 | + /** 是否处于严格模式(默认: 否) */ | | 141 | + inStrictMode?: boolean; | | 142 | + | | 143 | + /** | | 144 | + * 是否要容忍对 JSExpression 求值时的异常 | | 145 | + * 默认:true | | 146 | + * 注: 如果容忍异常,则会在求值时包裹 try-catch 块, | | 147 | + * catch 到异常时默认会抛出一个 CustomEvent 事件里面包含异常信息和求值的表达式 | | 148 | + */ | | 149 | + tolerateEvalErrors?: boolean; | | 150 | + | | 151 | + /** | | 152 | + * 容忍异常的时候的的错误处理语句块 | | 153 | + * 默认: 无 | | 154 | + * 您可以设置为一个语句块,比如: | | 155 | + * window.dispatchEvent(new CustomEvent('lowcode-eval-error', { error, expr })) | | 156 | + * | | 157 | + * 一般可以结合埋点监控模块用来监控求值异常 | | 158 | + * | | 159 | + * 其中: | | 160 | + * - error: 异常信息 | | 161 | + * - expr: 求值的表达式 | | 162 | + */ | | 163 | + evalErrorsHandler?: string; | | 164 | +} | | 165 | + | 142 | 166 | export interface IProjectBuilder { | 143 | 167 | generateProject: (schema: ProjectSchema | string) => Promise<ResultDir>; | 144 | 168 | } | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/test-cases/rax-app/demo01/expected/demo-project/src/pages/Home/index.jsx +1-7Lines changed: 1 addition & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -129,13 +129,7 @@ export default Home$$Page; | 129 | 129 | function __$$eval(expr) { | 130 | 130 | try { | 131 | 131 | return expr(); | 132 | | - } catch (err) { | 133 | | - try { | 134 | | - if (window.handleEvalError) { | 135 | | - window.handleEvalError('Failed to evaluate: ', expr, err); | 136 | | - } | 137 | | - } catch (e) {} | 138 | | - } | | 132 | + } catch (error) {} | 139 | 133 | } | 140 | 134 |
| 141 | 135 | function __$$evalArray(expr) { | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/test-cases/rax-app/demo02/expected/demo-project/src/pages/Home/index.jsx +1-7Lines changed: 1 addition & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -336,13 +336,7 @@ export default Home$$Page; | 336 | 336 | function __$$eval(expr) { | 337 | 337 | try { | 338 | 338 | return expr(); | 339 | | - } catch (err) { | 340 | | - try { | 341 | | - if (window.handleEvalError) { | 342 | | - window.handleEvalError('Failed to evaluate: ', expr, err); | 343 | | - } | 344 | | - } catch (e) {} | 345 | | - } | | 339 | + } catch (error) {} | 346 | 340 | } | 347 | 341 |
| 348 | 342 | function __$$evalArray(expr) { | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/test-cases/rax-app/demo03/expected/demo-project/src/pages/Detail/index.jsx +1-7Lines changed: 1 addition & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -138,13 +138,7 @@ export default Detail$$Page; | 138 | 138 | function __$$eval(expr) { | 139 | 139 | try { | 140 | 140 | return expr(); | 141 | | - } catch (err) { | 142 | | - try { | 143 | | - if (window.handleEvalError) { | 144 | | - window.handleEvalError('Failed to evaluate: ', expr, err); | 145 | | - } | 146 | | - } catch (e) {} | 147 | | - } | | 141 | + } catch (error) {} | 148 | 142 | } | 149 | 143 |
| 150 | 144 | function __$$evalArray(expr) { | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/test-cases/rax-app/demo03/expected/demo-project/src/pages/Home/index.jsx +1-7Lines changed: 1 addition & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -138,13 +138,7 @@ export default Home$$Page; | 138 | 138 | function __$$eval(expr) { | 139 | 139 | try { | 140 | 140 | return expr(); | 141 | | - } catch (err) { | 142 | | - try { | 143 | | - if (window.handleEvalError) { | 144 | | - window.handleEvalError('Failed to evaluate: ', expr, err); | 145 | | - } | 146 | | - } catch (e) {} | 147 | | - } | | 141 | + } catch (error) {} | 148 | 142 | } | 149 | 143 |
| 150 | 144 | function __$$evalArray(expr) { | |
|