-
-
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
32 changed files
with
1,157 additions
and
215 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
93 changes: 93 additions & 0 deletions
93
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,93 @@ | ||
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, | ||
) -> rspack_error::Result<ModuleInfo> { | ||
let module_info = self | ||
.module | ||
.call( | ||
RawModuleArg { | ||
module: identifier.to_string(), | ||
path, | ||
}, | ||
ThreadsafeFunctionCallMode::NonBlocking, | ||
) | ||
.map_err(|err| rspack_error::error!("failed to invoke backend.module: {}", err))? | ||
.await | ||
.expect("channel should have result")?; | ||
|
||
Ok(ModuleInfo { | ||
active: module_info.active, | ||
client: module_info.client, | ||
data: module_info.data, | ||
}) | ||
} | ||
|
||
async fn dispose(&mut self) -> rspack_error::Result<()> { | ||
self | ||
.dispose | ||
.call((), ThreadsafeFunctionCallMode::NonBlocking) | ||
.map_err(|err| rspack_error::error!("failed to invoke backend.dispose: {}", err))? | ||
.await | ||
.expect("channel should have result")?; | ||
|
||
Ok(()) | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
[package] | ||
edition = "2021" | ||
license = "MIT" | ||
name = "rspack_plugin_lazy_compilation" | ||
version = "0.1.0" | ||
|
||
[dependencies] | ||
async-trait = { workspace = true } | ||
dashmap = { workspace = true } | ||
once_cell = { workspace = true } | ||
regex = { workspace = true } | ||
rustc-hash = { workspace = true } | ||
tokio = { workspace = true } | ||
urlencoding = { workspace = true } | ||
|
||
rspack_core = { path = "../rspack_core" } | ||
rspack_error = { path = "../rspack_error" } | ||
rspack_identifier = { path = "../rspack_identifier" } | ||
rspack_plugin_javascript = { path = "../rspack_plugin_javascript" } | ||
rspack_regex = { path = "../rspack_regex" } | ||
rspack_util = { path = "../rspack_util" } |
Oops, something went wrong.