-
-
Notifications
You must be signed in to change notification settings - Fork 576
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
37 changed files
with
1,208 additions
and
223 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
crates/rspack_binding_options/src/options/raw_builtins/raw_lazy_compilation.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
use napi::{Env, JsFunction, Result}; | ||
use napi_derive::napi; | ||
use rspack_binding_macros::js_fn_into_threadsafe_fn; | ||
use rspack_core::ModuleIdentifier; | ||
use rspack_napi_shared::{ | ||
get_napi_env, | ||
threadsafe_function::{ThreadsafeFunction, ThreadsafeFunctionCallMode}, | ||
}; | ||
use rspack_plugin_lazy_compilation::backend::{Backend, ModuleInfo}; | ||
|
||
use crate::RawRegexMatcher; | ||
|
||
#[napi(object)] | ||
pub struct RawModuleInfo { | ||
pub active: bool, | ||
pub client: String, | ||
pub data: String, | ||
} | ||
|
||
#[napi(object)] | ||
pub struct RawLazyCompilationOption { | ||
pub module: JsFunction, | ||
pub dispose: JsFunction, | ||
pub test: Option<RawRegexMatcher>, | ||
pub entries: bool, | ||
pub imports: bool, | ||
} | ||
|
||
#[napi(object)] | ||
pub struct RawModuleArg { | ||
pub module: String, | ||
pub path: String, | ||
} | ||
|
||
pub(crate) struct JsBackend { | ||
module: ThreadsafeFunction<RawModuleArg, RawModuleInfo>, | ||
dispose: ThreadsafeFunction<(), ()>, | ||
} | ||
|
||
impl std::fmt::Debug for JsBackend { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
f.debug_struct("JsBackend").finish() | ||
} | ||
} | ||
|
||
impl TryFrom<&RawLazyCompilationOption> for JsBackend { | ||
type Error = napi::Error; | ||
fn try_from(value: &RawLazyCompilationOption) -> Result<Self> { | ||
Ok(Self { | ||
module: js_fn_into_threadsafe_fn!(value.module, Env::from(get_napi_env())), | ||
dispose: js_fn_into_threadsafe_fn!(value.dispose, Env::from(get_napi_env())), | ||
}) | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl Backend for JsBackend { | ||
async fn module(&mut self, identifier: ModuleIdentifier, path: String) -> ModuleInfo { | ||
let module_info = self | ||
.module | ||
.call( | ||
RawModuleArg { | ||
module: identifier.to_string(), | ||
path, | ||
}, | ||
ThreadsafeFunctionCallMode::NonBlocking, | ||
) | ||
.unwrap() | ||
.await | ||
.unwrap() | ||
.unwrap(); | ||
|
||
ModuleInfo { | ||
active: module_info.active, | ||
client: module_info.client, | ||
data: module_info.data, | ||
} | ||
} | ||
|
||
async fn dispose(&mut self) { | ||
self | ||
.dispose | ||
.call((), ThreadsafeFunctionCallMode::NonBlocking) | ||
.unwrap() | ||
.await | ||
.unwrap() | ||
.unwrap(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.