diff --git a/crates/rspack_binding_options/src/options/raw_external.rs b/crates/rspack_binding_options/src/options/raw_external.rs index 28a205471f42..87a204229d57 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 ff50c5b4ac7b..ca1ae462e77f 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 b4928de67464..b274b57ad43b 100644 --- a/crates/rspack_core/src/module_factory.rs +++ b/crates/rspack_core/src/module_factory.rs @@ -11,7 +11,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 a3824bb1c481..6efa2f352c0c 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 @@ -2013,15 +2015,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 = { @@ -6466,100 +6466,132 @@ export const rspackOptions: z.ZodObject<{ request: z.ZodOptional; contextInfo: z.ZodOptional>; }, "strict", z.ZodTypeAny, { issuer: string; + issuerLayer?: string | null | undefined; }, { issuer: string; + issuerLayer?: string | null | undefined; }>>; + getResolve: z.ZodOptional, z.ZodFunction, z.ZodUnknown>>], z.ZodUnknown>, z.ZodUnknown>>>; }, "strict", z.ZodTypeAny, { request?: string | undefined; context?: string | undefined; dependencyType?: string | undefined; contextInfo?: { issuer: string; + issuerLayer?: string | null | undefined; } | undefined; + getResolve?: ((...args: unknown[]) => (args_0: string, args_1: string, args_2: ((...args: unknown[]) => unknown) | undefined, ...args: unknown[]) => unknown) | undefined; }, { request?: string | undefined; context?: string | undefined; dependencyType?: string | undefined; contextInfo?: { issuer: string; + issuerLayer?: string | null | undefined; } | undefined; + getResolve?: ((...args: unknown[]) => (args_0: string, args_1: string, args_2: ((...args: unknown[]) => unknown) | undefined, ...args: unknown[]) => unknown) | 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, { issuer: string; + issuerLayer?: string | null | undefined; }, { issuer: string; + issuerLayer?: string | null | undefined; }>>; + getResolve: z.ZodOptional, z.ZodFunction, z.ZodUnknown>>], z.ZodUnknown>, z.ZodUnknown>>>; }, "strict", z.ZodTypeAny, { request?: string | undefined; context?: string | undefined; dependencyType?: string | undefined; contextInfo?: { issuer: string; + issuerLayer?: string | null | undefined; } | undefined; + getResolve?: ((...args: unknown[]) => (args_0: string, args_1: string, args_2: ((...args: unknown[]) => unknown) | undefined, ...args: unknown[]) => unknown) | undefined; }, { request?: string | undefined; context?: string | undefined; dependencyType?: string | undefined; contextInfo?: { issuer: string; + issuerLayer?: string | null | undefined; } | undefined; + getResolve?: ((...args: unknown[]) => (args_0: string, args_1: string, args_2: ((...args: unknown[]) => unknown) | undefined, ...args: unknown[]) => unknown) | 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, { issuer: string; + issuerLayer?: string | null | undefined; }, { issuer: string; + issuerLayer?: string | null | undefined; }>>; + getResolve: z.ZodOptional, z.ZodFunction, z.ZodUnknown>>], z.ZodUnknown>, z.ZodUnknown>>>; }, "strict", z.ZodTypeAny, { request?: string | undefined; context?: string | undefined; dependencyType?: string | undefined; contextInfo?: { issuer: string; + issuerLayer?: string | null | undefined; } | undefined; + getResolve?: ((...args: unknown[]) => (args_0: string, args_1: string, args_2: ((...args: unknown[]) => unknown) | undefined, ...args: unknown[]) => unknown) | undefined; }, { request?: string | undefined; context?: string | undefined; dependencyType?: string | undefined; contextInfo?: { issuer: string; + issuerLayer?: string | null | undefined; } | undefined; + getResolve?: ((...args: unknown[]) => (args_0: string, args_1: string, args_2: ((...args: unknown[]) => unknown) | undefined, ...args: unknown[]) => unknown) | 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, { issuer: string; + issuerLayer?: string | null | undefined; }, { issuer: string; + issuerLayer?: string | null | undefined; }>>; + getResolve: z.ZodOptional, z.ZodFunction, z.ZodUnknown>>], z.ZodUnknown>, z.ZodUnknown>>>; }, "strict", z.ZodTypeAny, { request?: string | undefined; context?: string | undefined; dependencyType?: string | undefined; contextInfo?: { issuer: string; + issuerLayer?: string | null | undefined; } | undefined; + getResolve?: ((...args: unknown[]) => (args_0: string, args_1: string, args_2: ((...args: unknown[]) => unknown) | undefined, ...args: unknown[]) => unknown) | undefined; }, { request?: string | undefined; context?: string | undefined; dependencyType?: string | undefined; contextInfo?: { issuer: string; + issuerLayer?: string | null | undefined; } | undefined; + getResolve?: ((...args: unknown[]) => (args_0: string, args_1: string, args_2: ((...args: unknown[]) => unknown) | undefined, ...args: unknown[]) => unknown) | undefined; }>], z.ZodUnknown>, z.ZodPromise, z.ZodArray]>, ZodRspackCrossChecker]>>>]>]>>; externalsType: z.ZodOptional>; externalsPresets: z.ZodOptional (args_0: string, args_1: string, args_2: ((...args: unknown[]) => unknown) | undefined, ...args: unknown[]) => unknown) | 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; + issuerLayer?: string | null | undefined; } | undefined; + getResolve?: ((...args: unknown[]) => (args_0: string, args_1: string, args_2: ((...args: unknown[]) => unknown) | undefined, ...args: unknown[]) => unknown) | undefined; }, ...args: unknown[]) => Promise) | (string | RegExp | Record | ((args_0: { request?: string | undefined; context?: string | undefined; dependencyType?: string | undefined; contextInfo?: { issuer: string; + issuerLayer?: string | null | undefined; } | undefined; + getResolve?: ((...args: unknown[]) => (args_0: string, args_1: string, args_2: ((...args: unknown[]) => unknown) | undefined, ...args: unknown[]) => unknown) | 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; + issuerLayer?: string | null | undefined; } | undefined; + getResolve?: ((...args: unknown[]) => (args_0: string, args_1: string, args_2: ((...args: unknown[]) => unknown) | undefined, ...args: unknown[]) => unknown) | undefined; }, ...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?: { @@ -9459,28 +9499,36 @@ export const rspackOptions: z.ZodObject<{ dependencyType?: string | undefined; contextInfo?: { issuer: string; + issuerLayer?: string | null | undefined; } | undefined; + getResolve?: ((...args: unknown[]) => (args_0: string, args_1: string, args_2: ((...args: unknown[]) => unknown) | undefined, ...args: unknown[]) => unknown) | 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; + issuerLayer?: string | null | undefined; } | undefined; + getResolve?: ((...args: unknown[]) => (args_0: string, args_1: string, args_2: ((...args: unknown[]) => unknown) | undefined, ...args: unknown[]) => unknown) | undefined; }, ...args: unknown[]) => Promise) | (string | RegExp | Record | ((args_0: { request?: string | undefined; context?: string | undefined; dependencyType?: string | undefined; contextInfo?: { issuer: string; + issuerLayer?: string | null | undefined; } | undefined; + getResolve?: ((...args: unknown[]) => (args_0: string, args_1: string, args_2: ((...args: unknown[]) => unknown) | undefined, ...args: unknown[]) => unknown) | 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; + issuerLayer?: string | null | undefined; } | undefined; + getResolve?: ((...args: unknown[]) => (args_0: string, args_1: string, args_2: ((...args: unknown[]) => unknown) | undefined, ...args: unknown[]) => unknown) | undefined; }, ...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?: { diff --git a/packages/rspack/src/builtin-plugin/ExternalsPlugin.ts b/packages/rspack/src/builtin-plugin/ExternalsPlugin.ts index 720ab943712d..de50c04a286f 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 5a3ba6b8c6a9..001eb8fe30c6 100644 --- a/packages/rspack/src/config/zod.ts +++ b/packages/rspack/src/config/zod.ts @@ -863,10 +863,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() @@ -876,7 +893,7 @@ const externalItem = z z .function() .args( - externalItemFunctionData, + externalItemFunctionData as z.ZodType, z .function() .args( @@ -890,14 +907,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 000000000000..e69de29bb2d1 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 042493e42a5b..000000000000 --- 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 }