Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: .dumirc apiParser add propFilter skipPropsWithName config #1666

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
11 changes: 10 additions & 1 deletion examples/normal/.dumirc.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
const names = [
'skipProps1',
'skipProps2'
];

export default {
locales: [
{ id: 'zh-CN', name: '中文' },
{ id: 'en-US', name: 'EN' },
],
themeConfig: { name: '示例' },
mfsu: false,
apiParser: {},
apiParser: {
propFilter: {
skipPropsWithName: names
}
},
resolve: { entryFile: './src/index.ts' },
};
7 changes: 6 additions & 1 deletion examples/normal/src/Foo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ interface A {
a: string;
}

interface SkipProps {
skipProps1: string;
skipProps2: number;
}

const Foo: FC<{
/**
* @description 标题
Expand All @@ -24,7 +29,7 @@ const Foo: FC<{
children: React.ReactNode;
onConfirm: (output: { children: any[] }) => void;
dom: HTMLElement;
}> = (props) => {
} & SkipProps> = (props) => {
return <>{props.title}</>;
};

Expand Down
16 changes: 16 additions & 0 deletions src/assetParsers/atom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class AtomAssetsParser {
paths: string | string[];
options: chokidar.WatchOptions;
};
private skipPropsWithName: string[] = [];

constructor(opts: {
entryFile: string;
Expand All @@ -40,11 +41,15 @@ class AtomAssetsParser {
unpkgHost?: string;
watch?: boolean;
parseOptions?: object;
propFilter?: {
skipPropsWithName?: string[];
};
}) {
const absEntryFile = path.resolve(opts.resolveDir, opts.entryFile);

this.resolveDir = opts.resolveDir;
this.resolveFilter = opts.resolveFilter || (() => true);
this.skipPropsWithName = opts.propFilter?.skipPropsWithName || [];
this.entryDir = path.relative(opts.resolveDir, path.dirname(absEntryFile));
this.parser = new SchemaParser({
entryPath: absEntryFile,
Expand Down Expand Up @@ -107,6 +112,17 @@ class AtomAssetsParser {
let propsConfig = needResolve
? (await resolver.getComponent(id)).props
: fallbackProps;

if (this.skipPropsWithName.length > 0) {
const filteredProperties = Object.keys(propsConfig.properties)
.filter((key) => !this.skipPropsWithName.includes(key))
.reduce((obj, key) => {
obj[key] = propsConfig.properties[key];
return obj;
}, {} as Record<string, any>);
propsConfig.properties = filteredProperties;
}

const size = Buffer.byteLength(JSON.stringify(propsConfig));

if (size > MAX_PARSE_SIZE) {
Expand Down
2 changes: 2 additions & 0 deletions src/features/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export default (api: IApi) => {
unpkgHost: Joi.string().uri().optional(),
resolveFilter: Joi.function().optional(),
parseOptions: Joi.object().optional(),
propFilter: Joi.object().optional(),
}),
},
});
Expand Down Expand Up @@ -56,6 +57,7 @@ export default (api: IApi) => {
unpkgHost: api.config.apiParser!.unpkgHost,
resolveFilter: api.config.apiParser!.resolveFilter,
parseOptions: api.config.apiParser!.parseOptions,
propFilter: api.config.apiParser!.propFilter,
});
});

Expand Down