-
-
Notifications
You must be signed in to change notification settings - Fork 579
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: rspack_cacheable support rspack_sources::BoxSource (#8527)
* feat: rspack_cacheable support rspack_sources::BoxSource * fix: ci
- Loading branch information
1 parent
8c3a2b0
commit b09e6b9
Showing
10 changed files
with
112 additions
and
216 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 was deleted.
Oops, something went wrong.
This file was deleted.
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
103 changes: 0 additions & 103 deletions
103
crates/rspack_cacheable/src/with/as_preset/rspack_source/mod.rs
This file was deleted.
Oops, something went wrong.
86 changes: 86 additions & 0 deletions
86
crates/rspack_cacheable/src/with/as_preset/rspack_sources/mod.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,86 @@ | ||
use rkyv::{ | ||
rancor::Fallible, | ||
ser::{Allocator, Writer}, | ||
with::{ArchiveWith, DeserializeWith, SerializeWith}, | ||
Archive, Archived, Deserialize, Place, Resolver, Serialize, | ||
}; | ||
use rspack_sources::{ | ||
BoxSource, RawSource, Source, SourceExt, SourceMap, SourceMapSource, WithoutOriginalOptions, | ||
}; | ||
|
||
use super::AsPreset; | ||
use crate::{cacheable, DeserializeError, SerializeError}; | ||
|
||
#[cacheable(crate=crate)] | ||
pub struct CacheableSource { | ||
buffer: Vec<u8>, | ||
map: Option<String>, | ||
} | ||
|
||
pub struct InnerResolver { | ||
source: CacheableSource, | ||
resolver: Resolver<CacheableSource>, | ||
} | ||
|
||
impl ArchiveWith<BoxSource> for AsPreset { | ||
type Archived = Archived<CacheableSource>; | ||
type Resolver = InnerResolver; | ||
|
||
#[inline] | ||
fn resolve_with(_field: &BoxSource, resolver: Self::Resolver, out: Place<Self::Archived>) { | ||
let InnerResolver { source, resolver } = resolver; | ||
source.resolve(resolver, out) | ||
} | ||
} | ||
|
||
impl<S> SerializeWith<BoxSource, S> for AsPreset | ||
where | ||
S: Fallible<Error = SerializeError> + Allocator + Writer, | ||
{ | ||
fn serialize_with( | ||
field: &BoxSource, | ||
serializer: &mut S, | ||
) -> Result<Self::Resolver, SerializeError> { | ||
let map = match field.map(&Default::default()) { | ||
Some(map) => Some( | ||
map | ||
.to_json() | ||
.map_err(|_| SerializeError::MessageError("source map to json failed"))?, | ||
), | ||
None => None, | ||
}; | ||
let source = CacheableSource { | ||
buffer: field.buffer().to_vec(), | ||
map, | ||
}; | ||
Ok(InnerResolver { | ||
resolver: source.serialize(serializer)?, | ||
source, | ||
}) | ||
} | ||
} | ||
|
||
impl<D> DeserializeWith<Archived<CacheableSource>, BoxSource, D> for AsPreset | ||
where | ||
D: Fallible<Error = DeserializeError>, | ||
{ | ||
fn deserialize_with( | ||
field: &Archived<CacheableSource>, | ||
deserializer: &mut D, | ||
) -> Result<BoxSource, DeserializeError> { | ||
let CacheableSource { buffer, map } = field.deserialize(deserializer)?; | ||
if let Some(map) = &map { | ||
if let Ok(source_map) = SourceMap::from_json(map) { | ||
return Ok( | ||
SourceMapSource::new(WithoutOriginalOptions { | ||
value: String::from_utf8_lossy(&buffer), | ||
name: "persistent-cache", | ||
source_map, | ||
}) | ||
.boxed(), | ||
); | ||
} | ||
} | ||
Ok(RawSource::from(buffer).boxed()) | ||
} | ||
} |
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
22 changes: 22 additions & 0 deletions
22
crates/rspack_cacheable_test/tests/with/as_preset/rspack_sources.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,22 @@ | ||
use rspack_cacheable::{cacheable, from_bytes, to_bytes, with::AsPreset}; | ||
use rspack_sources::{BoxSource, RawSource, SourceExt}; | ||
|
||
#[cacheable] | ||
#[derive(Debug)] | ||
struct Data(#[cacheable(with=AsPreset)] BoxSource); | ||
|
||
#[test] | ||
fn test_rspack_source() { | ||
fn test_data(data: Data) { | ||
let bytes = to_bytes(&data, &()).unwrap(); | ||
let new_data: Data = from_bytes(&bytes, &()).unwrap(); | ||
assert_eq!(data.0.buffer(), new_data.0.buffer()); | ||
assert_eq!( | ||
data.0.map(&Default::default()), | ||
new_data.0.map(&Default::default()) | ||
); | ||
} | ||
|
||
test_data(Data(RawSource::from("123".as_bytes()).boxed())); | ||
test_data(Data(RawSource::from("123").boxed())); | ||
} |
b09e6b9
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
b09e6b9
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