-
-
Notifications
You must be signed in to change notification settings - Fork 602
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
1 parent
c3c83ed
commit b194017
Showing
28 changed files
with
385 additions
and
84 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
#![feature(try_blocks)] | ||
#![feature(let_chains)] | ||
mod options; | ||
mod plugins; | ||
pub use options::*; |
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
108 changes: 108 additions & 0 deletions
108
crates/rspack_binding_options/src/plugins/css_extract_additional_data.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,108 @@ | ||
use std::fmt::Debug; | ||
|
||
use napi::{bindgen_prelude::Unknown, Env}; | ||
use rspack_core::{ | ||
ApplyContext, CompilerOptions, NormalModuleAdditionalData, Plugin, PluginContext, | ||
}; | ||
use rspack_error::Result; | ||
use rspack_hook::{plugin, plugin_hook}; | ||
use rspack_loader_runner::AdditionalData; | ||
use rspack_napi::{threadsafe_js_value_ref::ThreadsafeJsValueRef, JsCallback, NapiResultExt}; | ||
use rspack_plugin_extract_css::{CssExtractJsonData, CssExtractJsonDataList}; | ||
use tokio::sync::oneshot; | ||
|
||
#[plugin] | ||
pub(crate) struct CssExtractRspackAdditionalDataPlugin { | ||
js_callback: JsCallback<Box<dyn FnOnce(Env) + Sync>>, | ||
} | ||
|
||
impl CssExtractRspackAdditionalDataPlugin { | ||
pub fn new(env: Env) -> Result<Self> { | ||
Ok(Self::new_inner( | ||
unsafe { JsCallback::new(env.raw()) }.into_rspack_result()?, | ||
)) | ||
} | ||
} | ||
|
||
impl Debug for CssExtractRspackAdditionalDataPlugin { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "CssExtractRspackAdditionalDataPlugin(..)") | ||
} | ||
} | ||
|
||
#[plugin_hook(NormalModuleAdditionalData for CssExtractRspackAdditionalDataPlugin)] | ||
async fn additional_data(&self, additional_data: &mut AdditionalData) -> Result<()> { | ||
if !additional_data.contains::<ThreadsafeJsValueRef<Unknown>>() { | ||
return Ok(()); | ||
} | ||
let (tx, rx) = oneshot::channel::<AdditionalData>(); | ||
let mut old_data = std::mem::take(additional_data); | ||
self.js_callback.call(Box::new(move |env| { | ||
if let Some(data) = old_data.get::<ThreadsafeJsValueRef<Unknown>>() | ||
&& let Ok(data) = data.get(env) | ||
&& let Ok(data) = data.coerce_to_object() | ||
&& let Ok(Some(data)) = data.get::<_, String>("css-extract-rspack-plugin") | ||
{ | ||
let mut list = data.split("__RSPACK_CSS_EXTRACT_SEP__"); | ||
let mut data_list = vec![]; | ||
while let Some(identifier) = list.next() { | ||
#[allow(clippy::unwrap_used)] | ||
{ | ||
// parse the css data from js loader | ||
// data: | ||
// [identifier]__RSPACK_CSS_EXTRACT_SEP__ | ||
// [content]__RSPACK_CSS_EXTRACT_SEP__ | ||
// [context]__RSPACK_CSS_EXTRACT_SEP__ | ||
// [media]__RSPACK_CSS_EXTRACT_SEP__ | ||
// [supports]__RSPACK_CSS_EXTRACT_SEP__ | ||
// [sourceMap]__RSPACK_CSS_EXTRACT_SEP__ | ||
// [identifier]__RSPACK_CSS_EXTRACT_SEP__ ... repeated | ||
// [content]__RSPACK_CSS_EXTRACT_SEP__ | ||
data_list.push(CssExtractJsonData { | ||
identifier: identifier.into(), | ||
content: list.next().unwrap().into(), | ||
context: list.next().unwrap().into(), | ||
media: list.next().unwrap().into(), | ||
supports: list.next().unwrap().into(), | ||
source_map: list.next().unwrap().into(), | ||
identifier_index: list | ||
.next() | ||
.unwrap() | ||
.parse() | ||
.expect("Cannot parse identifier_index, this should never happen"), | ||
filepath: list.next().unwrap().into(), | ||
}); | ||
} | ||
} | ||
old_data.insert(CssExtractJsonDataList(data_list)); | ||
}; | ||
tx.send(old_data) | ||
.expect("should send `additional_data` for `CssExtractRspackAdditionalDataPlugin`"); | ||
})); | ||
let new_data = rx | ||
.await | ||
.expect("should receive `additional_data` for `CssExtractRspackAdditionalDataPlugin`"); | ||
// ignore the default value here | ||
let _ = std::mem::replace(additional_data, new_data); | ||
Ok(()) | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl Plugin for CssExtractRspackAdditionalDataPlugin { | ||
fn name(&self) -> &'static str { | ||
"CssExtractRspackAdditionalDataPlugin" | ||
} | ||
|
||
fn apply( | ||
&self, | ||
ctx: PluginContext<&mut ApplyContext>, | ||
_options: &mut CompilerOptions, | ||
) -> Result<()> { | ||
ctx | ||
.context | ||
.normal_module_hooks | ||
.additional_data | ||
.tap(additional_data::new(self)); | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
mod css_extract_additional_data; | ||
mod js_loader_resolver; | ||
pub(super) use css_extract_additional_data::CssExtractRspackAdditionalDataPlugin; | ||
pub(super) use js_loader_resolver::JsLoaderResolverPlugin; |
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,44 @@ | ||
use std::marker::PhantomData; | ||
|
||
use napi::bindgen_prelude::*; | ||
use napi::{Env, NapiValue, Ref}; | ||
|
||
pub struct JsValueRef<T: NapiValue> { | ||
ref_: Ref<()>, | ||
_phantom: PhantomData<T>, | ||
} | ||
|
||
impl<T: NapiValue> JsValueRef<T> { | ||
pub fn new(env: Env, value: T) -> Result<Self> { | ||
let ref_ = env.create_reference(value)?; | ||
|
||
Ok(Self { | ||
ref_, | ||
_phantom: PhantomData, | ||
}) | ||
} | ||
|
||
pub fn get(&self, env: Env) -> Result<T> { | ||
env.get_reference_value(&self.ref_) | ||
} | ||
|
||
pub fn unref(&mut self, env: Env) -> Result<u32> { | ||
self.ref_.unref(env) | ||
} | ||
} | ||
|
||
impl<T: NapiValue> ToNapiValue for JsValueRef<T> { | ||
unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> Result<sys::napi_value> { | ||
val | ||
.get(Env::from(env)) | ||
.and_then(|v| unsafe { T::to_napi_value(env, v) }) | ||
} | ||
} | ||
|
||
impl<T: NapiValue> FromNapiValue for JsValueRef<T> { | ||
unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> Result<Self> { | ||
JsValueRef::<T>::new(Env::from(env), unsafe { | ||
T::from_napi_value(env, napi_val) | ||
}?) | ||
} | ||
} |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod js_reg_exp; | ||
pub mod js_value_ref; |
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.
b194017
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 Ran ecosystem CI: Open
b194017
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 Benchmark detail: Open