GeezerVison is showing github.com — feat: 相对路径生成动态化 & 解决部分插件 ts 声明问题 (#1618) · zhYAO/lowcode-engine@b3a2991
File treeExpand file treeCollapse file tree Open diff view settings rax-app/demo02/expected/demo-project/src/pages/Home demo2-utils-name-alias/expected/demo-project demo3/expected/demo-project demo4/expected/demo-project demo5/expected/demo-project demo7-literal-condition2/expected/demo-project demo8-datasource-prop/expected/demo-project demo9-datasource-engine/expected/demo-project demo_10-jsslot/expected/demo-project demo_11-jsslot-2/expected/demo-project
react-module/demo1/expected/demo-project
postprocessor/__snapshots__
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/example-schema.json +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 71 | 71 | }, | 72 | 72 | "lifeCycles": { | 73 | 73 | "componentDidMount": { | 74 | | - "type": "JSExpression", | | 74 | + "type": "JSFunction", | 75 | 75 | "value": "function() { console.log('componentDidMount'); }" | 76 | 76 | } | 77 | 77 | }, | | 91 | 91 | "isSync": true | 92 | 92 | }, | 93 | 93 | "dataHandler": { | 94 | | - "type": "JSExpression", | | 94 | + "type": "JSFunction", | 95 | 95 | "value": "function (response) {\nif (!response.data.success){\n throw new Error(response.data.message);\n }\n return response.data.data;\n}" | 96 | 96 | } | 97 | 97 | }, | | 105 | 105 | "isSync": true | 106 | 106 | }, | 107 | 107 | "dataHandler": { | 108 | | - "type": "JSExpression", | | 108 | + "type": "JSFunction", | 109 | 109 | "value": "function (response) {\nif (!response.data.success){\n throw new Error(response.data.message);\n }\n return response.data.data.result;\n}" | 110 | 110 | } | 111 | 111 | } | 112 | 112 | ], | 113 | 113 | "dataHandler": { | 114 | | - "type": "JSExpression", | | 114 | + "type": "JSFunction", | 115 | 115 | "value": "function (dataMap) {\n console.info(\"All datasources loaded:\", dataMap);\n}" | 116 | 116 | } | 117 | 117 | }, | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/example-schema.json5 +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 71 | 71 | }, | 72 | 72 | lifeCycles: { | 73 | 73 | componentDidMount: { | 74 | | - type: 'JSExpression', | | 74 | + type: 'JSFunction', | 75 | 75 | value: "function() { console.log('componentDidMount'); }", | 76 | 76 | }, | 77 | 77 | }, | | 91 | 91 | isSync: true, | 92 | 92 | }, | 93 | 93 | dataHandler: { | 94 | | - type: 'JSExpression', | | 94 | + type: 'JSFunction', | 95 | 95 | value: 'function (response) {\nif (!response.data.success){\n throw new Error(response.data.message);\n }\n return response.data.data;\n}', | 96 | 96 | }, | 97 | 97 | }, | | 105 | 105 | isSync: true, | 106 | 106 | }, | 107 | 107 | dataHandler: { | 108 | | - type: 'JSExpression', | | 108 | + type: 'JSFunction', | 109 | 109 | value: 'function (response) {\nif (!response.data.success){\n throw new Error(response.data.message);\n }\n return response.data.data.result;\n}', | 110 | 110 | }, | 111 | 111 | }, | 112 | 112 | ], | 113 | 113 | dataHandler: { | 114 | | - type: 'JSExpression', | | 114 | + type: 'JSFunction', | 115 | 115 | value: 'function (dataMap) {\n console.info("All datasources loaded:", dataMap);\n}', | 116 | 116 | }, | 117 | 117 | }, | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/generator/ProjectBuilder.ts +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -135,6 +135,7 @@ export class ProjectBuilder implements IProjectBuilder { | 135 | 135 | const builders = this.createModuleBuilders({ | 136 | 136 | extraContextData: { | 137 | 137 | projectRemark: parseResult?.project?.projectRemark, | | 138 | + template: this.template, | 138 | 139 | }, | 139 | 140 | }); | 140 | 141 | // Generator Code module | @@ -328,7 +329,7 @@ export class ProjectBuilder implements IProjectBuilder { | 328 | 329 | // template: this.template, | 329 | 330 | inStrictMode: this.inStrictMode, | 330 | 331 | tolerateEvalErrors: true, | 331 | | - evalErrorsHandler: 'console.error(error)', | | 332 | + evalErrorsHandler: '', | 332 | 333 | ...this.extraContextData, | 333 | 334 | ...extraContextData, | 334 | 335 | }, | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/index.ts +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | /** | 2 | 2 | * 低代码引擎的出码模块,负责将编排产出的 Schema 转换成实际可执行的代码。 | 3 | | - * 注意:为了保持 API 的稳定性, 这里所有导出的 API 均要显式命名方式导出 | | 3 | + * 注意:为了保持 API 的稳定性,这里所有导出的 API 均要显式命名方式导出 | 4 | 4 | * (即用 export { xxx } from 'xx' 的方式,不要直接 export * from 'xxx') | 5 | 5 | * 而且所有导出的 API 务必在 tests/public 中编写单元测试 | 6 | 6 | */ | @@ -51,6 +51,7 @@ export default { | 51 | 51 | }, | 52 | 52 | plugins: { | 53 | 53 | common: { | | 54 | + | 54 | 55 | /** | 55 | 56 | * 处理 ES Module | 56 | 57 | * @deprecated please use esModule | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/plugins/component/react/containerInitState.ts +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -13,12 +13,12 @@ import { | 13 | 13 | } from '../../../types'; | 14 | 14 |
| 15 | 15 | export interface PluginConfig { | 16 | | - fileType: string; | | 16 | + fileType?: string; | 17 | 17 | implementType: 'inConstructor' | 'insMember' | 'hooks'; | 18 | 18 | } | 19 | 19 |
| 20 | 20 | const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) => { | 21 | | - const cfg: PluginConfig = { | | 21 | + const cfg: PluginConfig & { fileType: string } = { | 22 | 22 | fileType: FileType.JSX, | 23 | 23 | implementType: 'inConstructor', | 24 | 24 | ...config, | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/plugins/component/react/containerInjectI18n.ts +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -11,6 +11,7 @@ import { | 11 | 11 | FileType, | 12 | 12 | ICodeStruct, | 13 | 13 | } from '../../../types'; | | 14 | +import { getSlotRelativePath } from '../../../utils/pathHelper'; | 14 | 15 |
| 15 | 16 | export interface PluginConfig { | 16 | 17 | fileType: string; | @@ -31,9 +32,8 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) => | 31 | 32 | type: ChunkType.STRING, | 32 | 33 | fileType: cfg.fileType, | 33 | 34 | name: COMMON_CHUNK_NAME.InternalDepsImport, | 34 | | - // TODO: 下面这个路径有没有更好的方式来获取?而非写死 | 35 | 35 | content: ` | 36 | | - import * as __$$i18n from '../../i18n'; | | 36 | + import * as __$$i18n from '${getSlotRelativePath({ contextData: next.contextData, from: 'components', to: 'i18n' })}'; | 37 | 37 | `, | 38 | 38 | linkAfter: [COMMON_CHUNK_NAME.ExternalDepsImport], | 39 | 39 | }); | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/plugins/component/react/containerInjectUtils.ts +4-18Lines changed: 4 additions & 18 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -11,18 +11,18 @@ import { | 11 | 11 | FileType, | 12 | 12 | ICodeStruct, | 13 | 13 | IContainerInfo, | 14 | | - IProjectTemplate, | 15 | 14 | } from '../../../types'; | | 15 | +import { getSlotRelativePath } from '../../../utils/pathHelper'; | 16 | 16 |
| 17 | 17 | export interface PluginConfig { | 18 | | - fileType: string; | | 18 | + fileType?: string; | 19 | 19 |
| 20 | 20 | /** prefer using class property to define utils */ | 21 | 21 | preferClassProperty?: boolean; | 22 | 22 | } | 23 | 23 |
| 24 | 24 | const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) => { | 25 | | - const cfg: PluginConfig = { | | 25 | + const cfg: PluginConfig & { fileType: string } = { | 26 | 26 | fileType: FileType.JSX, | 27 | 27 | ...config, | 28 | 28 | }; | @@ -36,26 +36,12 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) => | 36 | 36 | next.contextData.useRefApi = true; | 37 | 37 | const useRef = !!ir.analyzeResult?.isUsingRef; | 38 | 38 |
| 39 | | - // const isSingleComponent = next.contextData?.projectRemark?.isSingleComponent; | 40 | | - // const template = next.contextData?.template; | 41 | | - | 42 | | - // function getRelativeUtilsPath(template: IProjectTemplate, isSingleComponent: boolean) { | 43 | | - // let relativeUtilsPath = '../../utils'; | 44 | | - // const utilsPath = template.slots.utils.path; | 45 | | - // if (ir.containerType === 'Component') { | 46 | | - // // TODO: isSingleComponent | 47 | | - // relativeUtilsPath = getRelativePath(template.slots.components.path.join('/'), utilsPath.join('/')); | 48 | | - // } | 49 | | - // return relativeUtilsPath; | 50 | | - // } | 51 | | - | 52 | 39 | next.chunks.push({ | 53 | 40 | type: ChunkType.STRING, | 54 | 41 | fileType: cfg.fileType, | 55 | 42 | name: COMMON_CHUNK_NAME.InternalDepsImport, | 56 | | - // TODO: 下面这个路径有没有更好的方式来获取?而非写死 | 57 | 43 | content: ` | 58 | | - import utils${useRef ? ', { RefsManager }' : ''} from '../../utils'; | | 44 | + import utils${useRef ? ', { RefsManager }' : ''} from '${getSlotRelativePath({ contextData: next.contextData, from: 'components', to: 'utils' })}'; | 59 | 45 | `, | 60 | 46 | linkAfter: [COMMON_CHUNK_NAME.ExternalDepsImport], | 61 | 47 | }); | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/plugins/project/i18n.ts +1-2Lines changed: 1 addition & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -25,8 +25,7 @@ const pluginFactory: BuilderComponentPluginFactory<unknown> = () => { | 25 | 25 | content: ` | 26 | 26 | const i18nConfig = ${i18nStr}; | 27 | 27 |
| 28 | | - // let locale = typeof navigator === 'object' && typeof navigator.language === 'string' ? navigator.language : 'zh-CN'; | 29 | | - let locale = 'zh-CN'; | | 28 | + let locale = typeof navigator === 'object' && typeof navigator.language === 'string' ? navigator.language : 'zh-CN'; | 30 | 29 |
| 31 | 30 | const getLocale = () => locale; | 32 | 31 |
| |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/utils/expressionParser.ts +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -271,7 +271,7 @@ export function parseExpressionConvertThis2Context( | 271 | 271 |
| 272 | 272 | const localVariablesSet = new Set(localVariables); | 273 | 273 |
| 274 | | - let thisScopeLevel = -1; | | 274 | + let thisScopeLevel = CROSS_THIS_SCOPE_TYPE_NODE[exprAst.type] ? -1 : 0; | 275 | 275 | traverse(fileAst, { | 276 | 276 | enter(path) { | 277 | 277 | if (CROSS_THIS_SCOPE_TYPE_NODE[path.node.type]) { | @@ -303,7 +303,7 @@ export function parseExpressionConvertThis2Context( | 303 | 303 | } | 304 | 304 |
| 305 | 305 | // 替换 this (只在顶层替换) | 306 | | - if (thisScopeLevel === 0) { | | 306 | + if (thisScopeLevel <= 0) { | 307 | 307 | obj.replaceWith(t.identifier(contextName)); | 308 | 308 | } | 309 | 309 | }, | @@ -317,7 +317,7 @@ export function parseExpressionConvertThis2Context( | 317 | 317 | return; | 318 | 318 | } | 319 | 319 |
| 320 | | - if (thisScopeLevel === 0) { | | 320 | + if (thisScopeLevel <= 0) { | 321 | 321 | path.replaceWith(t.identifier(contextName)); | 322 | 322 | } | 323 | 323 | }, | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/utils/index.ts +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,6 +12,7 @@ import * as version from './version'; | 12 | 12 | import * as scope from './Scope'; | 13 | 13 | import * as expressionParser from './expressionParser'; | 14 | 14 | import * as dataSource from './dataSource'; | | 15 | +import * as pathHelper from './pathHelper'; | 15 | 16 |
| 16 | 17 | export { | 17 | 18 | common, | @@ -27,4 +28,5 @@ export { | 27 | 28 | scope, | 28 | 29 | expressionParser, | 29 | 30 | dataSource, | | 31 | + pathHelper, | 30 | 32 | }; |
|