Skip to content

Commit 6b73fc7

Browse files
committed
perf: ExternalItemFnCtx
1 parent b5fc80f commit 6b73fc7

File tree

4 files changed

+15
-14
lines changed

4 files changed

+15
-14
lines changed

crates/rspack_binding_api/src/raw_options/raw_external.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use napi::{
66
};
77
use napi_derive::napi;
88
use rspack_core::{
9-
ExternalItem, ExternalItemFnCtx, ExternalItemFnResult, ExternalItemValue,
10-
ResolveOptionsWithDependencyType, ResolverFactory,
9+
Context, DependencyCategory, ExternalItem, ExternalItemFnCtx, ExternalItemFnResult,
10+
ExternalItemValue, ResolveOptionsWithDependencyType, ResolverFactory,
1111
};
1212
use rspack_napi::threadsafe_function::ThreadsafeFunction;
1313
use rspack_regex::RspackRegex;
@@ -90,8 +90,8 @@ pub struct RawExternalItemFnCtxData<'a> {
9090
#[derive(Debug)]
9191
struct RawExternalItemFnCtxInner {
9292
request: String,
93-
context: String,
94-
dependency_type: String,
93+
context: Context,
94+
dependency_type: DependencyCategory,
9595
context_info: ContextInfo,
9696
resolve_options_with_dependency_type: Arc<ResolveOptionsWithDependencyType>,
9797
resolver_factory: Arc<ResolverFactory>,

crates/rspack_core/src/options/externals.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rspack_error::Result;
55
use rspack_regex::RspackRegex;
66
use rustc_hash::FxHashMap as HashMap;
77

8-
use crate::{ResolveOptionsWithDependencyType, ResolverFactory};
8+
use crate::{Context, DependencyCategory, ResolveOptionsWithDependencyType, ResolverFactory};
99

1010
pub type Externals = Vec<ExternalItem>;
1111

@@ -26,8 +26,8 @@ pub struct ContextInfo {
2626

2727
pub struct ExternalItemFnCtx {
2828
pub request: String,
29-
pub context: String,
30-
pub dependency_type: String,
29+
pub context: Context,
30+
pub dependency_type: DependencyCategory,
3131
pub context_info: ContextInfo,
3232
pub resolve_options_with_dependency_type: ResolveOptionsWithDependencyType,
3333
pub resolver_factory: Arc<ResolverFactory>,

crates/rspack_plugin_externals/src/http_externals_plugin.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use rspack_core::{
2-
BoxPlugin, ExternalItem, ExternalItemFnCtx, ExternalItemFnResult, ExternalItemValue, PluginExt,
2+
BoxPlugin, DependencyCategory, ExternalItem, ExternalItemFnCtx, ExternalItemFnResult,
3+
ExternalItemValue, PluginExt,
34
};
45

56
use crate::ExternalsPlugin;
@@ -25,14 +26,14 @@ pub fn http_externals_rspack_plugin(css: bool, web_async: bool) -> BoxPlugin {
2526
fn http_external_item_web(css: bool) -> ExternalItem {
2627
ExternalItem::Fn(Box::new(move |ctx: ExternalItemFnCtx| {
2728
Box::pin(async move {
28-
if ctx.dependency_type == "url" {
29+
if ctx.dependency_type == DependencyCategory::Url {
2930
if is_external_http_request(&ctx.request) {
3031
return Ok(ExternalItemFnResult {
3132
external_type: Some("asset".to_owned()),
3233
result: Some(ExternalItemValue::String(ctx.request)),
3334
});
3435
}
35-
} else if css && ctx.dependency_type == "css-import" {
36+
} else if css && ctx.dependency_type == DependencyCategory::CssImport {
3637
if is_external_http_request(&ctx.request) {
3738
return Ok(ExternalItemFnResult {
3839
external_type: Some("css-import".to_owned()),
@@ -63,14 +64,14 @@ fn http_external_item_web(css: bool) -> ExternalItem {
6364
fn http_external_item_web_async(css: bool) -> ExternalItem {
6465
ExternalItem::Fn(Box::new(move |ctx: ExternalItemFnCtx| {
6566
Box::pin(async move {
66-
if ctx.dependency_type == "url" {
67+
if ctx.dependency_type == DependencyCategory::Url {
6768
if is_external_http_request(&ctx.request) {
6869
return Ok(ExternalItemFnResult {
6970
external_type: Some("asset".to_owned()),
7071
result: Some(ExternalItemValue::String(ctx.request)),
7172
});
7273
}
73-
} else if css && ctx.dependency_type == "css-import" {
74+
} else if css && ctx.dependency_type == DependencyCategory::CssImport {
7475
if is_external_http_request(&ctx.request) {
7576
return Ok(ExternalItemFnResult {
7677
external_type: Some("css-import".to_owned()),

crates/rspack_plugin_externals/src/plugin.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,9 @@ async fn factorize(&self, data: &mut ModuleFactoryCreateData) -> Result<Option<B
184184
ExternalItem::Fn(f) => {
185185
let request = dependency.request();
186186
let result = f(ExternalItemFnCtx {
187-
context: context.to_string(),
187+
context: context.clone(),
188188
request: request.to_string(),
189-
dependency_type: dependency.category().to_string(),
189+
dependency_type: *dependency.category(),
190190
context_info: ContextInfo {
191191
issuer: data
192192
.issuer

0 commit comments

Comments
 (0)