From a315f9bf5037907564cdf17f08639b1580b6c0c3 Mon Sep 17 00:00:00 2001 From: Hana Date: Tue, 12 Sep 2023 12:23:38 +0800 Subject: [PATCH] feat: improve zod types (#4173) --- packages/rspack/src/config/zod.ts | 65 +++++++++++-------------------- 1 file changed, 23 insertions(+), 42 deletions(-) diff --git a/packages/rspack/src/config/zod.ts b/packages/rspack/src/config/zod.ts index afb8ee3a64c..5284b47232c 100644 --- a/packages/rspack/src/config/zod.ts +++ b/packages/rspack/src/config/zod.ts @@ -337,20 +337,22 @@ export type Resolve = z.infer; //#endregion //#region Module -export type RuleSetCondition = - | RegExp - | string - | RuleSetConditions - | RuleSetLogicalConditions - | ((value: string) => boolean); -const ruleSetCondition: z.ZodType = z +const baseRuleSetCondition = z .instanceof(RegExp) .or(z.string()) - .or(z.lazy(() => ruleSetConditions)) - .or(z.lazy(() => ruleSetLogicalConditions)) .or(z.function().args(z.string()).returns(z.boolean())); +export type RuleSetCondition = + | z.infer + | RuleSetConditions + | RuleSetLogicalConditions; + +const ruleSetCondition: z.ZodType = baseRuleSetCondition + .or(z.lazy(() => ruleSetConditions)) + .or(z.lazy(() => ruleSetLogicalConditions)); + export type RuleSetConditions = RuleSetCondition[]; + const ruleSetConditions: z.ZodType = z.lazy(() => z.array(ruleSetCondition) ); @@ -360,6 +362,7 @@ export type RuleSetLogicalConditions = { or?: RuleSetConditions; not?: RuleSetConditions; }; + const ruleSetLogicalConditions: z.ZodType = z.strictObject({ and: ruleSetConditions.optional(), @@ -390,37 +393,7 @@ const ruleSetUse = ruleSetUseItem ); export type RuleSetUse = z.infer; -export type RuleSetRule = { - test?: RuleSetCondition; - exclude?: RuleSetCondition; - include?: RuleSetCondition; - issuer?: RuleSetCondition; - dependency?: RuleSetCondition; - resource?: RuleSetCondition; - resourceFragment?: RuleSetCondition; - resourceQuery?: RuleSetCondition; - scheme?: RuleSetCondition; - mimetype?: RuleSetCondition; - descriptionData?: { - [k: string]: RuleSetCondition; - }; - oneOf?: RuleSetRule[]; - rules?: RuleSetRule[]; - type?: string; - loader?: RuleSetLoader; - options?: RuleSetLoaderOptions; - use?: RuleSetUse; - parser?: { - [k: string]: any; - }; - generator?: { - [k: string]: any; - }; - resolve?: ResolveOptions; - sideEffects?: boolean; - enforce?: "pre" | "post"; -}; -const ruleSetRule: z.ZodType = z.strictObject({ +const baseRuleSetRule = z.strictObject({ test: ruleSetCondition.optional(), exclude: ruleSetCondition.optional(), include: ruleSetCondition.optional(), @@ -432,8 +405,6 @@ const ruleSetRule: z.ZodType = z.strictObject({ scheme: ruleSetCondition.optional(), mimetype: ruleSetCondition.optional(), descriptionData: z.record(ruleSetCondition).optional(), - oneOf: z.lazy(() => ruleSetRule.array()).optional(), - rules: z.lazy(() => ruleSetRule.array()).optional(), type: z.string().optional(), loader: ruleSetLoader.optional(), options: ruleSetLoaderOptions.optional(), @@ -445,6 +416,16 @@ const ruleSetRule: z.ZodType = z.strictObject({ enforce: z.literal("pre").or(z.literal("post")).optional() }); +export type RuleSetRule = z.infer & { + oneOf?: RuleSetRule[]; + rules?: RuleSetRule[]; +}; + +const ruleSetRule: z.ZodType = baseRuleSetRule.extend({ + oneOf: z.lazy(() => ruleSetRule.array()).optional(), + rules: z.lazy(() => ruleSetRule.array()).optional() +}); + const ruleSetRules = z.array(z.literal("...").or(ruleSetRule)); export type RuleSetRules = z.infer;