diff --git a/packages/rspack-cli/src/cli.ts b/packages/rspack-cli/src/cli.ts index 8c8c3915dff..cb5ce4676c6 100644 --- a/packages/rspack-cli/src/cli.ts +++ b/packages/rspack-cli/src/cli.ts @@ -150,10 +150,10 @@ export class RspackCLI { ); (item.plugins ??= []).push({ name: "rspack-bundle-analyzer", - apply(compiler) { + apply(compiler: any) { new BundleAnalyzerPlugin({ generateStatsFile: true - }).apply(compiler as any); + }).apply(compiler); } }); } diff --git a/packages/rspack/etc/api.md b/packages/rspack/etc/api.md index 18a0576b40a..42e2f69c7ef 100644 --- a/packages/rspack/etc/api.md +++ b/packages/rspack/etc/api.md @@ -7262,7 +7262,7 @@ const optimization: z.ZodObject<{ moduleIds: z.ZodOptional>; chunkIds: z.ZodOptional>; minimize: z.ZodOptional; - minimizer: z.ZodOptional, z.ZodUnion<[z.ZodType, z.ZodType, z.ZodUnion<[z.ZodLiteral, z.ZodLiteral<0>, z.ZodLiteral<"">, z.ZodNull, z.ZodUndefined]>]>]>, "many">>; + minimizer: z.ZodOptional, z.ZodUnion<[z.ZodType, z.ZodUnion<[z.ZodLiteral, z.ZodLiteral<0>, z.ZodLiteral<"">, z.ZodNull, z.ZodUndefined]>]>]>, "many">>; mergeDuplicateChunks: z.ZodOptional; splitChunks: z.ZodOptional, z.ZodObject<{ chunks: z.ZodOptional, z.ZodType]>, z.ZodFunction], z.ZodUnknown>, z.ZodBoolean>]>>; @@ -7461,7 +7461,7 @@ const optimization: z.ZodObject<{ moduleIds?: "named" | "natural" | "deterministic" | undefined; chunkIds?: "named" | "natural" | "deterministic" | undefined; minimize?: boolean | undefined; - minimizer?: (false | "" | 0 | RspackPluginInstance | "..." | RspackPluginFunction | null | undefined)[] | undefined; + minimizer?: (false | "" | 0 | RspackPluginInstance | "..." | RspackPluginFunction | WebpackPluginInstance | WebpackPluginFunction | null | undefined)[] | undefined; mergeDuplicateChunks?: boolean | undefined; splitChunks?: false | { name?: string | false | ((args_0: Module | undefined, ...args_1: unknown[]) => unknown) | undefined; @@ -7522,7 +7522,7 @@ const optimization: z.ZodObject<{ moduleIds?: "named" | "natural" | "deterministic" | undefined; chunkIds?: "named" | "natural" | "deterministic" | undefined; minimize?: boolean | undefined; - minimizer?: (false | "" | 0 | RspackPluginInstance | "..." | RspackPluginFunction | null | undefined)[] | undefined; + minimizer?: (false | "" | 0 | RspackPluginInstance | "..." | RspackPluginFunction | WebpackPluginInstance | WebpackPluginFunction | null | undefined)[] | undefined; mergeDuplicateChunks?: boolean | undefined; splitChunks?: false | { name?: string | false | ((args_0: Module | undefined, ...args_1: unknown[]) => unknown) | undefined; @@ -9248,7 +9248,7 @@ type PluginImportOptions = PluginImportConfig[] | undefined; export type Plugins = z.infer; // @public (undocumented) -const plugins: z.ZodArray, z.ZodType, z.ZodUnion<[z.ZodLiteral, z.ZodLiteral<0>, z.ZodLiteral<"">, z.ZodNull, z.ZodUndefined]>]>, "many">; +const plugins: z.ZodArray, z.ZodUnion<[z.ZodLiteral, z.ZodLiteral<0>, z.ZodLiteral<"">, z.ZodNull, z.ZodUndefined]>]>, "many">; // @public (undocumented) type PrintedElement = { @@ -9834,6 +9834,9 @@ declare namespace rspackExports { StatsValue, RspackPluginInstance, RspackPluginFunction, + WebpackCompiler, + WebpackPluginInstance, + WebpackPluginFunction, Plugins, OptimizationRuntimeChunk, OptimizationSplitChunksNameFunction, @@ -11306,7 +11309,7 @@ export const rspackOptions: z.ZodObject<{ moduleIds: z.ZodOptional>; chunkIds: z.ZodOptional>; minimize: z.ZodOptional; - minimizer: z.ZodOptional, z.ZodUnion<[z.ZodType, z.ZodType, z.ZodUnion<[z.ZodLiteral, z.ZodLiteral<0>, z.ZodLiteral<"">, z.ZodNull, z.ZodUndefined]>]>]>, "many">>; + minimizer: z.ZodOptional, z.ZodUnion<[z.ZodType, z.ZodUnion<[z.ZodLiteral, z.ZodLiteral<0>, z.ZodLiteral<"">, z.ZodNull, z.ZodUndefined]>]>]>, "many">>; mergeDuplicateChunks: z.ZodOptional; splitChunks: z.ZodOptional, z.ZodObject<{ chunks: z.ZodOptional, z.ZodType]>, z.ZodFunction], z.ZodUnknown>, z.ZodBoolean>]>>; @@ -11505,7 +11508,7 @@ export const rspackOptions: z.ZodObject<{ moduleIds?: "named" | "natural" | "deterministic" | undefined; chunkIds?: "named" | "natural" | "deterministic" | undefined; minimize?: boolean | undefined; - minimizer?: (false | "" | 0 | RspackPluginInstance | "..." | RspackPluginFunction | null | undefined)[] | undefined; + minimizer?: (false | "" | 0 | RspackPluginInstance | "..." | RspackPluginFunction | WebpackPluginInstance | WebpackPluginFunction | null | undefined)[] | undefined; mergeDuplicateChunks?: boolean | undefined; splitChunks?: false | { name?: string | false | ((args_0: Module | undefined, ...args_1: unknown[]) => unknown) | undefined; @@ -11566,7 +11569,7 @@ export const rspackOptions: z.ZodObject<{ moduleIds?: "named" | "natural" | "deterministic" | undefined; chunkIds?: "named" | "natural" | "deterministic" | undefined; minimize?: boolean | undefined; - minimizer?: (false | "" | 0 | RspackPluginInstance | "..." | RspackPluginFunction | null | undefined)[] | undefined; + minimizer?: (false | "" | 0 | RspackPluginInstance | "..." | RspackPluginFunction | WebpackPluginInstance | WebpackPluginFunction | null | undefined)[] | undefined; mergeDuplicateChunks?: boolean | undefined; splitChunks?: false | { name?: string | false | ((args_0: Module | undefined, ...args_1: unknown[]) => unknown) | undefined; @@ -11626,7 +11629,7 @@ export const rspackOptions: z.ZodObject<{ }>>; resolve: z.ZodOptional>; resolveLoader: z.ZodOptional>; - plugins: z.ZodOptional, z.ZodType, z.ZodUnion<[z.ZodLiteral, z.ZodLiteral<0>, z.ZodLiteral<"">, z.ZodNull, z.ZodUndefined]>]>, "many">>; + plugins: z.ZodOptional, z.ZodUnion<[z.ZodLiteral, z.ZodLiteral<0>, z.ZodLiteral<"">, z.ZodNull, z.ZodUndefined]>]>, "many">>; devServer: z.ZodOptional>; module: z.ZodOptional, z.ZodType]>, z.ZodUnion<[z.ZodLiteral, z.ZodLiteral<0>, z.ZodLiteral<"">, z.ZodNull, z.ZodUndefined]>]>, "many">>; @@ -13019,7 +13022,7 @@ export const rspackOptions: z.ZodObject<{ moduleIds?: "named" | "natural" | "deterministic" | undefined; chunkIds?: "named" | "natural" | "deterministic" | undefined; minimize?: boolean | undefined; - minimizer?: (false | "" | 0 | RspackPluginInstance | "..." | RspackPluginFunction | null | undefined)[] | undefined; + minimizer?: (false | "" | 0 | RspackPluginInstance | "..." | RspackPluginFunction | WebpackPluginInstance | WebpackPluginFunction | null | undefined)[] | undefined; mergeDuplicateChunks?: boolean | undefined; splitChunks?: false | { name?: string | false | ((args_0: Module | undefined, ...args_1: unknown[]) => unknown) | undefined; @@ -13078,7 +13081,7 @@ export const rspackOptions: z.ZodObject<{ nodeEnv?: string | false | undefined; } | undefined; resolveLoader?: ResolveOptions | undefined; - plugins?: (false | "" | 0 | RspackPluginInstance | RspackPluginFunction | null | undefined)[] | undefined; + plugins?: (false | "" | 0 | RspackPluginInstance | RspackPluginFunction | WebpackPluginInstance | WebpackPluginFunction | null | undefined)[] | undefined; devServer?: DevServer | undefined; bail?: boolean | undefined; }, { @@ -13584,7 +13587,7 @@ export const rspackOptions: z.ZodObject<{ moduleIds?: "named" | "natural" | "deterministic" | undefined; chunkIds?: "named" | "natural" | "deterministic" | undefined; minimize?: boolean | undefined; - minimizer?: (false | "" | 0 | RspackPluginInstance | "..." | RspackPluginFunction | null | undefined)[] | undefined; + minimizer?: (false | "" | 0 | RspackPluginInstance | "..." | RspackPluginFunction | WebpackPluginInstance | WebpackPluginFunction | null | undefined)[] | undefined; mergeDuplicateChunks?: boolean | undefined; splitChunks?: false | { name?: string | false | ((args_0: Module | undefined, ...args_1: unknown[]) => unknown) | undefined; @@ -13643,7 +13646,7 @@ export const rspackOptions: z.ZodObject<{ nodeEnv?: string | false | undefined; } | undefined; resolveLoader?: ResolveOptions | undefined; - plugins?: (false | "" | 0 | RspackPluginInstance | RspackPluginFunction | null | undefined)[] | undefined; + plugins?: (false | "" | 0 | RspackPluginInstance | RspackPluginFunction | WebpackPluginInstance | WebpackPluginFunction | null | undefined)[] | undefined; devServer?: DevServer | undefined; bail?: boolean | undefined; }>; @@ -15386,6 +15389,9 @@ export type WebassemblyModuleFilename = z.infer void; + +// @public (undocumented) +export interface WebpackPluginInstance { + // (undocumented) + [k: string]: any; + // (undocumented) + apply: (compiler: WebpackCompiler) => void; +} + // @public (undocumented) interface Webworker { // (undocumented) diff --git a/packages/rspack/src/config/zod.ts b/packages/rspack/src/config/zod.ts index 203c1d86dc3..f96507b955f 100644 --- a/packages/rspack/src/config/zod.ts +++ b/packages/rspack/src/config/zod.ts @@ -1159,9 +1159,27 @@ export interface RspackPluginInstance { } export type RspackPluginFunction = (this: Compiler, compiler: Compiler) => void; +// The Compiler type of webpack is not exactly the same as Rspack. +// It is allowed to use webpack plugins in in the Rspack config, +// so we have defined a loose type here to adapt to webpack plugins. +export type WebpackCompiler = any; + +export interface WebpackPluginInstance { + apply: (compiler: WebpackCompiler) => void; + [k: string]: any; +} +export type WebpackPluginFunction = ( + this: WebpackCompiler, + compiler: WebpackCompiler +) => void; + const plugin = z.union([ - z.custom(), - z.custom(), + z.custom< + | RspackPluginInstance + | RspackPluginFunction + | WebpackPluginInstance + | WebpackPluginFunction + >(), falsy ]); const plugins = plugin.array();