Skip to content

Commit

Permalink
fix: type mismatch when using webpack plugins (#7455)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Aug 6, 2024
1 parent b30f0a8 commit 7c2c6a8
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 16 deletions.
4 changes: 2 additions & 2 deletions packages/rspack-cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
}
Expand Down
41 changes: 29 additions & 12 deletions packages/rspack/etc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -7262,7 +7262,7 @@ const optimization: z.ZodObject<{
moduleIds: z.ZodOptional<z.ZodEnum<["named", "natural", "deterministic"]>>;
chunkIds: z.ZodOptional<z.ZodEnum<["natural", "named", "deterministic"]>>;
minimize: z.ZodOptional<z.ZodBoolean>;
minimizer: z.ZodOptional<z.ZodArray<z.ZodUnion<[z.ZodLiteral<"...">, z.ZodUnion<[z.ZodType<RspackPluginInstance, z.ZodTypeDef, RspackPluginInstance>, z.ZodType<RspackPluginFunction, z.ZodTypeDef, RspackPluginFunction>, z.ZodUnion<[z.ZodLiteral<false>, z.ZodLiteral<0>, z.ZodLiteral<"">, z.ZodNull, z.ZodUndefined]>]>]>, "many">>;
minimizer: z.ZodOptional<z.ZodArray<z.ZodUnion<[z.ZodLiteral<"...">, z.ZodUnion<[z.ZodType<RspackPluginInstance | RspackPluginFunction | WebpackPluginInstance | WebpackPluginFunction, z.ZodTypeDef, RspackPluginInstance | RspackPluginFunction | WebpackPluginInstance | WebpackPluginFunction>, z.ZodUnion<[z.ZodLiteral<false>, z.ZodLiteral<0>, z.ZodLiteral<"">, z.ZodNull, z.ZodUndefined]>]>]>, "many">>;
mergeDuplicateChunks: z.ZodOptional<z.ZodBoolean>;
splitChunks: z.ZodOptional<z.ZodUnion<[z.ZodLiteral<false>, z.ZodObject<{
chunks: z.ZodOptional<z.ZodUnion<[z.ZodUnion<[z.ZodEnum<["initial", "async", "all"]>, z.ZodType<RegExp, z.ZodTypeDef, RegExp>]>, z.ZodFunction<z.ZodTuple<[z.ZodType<Chunk, z.ZodTypeDef, Chunk>], z.ZodUnknown>, z.ZodBoolean>]>>;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -9248,7 +9248,7 @@ type PluginImportOptions = PluginImportConfig[] | undefined;
export type Plugins = z.infer<typeof plugins>;

// @public (undocumented)
const plugins: z.ZodArray<z.ZodUnion<[z.ZodType<RspackPluginInstance, z.ZodTypeDef, RspackPluginInstance>, z.ZodType<RspackPluginFunction, z.ZodTypeDef, RspackPluginFunction>, z.ZodUnion<[z.ZodLiteral<false>, z.ZodLiteral<0>, z.ZodLiteral<"">, z.ZodNull, z.ZodUndefined]>]>, "many">;
const plugins: z.ZodArray<z.ZodUnion<[z.ZodType<RspackPluginInstance | RspackPluginFunction | WebpackPluginInstance | WebpackPluginFunction, z.ZodTypeDef, RspackPluginInstance | RspackPluginFunction | WebpackPluginInstance | WebpackPluginFunction>, z.ZodUnion<[z.ZodLiteral<false>, z.ZodLiteral<0>, z.ZodLiteral<"">, z.ZodNull, z.ZodUndefined]>]>, "many">;

// @public (undocumented)
type PrintedElement = {
Expand Down Expand Up @@ -9834,6 +9834,9 @@ declare namespace rspackExports {
StatsValue,
RspackPluginInstance,
RspackPluginFunction,
WebpackCompiler,
WebpackPluginInstance,
WebpackPluginFunction,
Plugins,
OptimizationRuntimeChunk,
OptimizationSplitChunksNameFunction,
Expand Down Expand Up @@ -11306,7 +11309,7 @@ export const rspackOptions: z.ZodObject<{
moduleIds: z.ZodOptional<z.ZodEnum<["named", "natural", "deterministic"]>>;
chunkIds: z.ZodOptional<z.ZodEnum<["natural", "named", "deterministic"]>>;
minimize: z.ZodOptional<z.ZodBoolean>;
minimizer: z.ZodOptional<z.ZodArray<z.ZodUnion<[z.ZodLiteral<"...">, z.ZodUnion<[z.ZodType<RspackPluginInstance, z.ZodTypeDef, RspackPluginInstance>, z.ZodType<RspackPluginFunction, z.ZodTypeDef, RspackPluginFunction>, z.ZodUnion<[z.ZodLiteral<false>, z.ZodLiteral<0>, z.ZodLiteral<"">, z.ZodNull, z.ZodUndefined]>]>]>, "many">>;
minimizer: z.ZodOptional<z.ZodArray<z.ZodUnion<[z.ZodLiteral<"...">, z.ZodUnion<[z.ZodType<RspackPluginInstance | RspackPluginFunction | WebpackPluginInstance | WebpackPluginFunction, z.ZodTypeDef, RspackPluginInstance | RspackPluginFunction | WebpackPluginInstance | WebpackPluginFunction>, z.ZodUnion<[z.ZodLiteral<false>, z.ZodLiteral<0>, z.ZodLiteral<"">, z.ZodNull, z.ZodUndefined]>]>]>, "many">>;
mergeDuplicateChunks: z.ZodOptional<z.ZodBoolean>;
splitChunks: z.ZodOptional<z.ZodUnion<[z.ZodLiteral<false>, z.ZodObject<{
chunks: z.ZodOptional<z.ZodUnion<[z.ZodUnion<[z.ZodEnum<["initial", "async", "all"]>, z.ZodType<RegExp, z.ZodTypeDef, RegExp>]>, z.ZodFunction<z.ZodTuple<[z.ZodType<Chunk, z.ZodTypeDef, Chunk>], z.ZodUnknown>, z.ZodBoolean>]>>;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -11626,7 +11629,7 @@ export const rspackOptions: z.ZodObject<{
}>>;
resolve: z.ZodOptional<z.ZodType<ResolveOptions, z.ZodTypeDef, ResolveOptions>>;
resolveLoader: z.ZodOptional<z.ZodType<ResolveOptions, z.ZodTypeDef, ResolveOptions>>;
plugins: z.ZodOptional<z.ZodArray<z.ZodUnion<[z.ZodType<RspackPluginInstance, z.ZodTypeDef, RspackPluginInstance>, z.ZodType<RspackPluginFunction, z.ZodTypeDef, RspackPluginFunction>, z.ZodUnion<[z.ZodLiteral<false>, z.ZodLiteral<0>, z.ZodLiteral<"">, z.ZodNull, z.ZodUndefined]>]>, "many">>;
plugins: z.ZodOptional<z.ZodArray<z.ZodUnion<[z.ZodType<RspackPluginInstance | RspackPluginFunction | WebpackPluginInstance | WebpackPluginFunction, z.ZodTypeDef, RspackPluginInstance | RspackPluginFunction | WebpackPluginInstance | WebpackPluginFunction>, z.ZodUnion<[z.ZodLiteral<false>, z.ZodLiteral<0>, z.ZodLiteral<"">, z.ZodNull, z.ZodUndefined]>]>, "many">>;
devServer: z.ZodOptional<z.ZodType<DevServer, z.ZodTypeDef, DevServer>>;
module: z.ZodOptional<z.ZodObject<{
defaultRules: z.ZodOptional<z.ZodArray<z.ZodUnion<[z.ZodUnion<[z.ZodLiteral<"...">, z.ZodType<RuleSetRule, z.ZodTypeDef, RuleSetRule>]>, z.ZodUnion<[z.ZodLiteral<false>, z.ZodLiteral<0>, z.ZodLiteral<"">, z.ZodNull, z.ZodUndefined]>]>, "many">>;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}, {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}>;
Expand Down Expand Up @@ -15386,6 +15389,9 @@ export type WebassemblyModuleFilename = z.infer<typeof webassemblyModuleFilename
// @public (undocumented)
const webassemblyModuleFilename: z.ZodString;

// @public (undocumented)
export type WebpackCompiler = any;

// @public (undocumented)
export const WebpackError: ErrorConstructor;

Expand All @@ -15407,6 +15413,17 @@ class WebpackError_2 extends Error {
module?: Module;
}

// @public (undocumented)
export type WebpackPluginFunction = (this: WebpackCompiler, compiler: WebpackCompiler) => void;

// @public (undocumented)
export interface WebpackPluginInstance {
// (undocumented)
[k: string]: any;
// (undocumented)
apply: (compiler: WebpackCompiler) => void;
}

// @public (undocumented)
interface Webworker {
// (undocumented)
Expand Down
22 changes: 20 additions & 2 deletions packages/rspack/src/config/zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<RspackPluginInstance>(),
z.custom<RspackPluginFunction>(),
z.custom<
| RspackPluginInstance
| RspackPluginFunction
| WebpackPluginInstance
| WebpackPluginFunction
>(),
falsy
]);
const plugins = plugin.array();
Expand Down

2 comments on commit 7c2c6a8

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result
modernjs ❌ failure
_selftest ✅ success
nx ❌ failure
rspress ✅ success
rsbuild ❌ failure
examples ✅ success

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

Name Base (2024-08-06 b30f0a8) Current Change
10000_development-mode + exec 2.33 s ± 47 ms 2.32 s ± 28 ms -0.51 %
10000_development-mode_hmr + exec 711 ms ± 8 ms 712 ms ± 8.7 ms +0.18 %
10000_production-mode + exec 2.89 s ± 41 ms 2.89 s ± 39 ms -0.17 %
arco-pro_development-mode + exec 1.87 s ± 68 ms 1.92 s ± 76 ms +2.71 %
arco-pro_development-mode_hmr + exec 435 ms ± 1.7 ms 435 ms ± 1.9 ms -0.07 %
arco-pro_production-mode + exec 3.41 s ± 68 ms 3.44 s ± 71 ms +0.96 %
threejs_development-mode_10x + exec 1.71 s ± 16 ms 1.71 s ± 21 ms +0.04 %
threejs_development-mode_10x_hmr + exec 828 ms ± 7.8 ms 831 ms ± 7.6 ms +0.38 %
threejs_production-mode_10x + exec 5.51 s ± 48 ms 5.52 s ± 34 ms +0.08 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.53 s ± 98 ms 3.56 s ± 114 ms +0.93 %

Please sign in to comment.