Skip to content

Commit

Permalink
feat: rspack_cacheable support rspack_sources::BoxSource (web-infra-d…
Browse files Browse the repository at this point in the history
…ev#8527)

* feat: rspack_cacheable support rspack_sources::BoxSource

* fix: ci
  • Loading branch information
jerrykingxyz authored Nov 25, 2024
1 parent 8c3a2b0 commit b09e6b9
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 216 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/rspack_cacheable/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ pub use rspack_macros::{cacheable, cacheable_dyn};
#[cfg(feature = "noop")]
pub use rspack_macros::{disable_cacheable as cacheable, disable_cacheable_dyn as cacheable_dyn};
pub mod r#dyn;
pub mod utils;
pub mod with;

mod context;
Expand Down
3 changes: 0 additions & 3 deletions crates/rspack_cacheable/src/utils/mod.rs

This file was deleted.

108 changes: 0 additions & 108 deletions crates/rspack_cacheable/src/utils/type_wrapper.rs

This file was deleted.

2 changes: 1 addition & 1 deletion crates/rspack_cacheable/src/with/as_preset/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod camino;
mod json;
mod lightningcss;
mod rspack_resolver;
mod rspack_source;
mod rspack_sources;
mod serde_json;
mod swc;
mod ustr;
Expand Down
103 changes: 0 additions & 103 deletions crates/rspack_cacheable/src/with/as_preset/rspack_source/mod.rs

This file was deleted.

86 changes: 86 additions & 0 deletions crates/rspack_cacheable/src/with/as_preset/rspack_sources/mod.rs
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())
}
}
1 change: 1 addition & 0 deletions crates/rspack_cacheable_test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ lightningcss = { workspace = true }
once_cell = { workspace = true }
rspack_cacheable = { path = "../rspack_cacheable" }
rspack_resolver = { workspace = true }
rspack_sources = { workspace = true }
rustc-hash = { workspace = true }
serde_json = { workspace = true }
swc_core = { workspace = true, features = ["ecma_ast"] }
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_cacheable_test/tests/with/as_preset/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod camino;
mod json;
mod lightningcss;
mod rspack_resolver;
mod rspack_sources;
mod serde_json;
mod swc;
mod ustr;
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()));
}

0 comments on commit b09e6b9

Please sign in to comment.