Skip to content

Commit b09e6b9

Browse files
authored
feat: rspack_cacheable support rspack_sources::BoxSource (#8527)
* feat: rspack_cacheable support rspack_sources::BoxSource * fix: ci
1 parent 8c3a2b0 commit b09e6b9

File tree

10 files changed

+112
-216
lines changed

10 files changed

+112
-216
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/rspack_cacheable/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ pub use rspack_macros::{cacheable, cacheable_dyn};
33
#[cfg(feature = "noop")]
44
pub use rspack_macros::{disable_cacheable as cacheable, disable_cacheable_dyn as cacheable_dyn};
55
pub mod r#dyn;
6-
pub mod utils;
76
pub mod with;
87

98
mod context;

crates/rspack_cacheable/src/utils/mod.rs

Lines changed: 0 additions & 3 deletions
This file was deleted.

crates/rspack_cacheable/src/utils/type_wrapper.rs

Lines changed: 0 additions & 108 deletions
This file was deleted.

crates/rspack_cacheable/src/with/as_preset/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ mod camino;
22
mod json;
33
mod lightningcss;
44
mod rspack_resolver;
5-
mod rspack_source;
5+
mod rspack_sources;
66
mod serde_json;
77
mod swc;
88
mod ustr;

crates/rspack_cacheable/src/with/as_preset/rspack_source/mod.rs

Lines changed: 0 additions & 103 deletions
This file was deleted.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
use rkyv::{
2+
rancor::Fallible,
3+
ser::{Allocator, Writer},
4+
with::{ArchiveWith, DeserializeWith, SerializeWith},
5+
Archive, Archived, Deserialize, Place, Resolver, Serialize,
6+
};
7+
use rspack_sources::{
8+
BoxSource, RawSource, Source, SourceExt, SourceMap, SourceMapSource, WithoutOriginalOptions,
9+
};
10+
11+
use super::AsPreset;
12+
use crate::{cacheable, DeserializeError, SerializeError};
13+
14+
#[cacheable(crate=crate)]
15+
pub struct CacheableSource {
16+
buffer: Vec<u8>,
17+
map: Option<String>,
18+
}
19+
20+
pub struct InnerResolver {
21+
source: CacheableSource,
22+
resolver: Resolver<CacheableSource>,
23+
}
24+
25+
impl ArchiveWith<BoxSource> for AsPreset {
26+
type Archived = Archived<CacheableSource>;
27+
type Resolver = InnerResolver;
28+
29+
#[inline]
30+
fn resolve_with(_field: &BoxSource, resolver: Self::Resolver, out: Place<Self::Archived>) {
31+
let InnerResolver { source, resolver } = resolver;
32+
source.resolve(resolver, out)
33+
}
34+
}
35+
36+
impl<S> SerializeWith<BoxSource, S> for AsPreset
37+
where
38+
S: Fallible<Error = SerializeError> + Allocator + Writer,
39+
{
40+
fn serialize_with(
41+
field: &BoxSource,
42+
serializer: &mut S,
43+
) -> Result<Self::Resolver, SerializeError> {
44+
let map = match field.map(&Default::default()) {
45+
Some(map) => Some(
46+
map
47+
.to_json()
48+
.map_err(|_| SerializeError::MessageError("source map to json failed"))?,
49+
),
50+
None => None,
51+
};
52+
let source = CacheableSource {
53+
buffer: field.buffer().to_vec(),
54+
map,
55+
};
56+
Ok(InnerResolver {
57+
resolver: source.serialize(serializer)?,
58+
source,
59+
})
60+
}
61+
}
62+
63+
impl<D> DeserializeWith<Archived<CacheableSource>, BoxSource, D> for AsPreset
64+
where
65+
D: Fallible<Error = DeserializeError>,
66+
{
67+
fn deserialize_with(
68+
field: &Archived<CacheableSource>,
69+
deserializer: &mut D,
70+
) -> Result<BoxSource, DeserializeError> {
71+
let CacheableSource { buffer, map } = field.deserialize(deserializer)?;
72+
if let Some(map) = &map {
73+
if let Ok(source_map) = SourceMap::from_json(map) {
74+
return Ok(
75+
SourceMapSource::new(WithoutOriginalOptions {
76+
value: String::from_utf8_lossy(&buffer),
77+
name: "persistent-cache",
78+
source_map,
79+
})
80+
.boxed(),
81+
);
82+
}
83+
}
84+
Ok(RawSource::from(buffer).boxed())
85+
}
86+
}

crates/rspack_cacheable_test/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ lightningcss = { workspace = true }
1515
once_cell = { workspace = true }
1616
rspack_cacheable = { path = "../rspack_cacheable" }
1717
rspack_resolver = { workspace = true }
18+
rspack_sources = { workspace = true }
1819
rustc-hash = { workspace = true }
1920
serde_json = { workspace = true }
2021
swc_core = { workspace = true, features = ["ecma_ast"] }

crates/rspack_cacheable_test/tests/with/as_preset/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ mod camino;
22
mod json;
33
mod lightningcss;
44
mod rspack_resolver;
5+
mod rspack_sources;
56
mod serde_json;
67
mod swc;
78
mod ustr;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use rspack_cacheable::{cacheable, from_bytes, to_bytes, with::AsPreset};
2+
use rspack_sources::{BoxSource, RawSource, SourceExt};
3+
4+
#[cacheable]
5+
#[derive(Debug)]
6+
struct Data(#[cacheable(with=AsPreset)] BoxSource);
7+
8+
#[test]
9+
fn test_rspack_source() {
10+
fn test_data(data: Data) {
11+
let bytes = to_bytes(&data, &()).unwrap();
12+
let new_data: Data = from_bytes(&bytes, &()).unwrap();
13+
assert_eq!(data.0.buffer(), new_data.0.buffer());
14+
assert_eq!(
15+
data.0.map(&Default::default()),
16+
new_data.0.map(&Default::default())
17+
);
18+
}
19+
20+
test_data(Data(RawSource::from("123".as_bytes()).boxed()));
21+
test_data(Data(RawSource::from("123").boxed()));
22+
}

0 commit comments

Comments
 (0)