GeezerVison is showing github.com — feat: 支持 esModule#preferClassProperty 配置项 · Juny0117/lowcode-engine@574e348
File treeExpand file treeCollapse file tree Open diff view settings modules/code-generator/src
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 +19Lines changed: 19 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -24,22 +24,31 @@ interface IModuleInfo { | 24 | 24 | } | 25 | 25 |
| 26 | 26 | export interface ProjectBuilderInitOptions { | | 27 | + | 27 | 28 | /** 项目模板 */ | 28 | 29 | template: IProjectTemplate; | | 30 | + | 29 | 31 | /** 项目插件 */ | 30 | 32 | plugins: IProjectPlugins; | | 33 | + | 31 | 34 | /** 模块后置处理器 */ | 32 | 35 | postProcessors: PostProcessor[]; | | 36 | + | 33 | 37 | /** Schema 解析器 */ | 34 | 38 | schemaParser?: ISchemaParser; | | 39 | + | 35 | 40 | /** 项目级别的前置处理器 */ | 36 | 41 | projectPreProcessors?: ProjectPreProcessor[]; | | 42 | + | 37 | 43 | /** 项目级别的后置处理器 */ | 38 | 44 | projectPostProcessors?: ProjectPostProcessor[]; | | 45 | + | 39 | 46 | /** 是否处于严格模式 */ | 40 | 47 | inStrictMode?: boolean; | | 48 | + | 41 | 49 | /** 一些额外的上下文数据 */ | 42 | 50 | extraContextData?: Record<string, unknown>; | | 51 | + | 43 | 52 | /** | 44 | 53 | * Hook which is used to customize original options, we can reorder/add/remove plugins/processors | 45 | 54 | * of the existing solution. | @@ -264,6 +273,16 @@ export class ProjectBuilder implements IProjectBuilder { | 264 | 273 | } | 265 | 274 |
| 266 | 275 | // TODO: 更多 slots 的处理??是不是可以考虑把 template 中所有的 slots 都处理下? | | 276 | + // const whitelistSlotNames = [ | | 277 | + // 'router', | | 278 | + // 'entry', | | 279 | + // 'appConfig', | | 280 | + // 'buildConfig', | | 281 | + // 'router', | | 282 | + // ]; | | 283 | + // Object.keys(this.template.slots).forEach((slotName: string) => { | | 284 | + | | 285 | + // }); | 267 | 286 |
| 268 | 287 | // Post Process | 269 | 288 | const isSingleComponent = parseResult?.project?.projectRemark?.isSingleComponent; | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/plugins/common/esmodule.ts +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -443,6 +443,7 @@ function buildPackageImport( | 443 | 443 | export interface PluginConfig { | 444 | 444 | fileType?: string; // 导出的文件类型 | 445 | 445 | useAliasName?: boolean; // 是否使用 componentName 重命名组件 identifier | | 446 | + filter?: (deps: IDependency[]) => IDependency[]; // 支持过滤能力 | 446 | 447 | } | 447 | 448 |
| 448 | 449 | const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?: PluginConfig) => { | @@ -460,7 +461,8 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?: Plu | 460 | 461 | const ir = next.ir as IWithDependency; | 461 | 462 |
| 462 | 463 | if (ir && ir.deps && ir.deps.length > 0) { | 463 | | - const packs = groupDepsByPack(ir.deps); | | 464 | + const deps = cfg.filter ? cfg.filter(ir.deps) : ir.deps; | | 465 | + const packs = groupDepsByPack(deps); | 464 | 466 |
| 465 | 467 | Object.keys(packs).forEach((pkg) => { | 466 | 468 | const chunks = buildPackageImport(pkg, packs[pkg], cfg.fileType, cfg.useAliasName); | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/plugins/component/react/containerInjectUtils.ts +22-7Lines changed: 22 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -16,6 +16,9 @@ import { | 16 | 16 |
| 17 | 17 | export interface PluginConfig { | 18 | 18 | fileType: string; | | 19 | + | | 20 | + /** prefer using class property to define utils */ | | 21 | + preferClassProperty?: boolean; | 19 | 22 | } | 20 | 23 |
| 21 | 24 | const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) => { | @@ -57,13 +60,25 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) => | 57 | 60 | linkAfter: [COMMON_CHUNK_NAME.ExternalDepsImport], | 58 | 61 | }); | 59 | 62 |
| 60 | | - next.chunks.push({ | 61 | | - type: ChunkType.STRING, | 62 | | - fileType: cfg.fileType, | 63 | | - name: CLASS_DEFINE_CHUNK_NAME.ConstructorContent, | 64 | | - content: 'this.utils = utils;', | 65 | | - linkAfter: [CLASS_DEFINE_CHUNK_NAME.ConstructorStart], | 66 | | - }); | | 63 | + if (cfg.preferClassProperty) { | | 64 | + // mode: class property | | 65 | + next.chunks.push({ | | 66 | + type: ChunkType.STRING, | | 67 | + fileType: cfg.fileType, | | 68 | + name: CLASS_DEFINE_CHUNK_NAME.InsVar, | | 69 | + content: 'utils = utils;', | | 70 | + linkAfter: [...DEFAULT_LINK_AFTER[CLASS_DEFINE_CHUNK_NAME.InsVar]], | | 71 | + }); | | 72 | + } else { | | 73 | + // mode: assign in constructor | | 74 | + next.chunks.push({ | | 75 | + type: ChunkType.STRING, | | 76 | + fileType: cfg.fileType, | | 77 | + name: CLASS_DEFINE_CHUNK_NAME.ConstructorContent, | | 78 | + content: 'this.utils = utils;', | | 79 | + linkAfter: [CLASS_DEFINE_CHUNK_NAME.ConstructorStart], | | 80 | + }); | | 81 | + } | 67 | 82 |
| 68 | 83 | if (useRef) { | 69 | 84 | next.chunks.push({ | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/postprocessor/prettier/index.ts +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -22,6 +22,8 @@ const factory: PostProcessorFactory<ProcessorConfig> = (config?: ProcessorConfig | 22 | 22 | let parser: prettier.BuiltInParserName | any; | 23 | 23 | if (fileType === 'js' || fileType === 'jsx') { | 24 | 24 | parser = 'babel'; | | 25 | + } else if (fileType === 'json') { | | 26 | + parser = 'json-stringify'; | 25 | 27 | } else if (PARSERS.indexOf(fileType) >= 0) { | 26 | 28 | parser = fileType; | 27 | 29 | } else if (cfg.customFileTypeParser[fileType]) { | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/types/core.ts +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -22,8 +22,10 @@ export enum FileType { | 22 | 22 | LESS = 'less', | 23 | 23 | HTML = 'html', | 24 | 24 | JS = 'js', | | 25 | + MJS = 'mjs', | 25 | 26 | JSX = 'jsx', | 26 | 27 | TS = 'ts', | | 28 | + MTS = 'mts', | 27 | 29 | TSX = 'tsx', | 28 | 30 | JSON = 'json', | 29 | 31 | MD = 'md', | @@ -168,6 +170,7 @@ export interface IProjectBuilderOptions { | 168 | 170 | * - expr: 求值的表达式 | 169 | 171 | */ | 170 | 172 | evalErrorsHandler?: string; | | 173 | + | 171 | 174 | /** | 172 | 175 | * Hook which is used to customize original options, we can reorder/add/remove plugins/processors | 173 | 176 | * of the existing solution. | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/types/intermediate.ts +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -42,6 +42,7 @@ export interface IRouterInfo extends IWithDependency { | 42 | 42 | * project's remarks | 43 | 43 | */ | 44 | 44 | export interface ProjectRemark { | | 45 | + | 45 | 46 | /** if current project only contain one container which type is `Component` */ | 46 | 47 | isSingleComponent?: boolean; | 47 | 48 | } | |
|