feat: 🎸 出码模块的 schema 相关的类型统一都改成引用 @ali/lowcode-types 中的,与设计器一致

This commit is contained in:
牧毅 2020-08-13 00:21:44 +08:00
parent adcfacb233
commit 27a9800705
8 changed files with 48 additions and 113 deletions

View File

@ -8,12 +8,12 @@ import { SUPPORT_SCHEMA_VERSION_LIST } from '../const';
import { handleChildren } from '../utils/nodeToJSX'; import { handleChildren } from '../utils/nodeToJSX';
import { import {
ChildNodeType, NodeData,
CodeGeneratorError, CodeGeneratorError,
CompatibilityError, CompatibilityError,
DependencyType, DependencyType,
IBasicSchema, IBasicSchema,
IComponentNodeItem, NodeSchema,
IContainerInfo, IContainerInfo,
IContainerNodeItem, IContainerNodeItem,
IDependency, IDependency,
@ -24,7 +24,7 @@ import {
IParseResult, IParseResult,
IProjectSchema, IProjectSchema,
ISchemaParser, ISchemaParser,
IUtilItem, UtilItem,
} from '../types'; } from '../types';
const defaultContainer: IContainerInfo = { const defaultContainer: IContainerInfo = {
@ -85,7 +85,7 @@ class SchemaParser implements ISchemaParser {
// 整个 schema 描述一个容器,且无根节点定义 // 整个 schema 描述一个容器,且无根节点定义
const container: IContainerInfo = { const container: IContainerInfo = {
...defaultContainer, ...defaultContainer,
children: schema.componentsTree as IComponentNodeItem[], children: schema.componentsTree as NodeSchema[],
}; };
containers = [container]; containers = [container];
} else { } else {
@ -167,7 +167,7 @@ class SchemaParser implements ISchemaParser {
.filter((dep) => !!dep); .filter((dep) => !!dep);
// 分析 Utils 依赖 // 分析 Utils 依赖
let utils: IUtilItem[]; let utils: UtilItem[];
if (schema.utils) { if (schema.utils) {
utils = schema.utils; utils = schema.utils;
utilsDeps = schema.utils.filter((u) => u.type !== 'function').map((u) => u.content as IExternalDependency); utilsDeps = schema.utils.filter((u) => u.type !== 'function').map((u) => u.content as IExternalDependency);
@ -198,9 +198,9 @@ class SchemaParser implements ISchemaParser {
}; };
} }
public getComponentNames(children: ChildNodeType): string[] { public getComponentNames(children: NodeData | NodeData[]): string[] {
return handleChildren(children, { return handleChildren(children, {
node: (i: IComponentNodeItem) => [i.componentName], node: (i: NodeSchema) => [i.componentName],
}); });
} }
} }

View File

@ -12,7 +12,7 @@ import {
ICodeChunk, ICodeChunk,
ICodeStruct, ICodeStruct,
IContainerInfo, IContainerInfo,
IJSExpression, JSExpression,
} from '../../../types'; } from '../../../types';
type PluginConfig = { type PluginConfig = {
@ -50,7 +50,7 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) =>
// fileType: cfg.fileType, // fileType: cfg.fileType,
// name: CLASS_DEFINE_CHUNK_NAME.ConstructorContent, // name: CLASS_DEFINE_CHUNK_NAME.ConstructorContent,
// content: getFuncExprBody( // content: getFuncExprBody(
// (lifeCycles[lifeCycleName] as IJSExpression).value, // (lifeCycles[lifeCycleName] as JSExpression).value,
// ), // ),
// linkAfter: [...DEFAULT_LINK_AFTER[CLASS_DEFINE_CHUNK_NAME.ConstructorStart]], // linkAfter: [...DEFAULT_LINK_AFTER[CLASS_DEFINE_CHUNK_NAME.ConstructorStart]],
// }; // };
@ -61,7 +61,7 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) =>
// fileType: cfg.fileType, // fileType: cfg.fileType,
// name: RAX_CHUNK_NAME.ClassRenderPre, // name: RAX_CHUNK_NAME.ClassRenderPre,
// content: getFuncExprBody( // content: getFuncExprBody(
// (lifeCycles[lifeCycleName] as IJSExpression).value, // (lifeCycles[lifeCycleName] as JSExpression).value,
// ), // ),
// linkAfter: [RAX_CHUNK_NAME.ClassRenderStart], // linkAfter: [RAX_CHUNK_NAME.ClassRenderStart],
// }; // };
@ -73,7 +73,7 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) =>
// name: CLASS_DEFINE_CHUNK_NAME.InsMethod, // name: CLASS_DEFINE_CHUNK_NAME.InsMethod,
// content: transformFuncExpr2MethodMember( // content: transformFuncExpr2MethodMember(
// exportName, // exportName,
// (lifeCycles[lifeCycleName] as IJSExpression).value, // (lifeCycles[lifeCycleName] as JSExpression).value,
// ), // ),
// linkAfter: [...DEFAULT_LINK_AFTER[CLASS_DEFINE_CHUNK_NAME.InsMethod]], // linkAfter: [...DEFAULT_LINK_AFTER[CLASS_DEFINE_CHUNK_NAME.InsMethod]],
// }; // };

View File

@ -1,10 +1,7 @@
import { CLASS_DEFINE_CHUNK_NAME, DEFAULT_LINK_AFTER } from '../../../const/generator'; import { CLASS_DEFINE_CHUNK_NAME, DEFAULT_LINK_AFTER } from '../../../const/generator';
import { REACT_CHUNK_NAME } from './const'; import { REACT_CHUNK_NAME } from './const';
import { import { getFuncExprBody, transformFuncExpr2MethodMember } from '../../../utils/jsExpression';
getFuncExprBody,
transformFuncExpr2MethodMember,
} from '../../../utils/jsExpression';
import { import {
BuilderComponentPlugin, BuilderComponentPlugin,
@ -15,14 +12,14 @@ import {
ICodeChunk, ICodeChunk,
ICodeStruct, ICodeStruct,
IContainerInfo, IContainerInfo,
IJSExpression, JSExpression,
} from '../../../types'; } from '../../../types';
type PluginConfig = { type PluginConfig = {
fileType: string; fileType: string;
exportNameMapping: Record<string, string>; exportNameMapping: Record<string, string>;
normalizeNameMapping: Record<string, string>; normalizeNameMapping: Record<string, string>;
} };
const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) => { const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) => {
const cfg: PluginConfig = { const cfg: PluginConfig = {
@ -41,7 +38,7 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) =>
if (ir.lifeCycles) { if (ir.lifeCycles) {
const lifeCycles = ir.lifeCycles; const lifeCycles = ir.lifeCycles;
const chunks = Object.keys(lifeCycles).map<ICodeChunk>(lifeCycleName => { const chunks = Object.keys(lifeCycles).map<ICodeChunk>((lifeCycleName) => {
const normalizeName = cfg.normalizeNameMapping[lifeCycleName] || lifeCycleName; const normalizeName = cfg.normalizeNameMapping[lifeCycleName] || lifeCycleName;
const exportName = cfg.exportNameMapping[lifeCycleName] || lifeCycleName; const exportName = cfg.exportNameMapping[lifeCycleName] || lifeCycleName;
if (normalizeName === 'constructor') { if (normalizeName === 'constructor') {
@ -49,9 +46,7 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) =>
type: ChunkType.STRING, type: ChunkType.STRING,
fileType: cfg.fileType, fileType: cfg.fileType,
name: CLASS_DEFINE_CHUNK_NAME.ConstructorContent, name: CLASS_DEFINE_CHUNK_NAME.ConstructorContent,
content: getFuncExprBody( content: getFuncExprBody((lifeCycles[lifeCycleName] as JSExpression).value),
(lifeCycles[lifeCycleName] as IJSExpression).value,
),
linkAfter: [...DEFAULT_LINK_AFTER[CLASS_DEFINE_CHUNK_NAME.ConstructorStart]], linkAfter: [...DEFAULT_LINK_AFTER[CLASS_DEFINE_CHUNK_NAME.ConstructorStart]],
}; };
} }
@ -60,9 +55,7 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) =>
type: ChunkType.STRING, type: ChunkType.STRING,
fileType: cfg.fileType, fileType: cfg.fileType,
name: REACT_CHUNK_NAME.ClassRenderPre, name: REACT_CHUNK_NAME.ClassRenderPre,
content: getFuncExprBody( content: getFuncExprBody((lifeCycles[lifeCycleName] as JSExpression).value),
(lifeCycles[lifeCycleName] as IJSExpression).value,
),
linkAfter: [REACT_CHUNK_NAME.ClassRenderStart], linkAfter: [REACT_CHUNK_NAME.ClassRenderStart],
}; };
} }
@ -71,10 +64,7 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) =>
type: ChunkType.STRING, type: ChunkType.STRING,
fileType: cfg.fileType, fileType: cfg.fileType,
name: CLASS_DEFINE_CHUNK_NAME.InsMethod, name: CLASS_DEFINE_CHUNK_NAME.InsMethod,
content: transformFuncExpr2MethodMember( content: transformFuncExpr2MethodMember(exportName, (lifeCycles[lifeCycleName] as JSExpression).value),
exportName,
(lifeCycles[lifeCycleName] as IJSExpression).value,
),
linkAfter: [...DEFAULT_LINK_AFTER[CLASS_DEFINE_CHUNK_NAME.InsMethod]], linkAfter: [...DEFAULT_LINK_AFTER[CLASS_DEFINE_CHUNK_NAME.InsMethod]],
}; };
}); });

View File

@ -10,12 +10,12 @@ import {
ICodeChunk, ICodeChunk,
ICodeStruct, ICodeStruct,
IContainerInfo, IContainerInfo,
IJSExpression, JSExpression,
} from '../../../types'; } from '../../../types';
type PluginConfig = { type PluginConfig = {
fileType: string; fileType: string;
} };
const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) => { const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) => {
const cfg: PluginConfig = { const cfg: PluginConfig = {
@ -32,14 +32,11 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) =>
if (ir.methods) { if (ir.methods) {
const methods = ir.methods; const methods = ir.methods;
const chunks = Object.keys(methods).map<ICodeChunk>(methodName => ({ const chunks = Object.keys(methods).map<ICodeChunk>((methodName) => ({
type: ChunkType.STRING, type: ChunkType.STRING,
fileType: cfg.fileType, fileType: cfg.fileType,
name: CLASS_DEFINE_CHUNK_NAME.InsMethod, name: CLASS_DEFINE_CHUNK_NAME.InsMethod,
content: transformFuncExpr2MethodMember( content: transformFuncExpr2MethodMember(methodName, (methods[methodName] as JSExpression).value),
methodName,
(methods[methodName] as IJSExpression).value,
),
linkAfter: [...DEFAULT_LINK_AFTER[CLASS_DEFINE_CHUNK_NAME.InsMethod]], linkAfter: [...DEFAULT_LINK_AFTER[CLASS_DEFINE_CHUNK_NAME.InsMethod]],
})); }));

View File

@ -4,7 +4,7 @@ import {
ChunkType, ChunkType,
ICodeStruct, ICodeStruct,
IContainerInfo, IContainerInfo,
IComponentNodeItem, NodeSchema,
CodePiece, CodePiece,
PIECE_TYPE, PIECE_TYPE,
} from '../../../types'; } from '../../../types';
@ -14,14 +14,16 @@ import { createNodeGenerator, generateString } from '../../../utils/nodeToJSX';
import { generateExpression } from '../../../utils/jsExpression'; import { generateExpression } from '../../../utils/jsExpression';
import { generateCompositeType, handleStringValueDefault } from '../../../utils/compositeType'; import { generateCompositeType, handleStringValueDefault } from '../../../utils/compositeType';
const generateGlobalProps = (nodeItem: IComponentNodeItem): CodePiece[] => { const generateGlobalProps = (nodeItem: NodeSchema): CodePiece[] => {
return [{ return [
value: `{...globalProps.${nodeItem.componentName}}`, {
type: PIECE_TYPE.ATTR, value: `{...globalProps.${nodeItem.componentName}}`,
}]; type: PIECE_TYPE.ATTR,
},
];
}; };
const generateCtrlLine = (nodeItem: IComponentNodeItem): CodePiece[] => { const generateCtrlLine = (nodeItem: NodeSchema): CodePiece[] => {
const pieces: CodePiece[] = []; const pieces: CodePiece[] = [];
if (nodeItem.loop && nodeItem.loopArgs) { if (nodeItem.loop && nodeItem.loopArgs) {
@ -49,13 +51,13 @@ const generateCtrlLine = (nodeItem: IComponentNodeItem): CodePiece[] => {
}; };
const pluginFactory: BuilderComponentPluginFactory<unknown> = () => { const pluginFactory: BuilderComponentPluginFactory<unknown> = () => {
const generator = createNodeGenerator({ const generator = createNodeGenerator(
string: generateString, {
expression: (input) => [generateExpression(input)], string: generateString,
}, [ expression: (input) => [generateExpression(input)],
generateGlobalProps, },
generateCtrlLine, [generateGlobalProps, generateCtrlLine],
]); );
const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => { const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
const next: ICodeStruct = { const next: ICodeStruct = {

View File

@ -1,6 +1,9 @@
import { JSExpression, JSFunction, NodeSchema } from '@ali/lowcode-types'; import { JSExpression, JSFunction, NodeSchema } from '@ali/lowcode-types';
import { CustomHandlerSet } from '../utils/compositeType'; import { CustomHandlerSet } from '../utils/compositeType';
import { IBasicSchema, IParseResult, IProjectSchema, IResultDir, IResultFile, IComponentNodeItem } from './index'; import { IParseResult } from './intermediate';
import { IResultDir, IResultFile } from './result';
import { IBasicSchema, IProjectSchema } from './schema';
export enum FileType { export enum FileType {
CSS = 'css', CSS = 'css',
@ -158,7 +161,7 @@ export interface HandlerSet<T> {
common?: (input: unknown) => T[]; common?: (input: unknown) => T[];
} }
export type ExtGeneratorPlugin = (nodeItem: IComponentNodeItem, handlers: CustomHandlerSet) => CodePiece[]; export type ExtGeneratorPlugin = (nodeItem: NodeSchema, handlers: CustomHandlerSet) => CodePiece[];
// export interface InteratorScope { // export interface InteratorScope {
// [$item: string]: string; // $item 默认取值 "item" // [$item: string]: string; // $item 默认取值 "item"

View File

@ -1,12 +1,4 @@
import { import { IAppConfig, IAppMeta, IContainerNodeItem, IDependency, II18nMap, UtilItem } from '.';
IAppConfig,
IAppMeta,
IContainerNodeItem,
IDependency,
II18nMap,
IInternalDependency,
IUtilItem,
} from './index';
export interface IParseResult { export interface IParseResult {
containers: IContainerInfo[]; containers: IContainerInfo[];
@ -27,7 +19,7 @@ export interface IWithDependency {
} }
export interface IUtilInfo extends IWithDependency { export interface IUtilInfo extends IWithDependency {
utils: IUtilItem[]; utils: UtilItem[];
} }
export interface IRouterInfo extends IWithDependency { export interface IRouterInfo extends IWithDependency {

View File

@ -1,33 +1,7 @@
import { import { BlockSchema, ComponentSchema, NodeSchema, PageSchema, ProjectSchema } from '@ali/lowcode-types';
ProjectSchema,
CompositeObject,
JSExpression,
JSONObject,
NpmInfo,
NodeData,
NodeSchema,
UtilItem,
PageSchema,
BlockSchema,
ComponentSchema,
DataSourceConfig,
} from '@ali/lowcode-types';
export * from '@ali/lowcode-types'; export * from '@ali/lowcode-types';
/**
* -
*
* @export
* @interface IJSExpression
*/
export type IJSExpression = JSExpression;
// JSON 基本类型
export type IJSONObject = JSONObject;
export type ICompositeObject = CompositeObject;
/** /**
* - * -
* *
@ -56,38 +30,15 @@ export interface IProjectSchema extends IBasicSchema {
meta: IAppMeta; // 当前应用元数据信息 meta: IAppMeta; // 当前应用元数据信息
} }
export interface IComponentsMapItem extends NpmInfo {}
export type IUtilItem = UtilItem;
export type ChildNodeItem = NodeData;
export type ChildNodeType = ChildNodeItem | ChildNodeItem[];
/**
* -
* .jsx React Class render jsx
*
* @export
* @interface IComponentNodeItem
*/
export interface IComponentNodeItem extends NodeSchema {}
/** /**
* - * -
* *
* @export * @export
* @interface IContainerNodeItem * @interface IContainerNodeItem
* @extends {IComponentNodeItem} * @extends {NodeSchema}
*/ */
export type IContainerNodeItem = PageSchema | BlockSchema | ComponentSchema; export type IContainerNodeItem = PageSchema | BlockSchema | ComponentSchema;
/**
* -
*
* @export
* @interface IDataSourceConfig
*/
export interface IDataSourceConfig extends DataSourceConfig {}
// TODO... // TODO...
export interface IBasicMeta { export interface IBasicMeta {
title: string; // 标题描述 title: string; // 标题描述
@ -103,7 +54,7 @@ export interface IAppConfig {
sdkVersion?: string; // 渲染模块版本 sdkVersion?: string; // 渲染模块版本
historyMode?: 'browser' | 'hash'; // 浏览器路由browser 哈希路由hash historyMode?: 'browser' | 'hash'; // 浏览器路由browser 哈希路由hash
targetRootID?: string; // 渲染根节点 ID targetRootID?: string; // 渲染根节点 ID
layout?: IComponentNodeItem; layout?: NodeSchema;
theme?: object; // 主题配置,根据接入的主题模块不同 theme?: object; // 主题配置,根据接入的主题模块不同
} }