Skip to content

Commit b5fd823

Browse files
authored
feat(persistent cache): add snapshot module (#8424)
* feat: add snapshot * feat: add js api * fix: clippy * fix: ci * fix: js api remove type disable * refactor: update strategy lib version to package version
1 parent 6ae7121 commit b5fd823

File tree

39 files changed

+1303
-95
lines changed

39 files changed

+1303
-95
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/node_binding/binding.d.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,11 +1336,28 @@ export interface RawEvalDevToolModulePluginOptions {
13361336
sourceUrlComment?: string
13371337
}
13381338

1339+
export interface RawExperimentCacheOptionsCommon {
1340+
type: "disable"|"memory"
1341+
}
1342+
1343+
export interface RawExperimentCacheOptionsPersistent {
1344+
type: "persistent"
1345+
snapshot: RawExperimentSnapshotOptions
1346+
storage: Array<RawStorageOptions>
1347+
}
1348+
13391349
export interface RawExperiments {
13401350
layers: boolean
13411351
topLevelAwait: boolean
13421352
incremental?: RawIncremental
13431353
rspackFuture: RawRspackFuture
1354+
cache: RawExperimentCacheOptionsPersistent | RawExperimentCacheOptionsCommon
1355+
}
1356+
1357+
export interface RawExperimentSnapshotOptions {
1358+
immutablePaths: Array<string|RegExp>
1359+
unmanagedPaths: Array<string|RegExp>
1360+
managedPaths: Array<string|RegExp>
13441361
}
13451362

13461363
export interface RawExposeOptions {
@@ -1940,6 +1957,11 @@ export interface RawStatsOptions {
19401957
colors: boolean
19411958
}
19421959

1960+
export interface RawStorageOptions {
1961+
type: "filesystem"
1962+
directory: string
1963+
}
1964+
19431965
export interface RawSwcJsMinimizerOptions {
19441966
compress: any
19451967
mangle: any

crates/rspack_binding_options/src/options/mod.rs

Lines changed: 4 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -70,54 +70,10 @@ impl TryFrom<RawOptions> for CompilerOptions {
7070
let mode = value.mode.unwrap_or_default().into();
7171
let module: ModuleOptions = value.module.try_into()?;
7272
let cache = value.cache.into();
73-
let experiments = Experiments {
74-
incremental: match value.experiments.incremental {
75-
Some(value) => {
76-
let mut passes = IncrementalPasses::empty();
77-
if !matches!(cache, CacheOptions::Disabled) && value.make {
78-
passes.insert(IncrementalPasses::MAKE);
79-
}
80-
if value.infer_async_modules {
81-
passes.insert(IncrementalPasses::INFER_ASYNC_MODULES);
82-
}
83-
if value.provided_exports {
84-
passes.insert(IncrementalPasses::PROVIDED_EXPORTS);
85-
}
86-
if value.dependencies_diagnostics {
87-
passes.insert(IncrementalPasses::DEPENDENCIES_DIAGNOSTICS);
88-
}
89-
if value.build_chunk_graph {
90-
passes.insert(IncrementalPasses::BUILD_CHUNK_GRAPH);
91-
}
92-
if value.modules_hashes {
93-
passes.insert(IncrementalPasses::MODULES_HASHES);
94-
}
95-
if value.modules_codegen {
96-
passes.insert(IncrementalPasses::MODULES_CODEGEN);
97-
}
98-
if value.modules_runtime_requirements {
99-
passes.insert(IncrementalPasses::MODULES_RUNTIME_REQUIREMENTS);
100-
}
101-
if value.chunks_runtime_requirements {
102-
passes.insert(IncrementalPasses::CHUNKS_RUNTIME_REQUIREMENTS);
103-
}
104-
if value.chunks_hashes {
105-
passes.insert(IncrementalPasses::CHUNKS_HASHES);
106-
}
107-
if value.chunks_render {
108-
passes.insert(IncrementalPasses::CHUNKS_RENDER);
109-
}
110-
if value.emit_assets {
111-
passes.insert(IncrementalPasses::EMIT_ASSETS);
112-
}
113-
passes
114-
}
115-
None => IncrementalPasses::empty(),
116-
},
117-
layers: value.experiments.layers,
118-
top_level_await: value.experiments.top_level_await,
119-
rspack_future: value.experiments.rspack_future.into(),
120-
};
73+
let mut experiments: Experiments = value.experiments.into();
74+
if let CacheOptions::Disabled = cache {
75+
experiments.incremental = IncrementalPasses::empty();
76+
}
12177
let optimization = value.optimization.try_into()?;
12278
let stats = value.stats.into();
12379
let snapshot = value.snapshot.into();

crates/rspack_binding_options/src/options/raw_experiments.rs

Lines changed: 0 additions & 39 deletions
This file was deleted.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
mod raw_cache;
2+
mod raw_incremental;
3+
mod raw_rspack_future;
4+
5+
use napi_derive::napi;
6+
use raw_cache::{normalize_raw_experiment_cache_options, RawExperimentCacheOptions};
7+
use raw_incremental::RawIncremental;
8+
use raw_rspack_future::RawRspackFuture;
9+
use rspack_core::{incremental::IncrementalPasses, Experiments};
10+
11+
#[derive(Debug)]
12+
#[napi(object)]
13+
pub struct RawExperiments {
14+
pub layers: bool,
15+
pub top_level_await: bool,
16+
pub incremental: Option<RawIncremental>,
17+
pub rspack_future: RawRspackFuture,
18+
#[napi(ts_type = r#"RawExperimentCacheOptionsPersistent | RawExperimentCacheOptionsCommon"#)]
19+
pub cache: RawExperimentCacheOptions,
20+
}
21+
22+
impl From<RawExperiments> for Experiments {
23+
fn from(value: RawExperiments) -> Self {
24+
Self {
25+
incremental: match value.incremental {
26+
Some(value) => value.into(),
27+
None => IncrementalPasses::empty(),
28+
},
29+
layers: value.layers,
30+
top_level_await: value.top_level_await,
31+
rspack_future: value.rspack_future.into(),
32+
cache: normalize_raw_experiment_cache_options(value.cache),
33+
}
34+
}
35+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
mod raw_snapshot;
2+
mod raw_storage;
3+
4+
use napi::Either;
5+
use napi_derive::napi;
6+
use raw_snapshot::RawExperimentSnapshotOptions;
7+
use raw_storage::RawStorageOptions;
8+
use rspack_core::{cache::persistent::PersistentCacheOptions, ExperimentCacheOptions};
9+
10+
pub type RawExperimentCacheOptions =
11+
Either<RawExperimentCacheOptionsPersistent, RawExperimentCacheOptionsCommon>;
12+
13+
#[derive(Debug, Default)]
14+
#[napi(object)]
15+
pub struct RawExperimentCacheOptionsCommon {
16+
#[napi(ts_type = r#""disable"|"memory""#)]
17+
pub r#type: String,
18+
}
19+
20+
#[derive(Debug, Default)]
21+
#[napi(object)]
22+
pub struct RawExperimentCacheOptionsPersistent {
23+
#[napi(ts_type = r#""persistent""#)]
24+
pub r#type: String,
25+
// pub build_dependencies: Vec<String>,
26+
// pub version: String,
27+
pub snapshot: RawExperimentSnapshotOptions,
28+
pub storage: Vec<RawStorageOptions>,
29+
}
30+
31+
pub fn normalize_raw_experiment_cache_options(
32+
options: RawExperimentCacheOptions,
33+
) -> ExperimentCacheOptions {
34+
match options {
35+
Either::A(persistent_options) => ExperimentCacheOptions::Persistent(PersistentCacheOptions {
36+
snapshot: persistent_options.snapshot.into(),
37+
storage: persistent_options
38+
.storage
39+
.into_iter()
40+
.map(Into::into)
41+
.collect(),
42+
}),
43+
Either::B(options) => match options.r#type.as_str() {
44+
"disable" => ExperimentCacheOptions::Disabled,
45+
"memory" => ExperimentCacheOptions::Memory,
46+
_ => panic!("unsupported cache type"),
47+
},
48+
}
49+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use napi::Either;
2+
use napi_derive::napi;
3+
use rspack_core::cache::persistent::snapshot::{PathMatcher, SnapshotOptions};
4+
use rspack_regex::RspackRegex;
5+
6+
#[derive(Debug, Default)]
7+
#[napi(object)]
8+
pub struct RawExperimentSnapshotOptions {
9+
#[napi(ts_type = r#"Array<string|RegExp>"#)]
10+
pub immutable_paths: Vec<RawPathMatcher>,
11+
#[napi(ts_type = r#"Array<string|RegExp>"#)]
12+
pub unmanaged_paths: Vec<RawPathMatcher>,
13+
#[napi(ts_type = r#"Array<string|RegExp>"#)]
14+
pub managed_paths: Vec<RawPathMatcher>,
15+
}
16+
17+
type RawPathMatcher = Either<String, RspackRegex>;
18+
19+
impl From<RawExperimentSnapshotOptions> for SnapshotOptions {
20+
fn from(value: RawExperimentSnapshotOptions) -> Self {
21+
SnapshotOptions::new(
22+
value
23+
.immutable_paths
24+
.into_iter()
25+
.map(normalize_raw_path_matcher)
26+
.collect(),
27+
value
28+
.unmanaged_paths
29+
.into_iter()
30+
.map(normalize_raw_path_matcher)
31+
.collect(),
32+
value
33+
.managed_paths
34+
.into_iter()
35+
.map(normalize_raw_path_matcher)
36+
.collect(),
37+
)
38+
}
39+
}
40+
41+
fn normalize_raw_path_matcher(value: RawPathMatcher) -> PathMatcher {
42+
match value {
43+
Either::A(s) => PathMatcher::String(s),
44+
Either::B(reg) => PathMatcher::Regexp(reg),
45+
}
46+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use napi_derive::napi;
2+
use rspack_core::cache::persistent::storage::StorageOptions;
3+
4+
#[derive(Debug, Default)]
5+
#[napi(object)]
6+
pub struct RawStorageOptions {
7+
#[napi(ts_type = r#""filesystem""#)]
8+
pub r#type: String,
9+
pub directory: String,
10+
}
11+
12+
impl From<RawStorageOptions> for StorageOptions {
13+
fn from(_value: RawStorageOptions) -> Self {
14+
StorageOptions::FileSystem
15+
}
16+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use napi_derive::napi;
2+
use rspack_core::incremental::IncrementalPasses;
3+
4+
#[derive(Debug, Default)]
5+
#[napi(object)]
6+
pub struct RawIncremental {
7+
pub make: bool,
8+
pub infer_async_modules: bool,
9+
pub provided_exports: bool,
10+
pub dependencies_diagnostics: bool,
11+
pub build_chunk_graph: bool,
12+
pub modules_hashes: bool,
13+
pub modules_codegen: bool,
14+
pub modules_runtime_requirements: bool,
15+
pub chunks_runtime_requirements: bool,
16+
pub chunks_hashes: bool,
17+
pub chunks_render: bool,
18+
pub emit_assets: bool,
19+
}
20+
21+
impl From<RawIncremental> for IncrementalPasses {
22+
fn from(value: RawIncremental) -> Self {
23+
let mut passes = IncrementalPasses::empty();
24+
if value.make {
25+
passes.insert(IncrementalPasses::MAKE);
26+
}
27+
if value.infer_async_modules {
28+
passes.insert(IncrementalPasses::INFER_ASYNC_MODULES);
29+
}
30+
if value.provided_exports {
31+
passes.insert(IncrementalPasses::PROVIDED_EXPORTS);
32+
}
33+
if value.dependencies_diagnostics {
34+
passes.insert(IncrementalPasses::DEPENDENCIES_DIAGNOSTICS);
35+
}
36+
if value.build_chunk_graph {
37+
passes.insert(IncrementalPasses::BUILD_CHUNK_GRAPH);
38+
}
39+
if value.modules_hashes {
40+
passes.insert(IncrementalPasses::MODULES_HASHES);
41+
}
42+
if value.modules_codegen {
43+
passes.insert(IncrementalPasses::MODULES_CODEGEN);
44+
}
45+
if value.modules_runtime_requirements {
46+
passes.insert(IncrementalPasses::MODULES_RUNTIME_REQUIREMENTS);
47+
}
48+
if value.chunks_runtime_requirements {
49+
passes.insert(IncrementalPasses::CHUNKS_RUNTIME_REQUIREMENTS);
50+
}
51+
if value.chunks_hashes {
52+
passes.insert(IncrementalPasses::CHUNKS_HASHES);
53+
}
54+
if value.chunks_render {
55+
passes.insert(IncrementalPasses::CHUNKS_RENDER);
56+
}
57+
if value.emit_assets {
58+
passes.insert(IncrementalPasses::EMIT_ASSETS);
59+
}
60+
passes
61+
}
62+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use napi_derive::napi;
2+
use rspack_core::RspackFuture;
3+
4+
#[allow(clippy::empty_structs_with_brackets)]
5+
#[derive(Debug, Default)]
6+
#[napi(object)]
7+
pub struct RawRspackFuture {}
8+
9+
impl From<RawRspackFuture> for RspackFuture {
10+
fn from(_value: RawRspackFuture) -> Self {
11+
Self {}
12+
}
13+
}

0 commit comments

Comments
 (0)