Merge branch 'feat-wrapper' into 'release/1.0.37'

feat: add prototypeWrapper&preprocessor



See merge request !1165815
This commit is contained in:
力皓 2021-02-24 16:04:41 +08:00
commit ff7df3b64d
2 changed files with 67 additions and 2 deletions

View File

@ -33,6 +33,37 @@ export interface ComponentViewBundle {
module: ComponentType<any>;
}
const prototypeViewWrapperList: any[] = [];
function registerPrototypeViewWrapper(name: string, prototypeViewWrapper: Function) {
if (!prototypeViewWrapperList.find(p => p.name === name) && typeof prototypeViewWrapper === 'function') {
prototypeViewWrapperList.push({
name,
prototypeViewWrapper,
});
}
}
function wrapPrototypeView(view: ComponentClass) {
const newView = prototypeViewWrapperList.reduce((acc, { prototypeViewWrapper }) => {
return prototypeViewWrapper(acc.displayName, acc) || acc;
}, view);
if (!newView.displayName && view.displayName) {
newView.displayName = view.displayName;
}
if (!newView.propTypes && view.propTypes) {
newView.propTypes = view.propTypes;
}
if (!newView.defaultProps && view.defaultProps) {
newView.defaultProps = view.defaultProps;
}
return newView;
}
function getPrototypeViewWrapperList() {
return prototypeViewWrapperList;
}
export default class Bundle {
static createPrototype = Prototype.create;
@ -46,6 +77,14 @@ export default class Bundle {
static overridePropsConfigure = Prototype.overridePropsConfigure;
static registerPrototypeConfigPreprocessor = Prototype.registerPrototypeConfigPreprocessor;
static getPrototypeConfigPreprocessorList = Prototype.getPrototypeConfigPreprocessorList;
static registerPrototypeViewWrapper = registerPrototypeViewWrapper;
static getPrototypeViewWrapperList = getPrototypeViewWrapperList;
static create(protos: ComponentProtoBundle[], views?: ComponentViewBundle[]) {
return new Bundle(protos, views);
}
@ -161,7 +200,7 @@ export default class Bundle {
viewDetail.displayName = getCamelName(viewName || item.name);
}
(viewDetail as any)._packageName_ = viewName || item.name;
this.viewsMap[viewDetail.displayName] = viewDetail;
this.viewsMap[viewDetail.displayName] = wrapPrototypeView(viewDetail);
});
}

View File

@ -205,6 +205,27 @@ function isNewSpec(options: any): options is ComponentMetadata {
);
}
const prototypeConfigPreprocessorList: any[] = [];
function registerPrototypeConfigPreprocessor(name: String, preprocessor: Function) {
if (!prototypeConfigPreprocessorList.find(p => p.name === name) && typeof preprocessor === 'function') {
prototypeConfigPreprocessorList.push({
name,
preprocessor,
});
}
}
function modifyPrototypeConfig(config: any) {
const { componentName } = config;
return prototypeConfigPreprocessorList.reduce((acc, { preprocessor }) => {
return preprocessor(componentName, acc) || acc;
}, config);
}
function getPrototypeConfigPreprocessorList() {
return prototypeConfigPreprocessorList;
}
class Prototype {
static addGlobalPropsReducer = addGlobalPropsReducer;
@ -216,8 +237,13 @@ class Prototype {
static overridePropsConfigure = overridePropsConfigure;
static registerPrototypeConfigPreprocessor = registerPrototypeConfigPreprocessor;
static getPrototypeConfigPreprocessorList = getPrototypeConfigPreprocessorList;
static create(config: OldPrototypeConfig | ComponentMetadata | ComponentMeta, extraConfigs: any = null, lookup = false) {
return new Prototype(config, extraConfigs, lookup);
const modifiedConfig = modifyPrototypeConfig(config);
return new Prototype(modifiedConfig, extraConfigs, lookup);
}
readonly isPrototype = true;