Skip to content

Commit

Permalink
fix: types
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Dec 3, 2024
1 parent 6585bfa commit 06e849d
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 24 deletions.
1 change: 0 additions & 1 deletion crates/rspack_binding_options/src/options/raw_external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_core/src/context_module_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down
1 change: 0 additions & 1 deletion crates/rspack_core/src/module_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use crate::{
#[derive(Debug, Clone)]
pub struct ModuleFactoryCreateData {
pub compilation_id: CompilationId,
// pub resolve_options: Option<Box<Resolve>>,
pub resolve_options: Option<Arc<Resolve>>,
pub options: Arc<CompilerOptions>,
pub context: Context,
Expand Down
7 changes: 5 additions & 2 deletions crates/rspack_plugin_externals/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,11 @@ async fn factorize(&self, data: &mut ModuleFactoryCreateData) -> Result<Option<B
.clone()
.map(|r| Box::new(Arc::unwrap_or_clone(r))),
resolve_to_context: false,
// dependency_category: *data.dependency.category(),
dependency_category: *data.dependencies.get(0).unwrap().category(),
dependency_category: *data
.dependencies
.first()
.expect("Expected at least one dependency")
.category(),
},
resolver_factory: data.resolver_factory.clone(),
})
Expand Down
66 changes: 57 additions & 9 deletions packages/rspack/etc/core.api.md

Large diffs are not rendered by default.

11 changes: 9 additions & 2 deletions packages/rspack/src/builtin-plugin/ExternalsPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
28 changes: 22 additions & 6 deletions packages/rspack/src/config/zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof externalItemFunctionData>;
}) satisfies z.ZodType<t.ExternalItemFunctionData>;

const externalItem = z
.string()
Expand All @@ -876,7 +893,7 @@ const externalItem = z
z
.function()
.args(
externalItemFunctionData,
externalItemFunctionData as z.ZodType<t.ExternalItemFunctionData>,
z
.function()
.args(
Expand All @@ -890,14 +907,13 @@ const externalItem = z
.or(
z
.function()
.args(externalItemFunctionData)
.args(externalItemFunctionData as z.ZodType<t.ExternalItemFunctionData>)
.returns(z.promise(externalItemValue))
) satisfies z.ZodType<t.ExternalItem>;

const externals = externalItem
.array()
.or(externalItem) satisfies z.ZodType<t.Externals>;
//#endregion

//#region ExternalsPresets
const externalsPresets = z.strictObject({
Expand Down
Empty file.

This file was deleted.

0 comments on commit 06e849d

Please sign in to comment.