From 2b2a9c6706333d777bf87a06ec67f15f10294de3 Mon Sep 17 00:00:00 2001 From: roymondchen Date: Sun, 1 Oct 2023 12:18:15 +0800 Subject: [PATCH] =?UTF-8?q?feat(cli):=20=E6=96=B0=E5=A2=9EdatasoucreSuperC?= =?UTF-8?q?lass=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/cli/src/types.ts | 8 ++++--- packages/cli/src/utils/resolveAppPackages.ts | 24 +++++++++++++++----- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/packages/cli/src/types.ts b/packages/cli/src/types.ts index bfd90658..e7ba9643 100644 --- a/packages/cli/src/types.ts +++ b/packages/cli/src/types.ts @@ -70,10 +70,12 @@ export interface UserConfig { /** 组件目录或者npm包名 */ packages: (string | Record)[]; /** 组件文件后缀名,例如vue文件为.vue,tsx文件为.tsx,普通js文件则为.js */ - componentFileAffix: string; - cleanTemp: boolean; + componentFileAffix?: string; + /** 继承这些类的类将被判定为数据源,默认有['DataSource', 'HttpDataSource'] */ + datasoucreSuperClass?: string[]; + cleanTemp?: boolean; /** 入口文件是否生成为 ts 格式 */ - useTs: boolean; + useTs?: boolean; /** npm 配置,用于当packages配置有npm包名时,可以自动安装npm包 */ npmConfig?: NpmConfig; /** 是否使用import()加载组件 */ diff --git a/packages/cli/src/utils/resolveAppPackages.ts b/packages/cli/src/utils/resolveAppPackages.ts index c18f0e66..5ec32de4 100644 --- a/packages/cli/src/utils/resolveAppPackages.ts +++ b/packages/cli/src/utils/resolveAppPackages.ts @@ -27,6 +27,7 @@ interface TypeAssertionOption { ast: Ast; indexPath: string; componentFileAffix?: string; + datasoucreSuperClass?: string[]; } const isFile = (filePath: string) => fs.existsSync(filePath) && fs.lstatSync(filePath).isFile(); @@ -76,7 +77,12 @@ const npmInstall = function (dependencies: Record, cwd: string, * @param {String} indexPath * @return {Object} { type: '', imports: [] } 返回传入组件的类型。如果是组件包,imports 中包含所有子组件的入口文件路径 */ -const typeAssertion = function ({ ast, indexPath, componentFileAffix }: TypeAssertionOption): TypeAssertion { +const typeAssertion = function ({ + ast, + indexPath, + componentFileAffix, + datasoucreSuperClass, +}: TypeAssertionOption): TypeAssertion { const n = recast.types.namedTypes; const result: TypeAssertion = { @@ -102,7 +108,12 @@ const typeAssertion = function ({ ast, indexPath, componentFileAffix }: TypeAsse if (isFile(defaultFile)) { const defaultCode = fs.readFileSync(defaultFile, { encoding: 'utf-8', flag: 'r' }); const ast = recast.parse(defaultCode, { parser: require('recast/parsers/typescript') }); - if (isDatasource(ast.program.body.find((node: any) => node.type === 'ExportDefaultDeclaration')?.declaration)) { + if ( + isDatasource( + datasoucreSuperClass, + ast.program.body.find((node: any) => node.type === 'ExportDefaultDeclaration')?.declaration, + ) + ) { result.type = PackageType.DATASOURCE; return false; } @@ -154,7 +165,7 @@ const typeAssertion = function ({ ast, indexPath, componentFileAffix }: TypeAsse } } - if (isDatasource(exportDefaultClass)) { + if (isDatasource(datasoucreSuperClass, exportDefaultClass)) { result.type = PackageType.DATASOURCE; } @@ -217,7 +228,8 @@ const isPlugin = function (properties: any[]) { return !!match; }; -const isDatasource = (exportDefaultClass: any) => exportDefaultClass?.superClass?.name === 'DataSource'; +const isDatasource = (datasoucreSuperClass: string[] = [], exportDefaultClass: any) => + [...datasoucreSuperClass, 'DataSource', 'HttpDataSource'].includes(exportDefaultClass?.superClass?.name); const getComponentPackageImports = function ({ result, @@ -428,7 +440,7 @@ const getDependencies = (dependencies: Record, packagePath: stri const setPackages = (packages: ModuleMainFilePath, app: App, packagePath: string, key?: string) => { const { options } = app; - const { temp, source, componentFileAffix } = options; + const { temp, source, componentFileAffix, datasoucreSuperClass } = options; let { name: moduleName } = splitNameVersion(packagePath); @@ -455,7 +467,7 @@ const setPackages = (packages: ModuleMainFilePath, app: App, packagePath: string const indexCode = fs.readFileSync(indexPath, { encoding: 'utf-8', flag: 'r' }); const ast: Ast = recast.parse(indexCode, { parser: require('recast/parsers/typescript') }); - const result = typeAssertion({ ast, indexPath, componentFileAffix }); + const result = typeAssertion({ ast, indexPath, componentFileAffix, datasoucreSuperClass }); // 组件&插件&数据源包 if (result.type === PackageType.COMPONENT_PACKAGE) {