Skip to content

Commit 48eff5e

Browse files
feat: NormalModuleFactory and resolveForScheme hook (#2356)
* chore: πŸ€– init nmf * feat: 🎸 finish normalmodulefactory resolveForScheme * chore: πŸ€– recover * chore: πŸ€– recover * chore: πŸ€– recover * chore: πŸ€– lint * chore: πŸ€– compile * chore: πŸ€– fix compile error * chore: πŸ€– changeset
1 parent 68c4df8 commit 48eff5e

File tree

16 files changed

+350
-92
lines changed

16 files changed

+350
-92
lines changed

β€Ž.changeset/hungry-otters-kneel.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@rspack/binding": patch
3+
"@rspack/core": patch
4+
---
5+
6+
feat: add normalModuleFactory and resolveForScheme hook

β€Ž.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,4 @@ npm/**/*.node
194194

195195
# Precompiled config schema
196196
packages/rspack/src/config/schema.check.js
197+
justfile

β€Žcrates/loader_runner/src/runner.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_hash::FxHashSet as HashSet;
1010

1111
use crate::{Content, LoaderRunnerPlugin};
1212

13-
#[derive(Debug, Clone)]
13+
#[derive(Debug, Clone, Default)]
1414
pub struct ResourceData {
1515
/// Resource with absolute path, query and fragment
1616
pub resource: String,

β€Žcrates/node_binding/binding.d.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,12 +426,27 @@ export interface JsHooks {
426426
afterEmit: (...args: any[]) => any
427427
make: (...args: any[]) => any
428428
optimizeChunkModule: (...args: any[]) => any
429+
normalModuleFactoryResolveForScheme: (...args: any[]) => any
429430
}
430431
export interface JsModule {
431432
originalSource?: JsCompatSource
432433
resource: string
433434
moduleIdentifier: string
434435
}
436+
export interface SchemeAndJsResourceData {
437+
resourceData: JsResourceData
438+
scheme: string
439+
}
440+
export interface JsResourceData {
441+
/** Resource with absolute path, query and fragment */
442+
resource: string
443+
/** Absolute resource path only */
444+
path: string
445+
/** Resource query with `?` prefix */
446+
query?: string
447+
/** Resource fragment with `#` prefix */
448+
fragment?: string
449+
}
435450
export interface JsCompatSource {
436451
/** Whether the underlying data structure is a `RawSource` */
437452
isRaw: boolean

β€Žcrates/node_binding/src/js_values/hooks.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ pub struct JsHooks {
1414
pub after_emit: JsFunction,
1515
pub make: JsFunction,
1616
pub optimize_chunk_module: JsFunction,
17+
pub normal_module_factory_resolve_for_scheme: JsFunction,
1718
}

β€Žcrates/node_binding/src/js_values/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod chunk_group;
44
mod compilation;
55
mod hooks;
66
mod module;
7+
mod normal_module_factory;
78
mod source;
89
mod stats;
910

@@ -12,5 +13,6 @@ pub use chunk::*;
1213
pub use chunk_group::*;
1314
pub use compilation::*;
1415
pub use hooks::*;
16+
pub use normal_module_factory::*;
1517
pub use source::*;
1618
pub use stats::*;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use rspack_core::{NormalModuleFactoryResolveForSchemeArgs, ResourceData};
2+
3+
#[napi(object)]
4+
pub struct SchemeAndJsResourceData {
5+
pub resource_data: JsResourceData,
6+
pub scheme: String,
7+
}
8+
9+
#[napi(object)]
10+
pub struct JsResourceData {
11+
/// Resource with absolute path, query and fragment
12+
pub resource: String,
13+
/// Absolute resource path only
14+
pub path: String,
15+
/// Resource query with `?` prefix
16+
pub query: Option<String>,
17+
/// Resource fragment with `#` prefix
18+
pub fragment: Option<String>,
19+
}
20+
21+
impl From<ResourceData> for JsResourceData {
22+
fn from(value: ResourceData) -> Self {
23+
Self {
24+
resource: value.resource,
25+
path: value.resource_path.to_string_lossy().to_string(),
26+
query: value.resource_query,
27+
fragment: value.resource_fragment,
28+
}
29+
}
30+
}
31+
32+
impl From<NormalModuleFactoryResolveForSchemeArgs> for SchemeAndJsResourceData {
33+
fn from(value: NormalModuleFactoryResolveForSchemeArgs) -> Self {
34+
Self {
35+
resource_data: value.resource.into(),
36+
scheme: value.scheme,
37+
}
38+
}
39+
}

β€Žcrates/node_binding/src/plugins/mod.rs

Lines changed: 59 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
use std::fmt::Debug;
2+
use std::path::PathBuf;
23

34
use async_trait::async_trait;
45
use napi::{Env, Result};
56
use rspack_binding_macros::js_fn_into_theadsafe_fn;
7+
use rspack_core::{
8+
NormalModuleFactoryResolveForSchemeArgs, PluginNormalModuleFactoryResolveForSchemeOutput,
9+
ResourceData,
10+
};
611
use rspack_error::internal_error;
712
use rspack_napi_shared::threadsafe_function::{ThreadsafeFunction, ThreadsafeFunctionCallMode};
813
use rspack_napi_shared::NapiResultExt;
914

15+
use crate::js_values::{JsResourceData, SchemeAndJsResourceData};
1016
use crate::{DisabledHooks, Hook, JsCompilation, JsHooks};
1117

1218
pub struct JsHooksAdapter {
@@ -23,6 +29,8 @@ pub struct JsHooksAdapter {
2329
pub emit_tsfn: ThreadsafeFunction<(), ()>,
2430
pub after_emit_tsfn: ThreadsafeFunction<(), ()>,
2531
pub optimize_chunk_modules_tsfn: ThreadsafeFunction<JsCompilation, ()>,
32+
pub normal_module_factory_resolve_for_scheme:
33+
ThreadsafeFunction<SchemeAndJsResourceData, JsResourceData>,
2634
}
2735

2836
impl Debug for JsHooksAdapter {
@@ -100,6 +108,28 @@ impl rspack_core::Plugin for JsHooksAdapter {
100108
.map_err(|err| internal_error!("Failed to call make: {err}",))?
101109
}
102110

111+
async fn normal_module_factory_resolve_for_scheme(
112+
&self,
113+
_ctx: rspack_core::PluginContext,
114+
args: &NormalModuleFactoryResolveForSchemeArgs,
115+
) -> PluginNormalModuleFactoryResolveForSchemeOutput {
116+
let res = self
117+
.normal_module_factory_resolve_for_scheme
118+
.call(args.clone().into(), ThreadsafeFunctionCallMode::NonBlocking)
119+
.into_rspack_result()?
120+
.await
121+
.map_err(|err| internal_error!("Failed to call this_compilation: {err}"))?;
122+
res.map(|res| {
123+
Some(ResourceData {
124+
resource: res.resource,
125+
resource_fragment: res.fragment,
126+
resource_path: PathBuf::from(res.path),
127+
resource_query: res.query,
128+
resource_description: None,
129+
})
130+
})
131+
}
132+
103133
async fn process_assets_stage_additional(
104134
&mut self,
105135
_ctx: rspack_core::PluginContext,
@@ -205,6 +235,28 @@ impl rspack_core::Plugin for JsHooksAdapter {
205235
.map_err(|err| internal_error!("Failed to call process assets stage report: {err}",))?
206236
}
207237

238+
async fn optimize_chunk_modules(
239+
&mut self,
240+
args: rspack_core::OptimizeChunksArgs<'_>,
241+
) -> rspack_error::Result<()> {
242+
if self.is_hook_disabled(&Hook::OptimizeChunkModules) {
243+
return Ok(());
244+
}
245+
246+
let compilation = JsCompilation::from_compilation(unsafe {
247+
std::mem::transmute::<&'_ mut rspack_core::Compilation, &'static mut rspack_core::Compilation>(
248+
args.compilation,
249+
)
250+
});
251+
252+
self
253+
.optimize_chunk_modules_tsfn
254+
.call(compilation, ThreadsafeFunctionCallMode::NonBlocking)
255+
.into_rspack_result()?
256+
.await
257+
.map_err(|err| internal_error!("Failed to compilation: {err}"))?
258+
}
259+
208260
async fn emit(&mut self, _: &mut rspack_core::Compilation) -> rspack_error::Result<()> {
209261
if self.is_hook_disabled(&Hook::Emit) {
210262
return Ok(());
@@ -230,28 +282,6 @@ impl rspack_core::Plugin for JsHooksAdapter {
230282
.await
231283
.map_err(|err| internal_error!("Failed to call after emit: {err}",))?
232284
}
233-
234-
async fn optimize_chunk_modules(
235-
&mut self,
236-
args: rspack_core::OptimizeChunksArgs<'_>,
237-
) -> rspack_error::Result<()> {
238-
if self.is_hook_disabled(&Hook::OptimizeChunkModules) {
239-
return Ok(());
240-
}
241-
242-
let compilation = JsCompilation::from_compilation(unsafe {
243-
std::mem::transmute::<&'_ mut rspack_core::Compilation, &'static mut rspack_core::Compilation>(
244-
args.compilation,
245-
)
246-
});
247-
248-
self
249-
.optimize_chunk_modules_tsfn
250-
.call(compilation, ThreadsafeFunctionCallMode::NonBlocking)
251-
.into_rspack_result()?
252-
.await
253-
.map_err(|err| internal_error!("Failed to compilation: {err}"))?
254-
}
255285
}
256286

257287
impl JsHooksAdapter {
@@ -269,6 +299,7 @@ impl JsHooksAdapter {
269299
emit,
270300
after_emit,
271301
optimize_chunk_module,
302+
normal_module_factory_resolve_for_scheme,
272303
} = js_hooks;
273304

274305
let process_assets_stage_additional_tsfn: ThreadsafeFunction<(), ()> =
@@ -293,6 +324,11 @@ impl JsHooksAdapter {
293324
let optimize_chunk_modules_tsfn: ThreadsafeFunction<JsCompilation, ()> =
294325
js_fn_into_theadsafe_fn!(optimize_chunk_module, env);
295326

327+
let normal_module_factory_resolve_for_scheme: ThreadsafeFunction<
328+
SchemeAndJsResourceData,
329+
JsResourceData,
330+
> = js_fn_into_theadsafe_fn!(normal_module_factory_resolve_for_scheme, env);
331+
296332
Ok(JsHooksAdapter {
297333
disabled_hooks,
298334
make_tsfn,
@@ -307,6 +343,7 @@ impl JsHooksAdapter {
307343
emit_tsfn,
308344
after_emit_tsfn,
309345
optimize_chunk_modules_tsfn,
346+
normal_module_factory_resolve_for_scheme,
310347
})
311348
}
312349

0 commit comments

Comments
Β (0)