From bac1ca1fe9568e981550cb5c9741c8e6e5f516a1 Mon Sep 17 00:00:00 2001 From: fi3ework Date: Fri, 29 Nov 2024 17:00:40 +0800 Subject: [PATCH] fix: types --- .../src/options/raw_external.rs | 1 - .../rspack_core/src/context_module_factory.rs | 2 +- crates/rspack_core/src/module_factory.rs | 1 - crates/rspack_plugin_externals/src/plugin.rs | 7 +- packages/rspack/etc/core.api.md | 180 ++---------------- .../src/builtin-plugin/ExternalsPlugin.ts | 11 +- packages/rspack/src/config/zod.ts | 28 ++- .../resolve/node_modules/external.js | 0 .../externals/resolve/test.filter.js | 2 - 9 files changed, 49 insertions(+), 183 deletions(-) create mode 100644 tests/webpack-test/configCases/externals/resolve/node_modules/external.js delete mode 100644 tests/webpack-test/configCases/externals/resolve/test.filter.js diff --git a/crates/rspack_binding_options/src/options/raw_external.rs b/crates/rspack_binding_options/src/options/raw_external.rs index 28a205471f4..87a204229d5 100644 --- a/crates/rspack_binding_options/src/options/raw_external.rs +++ b/crates/rspack_binding_options/src/options/raw_external.rs @@ -7,7 +7,6 @@ use napi_derive::napi; use rspack_binding_values::JsResolver; use rspack_core::{ExternalItem, ExternalItemFnResult, ExternalItemValue}; use rspack_core::{ExternalItemFnCtx, ResolveOptionsWithDependencyType, ResolverFactory}; -// use rspack_napi::regexp::{JsRegExp, JsRegExpExt}; use rspack_napi::threadsafe_function::ThreadsafeFunction; use rspack_regex::RspackRegex; diff --git a/crates/rspack_core/src/context_module_factory.rs b/crates/rspack_core/src/context_module_factory.rs index ff50c5b4ac7..ca1ae462e77 100644 --- a/crates/rspack_core/src/context_module_factory.rs +++ b/crates/rspack_core/src/context_module_factory.rs @@ -128,7 +128,7 @@ impl ContextModuleFactory { resolve_options: options .resolve_options .clone() - .map(|arc| Box::new(Arc::try_unwrap(arc).unwrap_or_else(|arc| (*arc).clone()))), + .map(|r| Box::new(Arc::unwrap_or_clone(r))), resolve_to_context: false, dependency_category: options.context_options.category, }); diff --git a/crates/rspack_core/src/module_factory.rs b/crates/rspack_core/src/module_factory.rs index 89332dfbd6c..376df7f71a3 100644 --- a/crates/rspack_core/src/module_factory.rs +++ b/crates/rspack_core/src/module_factory.rs @@ -12,7 +12,6 @@ use crate::{ #[derive(Debug, Clone)] pub struct ModuleFactoryCreateData { pub compilation_id: CompilationId, - // pub resolve_options: Option>, pub resolve_options: Option>, pub options: Arc, pub context: Context, diff --git a/crates/rspack_plugin_externals/src/plugin.rs b/crates/rspack_plugin_externals/src/plugin.rs index a3824bb1c48..6efa2f352c0 100644 --- a/crates/rspack_plugin_externals/src/plugin.rs +++ b/crates/rspack_plugin_externals/src/plugin.rs @@ -188,8 +188,11 @@ async fn factorize(&self, data: &mut ModuleFactoryCreateData) -> Result ((context: string, request: string, callback: (err?: Error, result?: string) => void) => void) | ((context: string, request: string) => Promise); }; // @public @@ -2015,15 +2017,13 @@ export type ExternalItemValue = string | boolean | string[] | ExternalItemUmdVal export type Externals = ExternalItem | ExternalItem[]; // @public (undocumented) -export const ExternalsPlugin: { - new (type: string, externals: Externals): { - name: BuiltinPluginName; - _args: [type: string, externals: Externals]; - affectedHooks: "done" | "make" | "compile" | "emit" | "afterEmit" | "invalid" | "thisCompilation" | "afterDone" | "compilation" | "normalModuleFactory" | "contextModuleFactory" | "initialize" | "shouldEmit" | "infrastructureLog" | "beforeRun" | "run" | "assetEmitted" | "failed" | "shutdown" | "watchRun" | "watchClose" | "environment" | "afterEnvironment" | "afterPlugins" | "afterResolvers" | "beforeCompile" | "afterCompile" | "finishMake" | "entryOption" | undefined; - raw(compiler: Compiler_2): BuiltinPlugin; - apply(compiler: Compiler_2): void; - }; -}; +export class ExternalsPlugin extends RspackBuiltinPlugin { + constructor(type: string, externals: Externals); + // (undocumented) + name: BuiltinPluginName; + // (undocumented) + raw(compiler: Compiler): BuiltinPlugin | undefined; +} // @public export type ExternalsPresets = { @@ -6490,107 +6490,7 @@ export const rspackOptions: z.ZodObject<{ } | undefined; } | undefined; }>>; - externals: z.ZodOptional]>, z.ZodRecord, z.ZodArray]>, ZodRspackCrossChecker]>>]>, z.ZodFunction; - dependencyType: z.ZodOptional; - request: z.ZodOptional; - contextInfo: z.ZodOptional>; - }, "strict", z.ZodTypeAny, { - request?: string | undefined; - context?: string | undefined; - dependencyType?: string | undefined; - contextInfo?: { - issuer: string; - } | undefined; - }, { - request?: string | undefined; - context?: string | undefined; - dependencyType?: string | undefined; - contextInfo?: { - issuer: string; - } | undefined; - }>, z.ZodFunction>, z.ZodOptional, z.ZodArray]>, ZodRspackCrossChecker]>>, z.ZodOptional>], z.ZodUnknown>, z.ZodVoid>], z.ZodUnknown>, z.ZodUnknown>]>, z.ZodFunction; - dependencyType: z.ZodOptional; - request: z.ZodOptional; - contextInfo: z.ZodOptional>; - }, "strict", z.ZodTypeAny, { - request?: string | undefined; - context?: string | undefined; - dependencyType?: string | undefined; - contextInfo?: { - issuer: string; - } | undefined; - }, { - request?: string | undefined; - context?: string | undefined; - dependencyType?: string | undefined; - contextInfo?: { - issuer: string; - } | undefined; - }>], z.ZodUnknown>, z.ZodPromise, z.ZodArray]>, ZodRspackCrossChecker]>>>]>, "many">, z.ZodUnion<[z.ZodUnion<[z.ZodUnion<[z.ZodUnion<[z.ZodString, z.ZodType]>, z.ZodRecord, z.ZodArray]>, ZodRspackCrossChecker]>>]>, z.ZodFunction; - dependencyType: z.ZodOptional; - request: z.ZodOptional; - contextInfo: z.ZodOptional>; - }, "strict", z.ZodTypeAny, { - request?: string | undefined; - context?: string | undefined; - dependencyType?: string | undefined; - contextInfo?: { - issuer: string; - } | undefined; - }, { - request?: string | undefined; - context?: string | undefined; - dependencyType?: string | undefined; - contextInfo?: { - issuer: string; - } | undefined; - }>, z.ZodFunction>, z.ZodOptional, z.ZodArray]>, ZodRspackCrossChecker]>>, z.ZodOptional>], z.ZodUnknown>, z.ZodVoid>], z.ZodUnknown>, z.ZodUnknown>]>, z.ZodFunction; - dependencyType: z.ZodOptional; - request: z.ZodOptional; - contextInfo: z.ZodOptional>; - }, "strict", z.ZodTypeAny, { - request?: string | undefined; - context?: string | undefined; - dependencyType?: string | undefined; - contextInfo?: { - issuer: string; - } | undefined; - }, { - request?: string | undefined; - context?: string | undefined; - dependencyType?: string | undefined; - contextInfo?: { - issuer: string; - } | undefined; - }>], z.ZodUnknown>, z.ZodPromise, z.ZodArray]>, ZodRspackCrossChecker]>>>]>]>>; + externals: z.ZodOptional]>, z.ZodRecord, z.ZodArray]>, ZodRspackCrossChecker]>>]>, z.ZodFunction, z.ZodFunction>, z.ZodOptional, z.ZodArray]>, ZodRspackCrossChecker]>>, z.ZodOptional>], z.ZodUnknown>, z.ZodVoid>], z.ZodUnknown>, z.ZodUnknown>]>, z.ZodFunction], z.ZodUnknown>, z.ZodPromise, z.ZodArray]>, ZodRspackCrossChecker]>>>]>, "many">, z.ZodUnion<[z.ZodUnion<[z.ZodUnion<[z.ZodUnion<[z.ZodString, z.ZodType]>, z.ZodRecord, z.ZodArray]>, ZodRspackCrossChecker]>>]>, z.ZodFunction, z.ZodFunction>, z.ZodOptional, z.ZodArray]>, ZodRspackCrossChecker]>>, z.ZodOptional>], z.ZodUnknown>, z.ZodVoid>], z.ZodUnknown>, z.ZodUnknown>]>, z.ZodFunction], z.ZodUnknown>, z.ZodPromise, z.ZodArray]>, ZodRspackCrossChecker]>>>]>]>>; externalsType: z.ZodOptional>; externalsPresets: z.ZodOptional; @@ -8865,35 +8765,7 @@ export const rspackOptions: z.ZodObject<{ } | undefined; loader?: Record | undefined; resolveLoader?: t.ResolveOptions | undefined; - externals?: string | RegExp | Record | ((args_0: { - request?: string | undefined; - context?: string | undefined; - dependencyType?: string | undefined; - contextInfo?: { - issuer: string; - } | undefined; - }, args_1: (args_0: Error | undefined, args_1: string | boolean | string[] | t.ExternalItemUmdValue | t.ExternalItemObjectValue | undefined, args_2: "module" | "global" | "system" | "script" | "commonjs" | "umd" | "amd" | "var" | "jsonp" | "import" | "assign" | "this" | "window" | "self" | "commonjs2" | "commonjs-module" | "commonjs-static" | "amd-require" | "umd2" | "promise" | "module-import" | "node-commonjs" | "commonjs-import" | undefined, ...args: unknown[]) => void, ...args: unknown[]) => unknown) | ((args_0: { - request?: string | undefined; - context?: string | undefined; - dependencyType?: string | undefined; - contextInfo?: { - issuer: string; - } | undefined; - }, ...args: unknown[]) => Promise) | (string | RegExp | Record | ((args_0: { - request?: string | undefined; - context?: string | undefined; - dependencyType?: string | undefined; - contextInfo?: { - issuer: string; - } | undefined; - }, args_1: (args_0: Error | undefined, args_1: string | boolean | string[] | t.ExternalItemUmdValue | t.ExternalItemObjectValue | undefined, args_2: "module" | "global" | "system" | "script" | "commonjs" | "umd" | "amd" | "var" | "jsonp" | "import" | "assign" | "this" | "window" | "self" | "commonjs2" | "commonjs-module" | "commonjs-static" | "amd-require" | "umd2" | "promise" | "module-import" | "node-commonjs" | "commonjs-import" | undefined, ...args: unknown[]) => void, ...args: unknown[]) => unknown) | ((args_0: { - request?: string | undefined; - context?: string | undefined; - dependencyType?: string | undefined; - contextInfo?: { - issuer: string; - } | undefined; - }, ...args: unknown[]) => Promise))[] | undefined; + externals?: string | RegExp | Record | ((args_0: t.ExternalItemFunctionData, args_1: (args_0: Error | undefined, args_1: string | boolean | string[] | t.ExternalItemUmdValue | t.ExternalItemObjectValue | undefined, args_2: "module" | "global" | "system" | "script" | "commonjs" | "umd" | "amd" | "var" | "jsonp" | "import" | "assign" | "this" | "window" | "self" | "commonjs2" | "commonjs-module" | "commonjs-static" | "amd-require" | "umd2" | "promise" | "module-import" | "node-commonjs" | "commonjs-import" | undefined, ...args: unknown[]) => void, ...args: unknown[]) => unknown) | ((args_0: t.ExternalItemFunctionData, ...args: unknown[]) => Promise) | (string | RegExp | Record | ((args_0: t.ExternalItemFunctionData, args_1: (args_0: Error | undefined, args_1: string | boolean | string[] | t.ExternalItemUmdValue | t.ExternalItemObjectValue | undefined, args_2: "module" | "global" | "system" | "script" | "commonjs" | "umd" | "amd" | "var" | "jsonp" | "import" | "assign" | "this" | "window" | "self" | "commonjs2" | "commonjs-module" | "commonjs-static" | "amd-require" | "umd2" | "promise" | "module-import" | "node-commonjs" | "commonjs-import" | undefined, ...args: unknown[]) => void, ...args: unknown[]) => unknown) | ((args_0: t.ExternalItemFunctionData, ...args: unknown[]) => Promise))[] | undefined; externalsType?: "module" | "global" | "system" | "script" | "commonjs" | "umd" | "amd" | "var" | "jsonp" | "import" | "assign" | "this" | "window" | "self" | "commonjs2" | "commonjs-module" | "commonjs-static" | "amd-require" | "umd2" | "promise" | "module-import" | "node-commonjs" | "commonjs-import" | undefined; externalsPresets?: { node?: boolean | undefined; @@ -9493,35 +9365,7 @@ export const rspackOptions: z.ZodObject<{ } | undefined; loader?: Record | undefined; resolveLoader?: t.ResolveOptions | undefined; - externals?: string | RegExp | Record | ((args_0: { - request?: string | undefined; - context?: string | undefined; - dependencyType?: string | undefined; - contextInfo?: { - issuer: string; - } | undefined; - }, args_1: (args_0: Error | undefined, args_1: string | boolean | string[] | t.ExternalItemUmdValue | t.ExternalItemObjectValue | undefined, args_2: "module" | "global" | "system" | "script" | "commonjs" | "umd" | "amd" | "var" | "jsonp" | "import" | "assign" | "this" | "window" | "self" | "commonjs2" | "commonjs-module" | "commonjs-static" | "amd-require" | "umd2" | "promise" | "module-import" | "node-commonjs" | "commonjs-import" | undefined, ...args: unknown[]) => void, ...args: unknown[]) => unknown) | ((args_0: { - request?: string | undefined; - context?: string | undefined; - dependencyType?: string | undefined; - contextInfo?: { - issuer: string; - } | undefined; - }, ...args: unknown[]) => Promise) | (string | RegExp | Record | ((args_0: { - request?: string | undefined; - context?: string | undefined; - dependencyType?: string | undefined; - contextInfo?: { - issuer: string; - } | undefined; - }, args_1: (args_0: Error | undefined, args_1: string | boolean | string[] | t.ExternalItemUmdValue | t.ExternalItemObjectValue | undefined, args_2: "module" | "global" | "system" | "script" | "commonjs" | "umd" | "amd" | "var" | "jsonp" | "import" | "assign" | "this" | "window" | "self" | "commonjs2" | "commonjs-module" | "commonjs-static" | "amd-require" | "umd2" | "promise" | "module-import" | "node-commonjs" | "commonjs-import" | undefined, ...args: unknown[]) => void, ...args: unknown[]) => unknown) | ((args_0: { - request?: string | undefined; - context?: string | undefined; - dependencyType?: string | undefined; - contextInfo?: { - issuer: string; - } | undefined; - }, ...args: unknown[]) => Promise))[] | undefined; + externals?: string | RegExp | Record | ((args_0: t.ExternalItemFunctionData, args_1: (args_0: Error | undefined, args_1: string | boolean | string[] | t.ExternalItemUmdValue | t.ExternalItemObjectValue | undefined, args_2: "module" | "global" | "system" | "script" | "commonjs" | "umd" | "amd" | "var" | "jsonp" | "import" | "assign" | "this" | "window" | "self" | "commonjs2" | "commonjs-module" | "commonjs-static" | "amd-require" | "umd2" | "promise" | "module-import" | "node-commonjs" | "commonjs-import" | undefined, ...args: unknown[]) => void, ...args: unknown[]) => unknown) | ((args_0: t.ExternalItemFunctionData, ...args: unknown[]) => Promise) | (string | RegExp | Record | ((args_0: t.ExternalItemFunctionData, args_1: (args_0: Error | undefined, args_1: string | boolean | string[] | t.ExternalItemUmdValue | t.ExternalItemObjectValue | undefined, args_2: "module" | "global" | "system" | "script" | "commonjs" | "umd" | "amd" | "var" | "jsonp" | "import" | "assign" | "this" | "window" | "self" | "commonjs2" | "commonjs-module" | "commonjs-static" | "amd-require" | "umd2" | "promise" | "module-import" | "node-commonjs" | "commonjs-import" | undefined, ...args: unknown[]) => void, ...args: unknown[]) => unknown) | ((args_0: t.ExternalItemFunctionData, ...args: unknown[]) => Promise))[] | undefined; externalsType?: "module" | "global" | "system" | "script" | "commonjs" | "umd" | "amd" | "var" | "jsonp" | "import" | "assign" | "this" | "window" | "self" | "commonjs2" | "commonjs-module" | "commonjs-static" | "amd-require" | "umd2" | "promise" | "module-import" | "node-commonjs" | "commonjs-import" | undefined; externalsPresets?: { node?: boolean | undefined; diff --git a/packages/rspack/src/builtin-plugin/ExternalsPlugin.ts b/packages/rspack/src/builtin-plugin/ExternalsPlugin.ts index 720ab943712..de50c04a286 100644 --- a/packages/rspack/src/builtin-plugin/ExternalsPlugin.ts +++ b/packages/rspack/src/builtin-plugin/ExternalsPlugin.ts @@ -73,8 +73,15 @@ function getRawExternalItem( context, request, getResolveContext(), - // @ts-expect-error TODO: fix the type - callback + (err, result) => { + if (err) return callback(err); + // Sync with how webpack fixes the type: + // https://github.com/webpack/webpack/blob/a2ad76cd50ae780dead395c68ea67d46de9828f3/lib/ExternalModuleFactoryPlugin.js#L276 + callback( + undefined, + typeof result === "string" ? result : undefined + ); + } ); } else { return new Promise((resolve, reject) => { diff --git a/packages/rspack/src/config/zod.ts b/packages/rspack/src/config/zod.ts index 4e43e8c1dd9..8e4ed818f43 100644 --- a/packages/rspack/src/config/zod.ts +++ b/packages/rspack/src/config/zod.ts @@ -869,10 +869,27 @@ const externalItemFunctionData = z.strictObject({ .optional(), getResolve: z .function() - .returns(z.function().args(z.string(), z.string(), z.function().optional())) + .returns( + z + .function() + .args(z.string(), z.string()) + .returns(z.promise(z.string())) + .or( + z + .function() + .args( + z.string(), + z.string(), + z + .function() + .args(z.instanceof(Error).optional(), z.string().optional()) + .returns(z.void()) + ) + .returns(z.void()) + ) + ) .optional() -}); -export type ExternalItemFunctionData = z.infer; +}) satisfies z.ZodType; const externalItem = z .string() @@ -882,7 +899,7 @@ const externalItem = z z .function() .args( - externalItemFunctionData, + externalItemFunctionData as z.ZodType, z .function() .args( @@ -896,14 +913,13 @@ const externalItem = z .or( z .function() - .args(externalItemFunctionData) + .args(externalItemFunctionData as z.ZodType) .returns(z.promise(externalItemValue)) ) satisfies z.ZodType; const externals = externalItem .array() .or(externalItem) satisfies z.ZodType; -//#endregion //#region ExternalsPresets const externalsPresets = z.strictObject({ diff --git a/tests/webpack-test/configCases/externals/resolve/node_modules/external.js b/tests/webpack-test/configCases/externals/resolve/node_modules/external.js new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/webpack-test/configCases/externals/resolve/test.filter.js b/tests/webpack-test/configCases/externals/resolve/test.filter.js deleted file mode 100644 index 042493e42a5..00000000000 --- a/tests/webpack-test/configCases/externals/resolve/test.filter.js +++ /dev/null @@ -1,2 +0,0 @@ -// TODO: Should create a issue for this test -module.exports = () => { return false }