Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow empty persistent cache options #8813

Merged
merged 8 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1367,24 +1367,19 @@ export interface RawEvalDevToolModulePluginOptions {
sourceUrlComment?: string
}

export interface RawExperimentCacheOptionsMemory {
type: "memory" | "disable"
}

export interface RawExperimentCacheOptionsPersistent {
type: "persistent"
buildDependencies: Array<string>
version: string
snapshot: RawExperimentSnapshotOptions
storage: RawStorageOptions
buildDependencies?: Array<string>
version?: string
snapshot?: RawExperimentSnapshotOptions
storage?: RawStorageOptions
}

export interface RawExperiments {
layers: boolean
topLevelAwait: boolean
incremental?: false | { [key: string]: boolean }
rspackFuture?: RawRspackFuture
cache: RawExperimentCacheOptionsPersistent | RawExperimentCacheOptionsMemory | boolean
cache: boolean | { type: "persistent" } & RawExperimentCacheOptionsPersistent | { type: "memory" }
}

export interface RawExperimentSnapshotOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct RawExperiments {
pub incremental: Option<WithFalse<RawIncremental>>,
pub rspack_future: Option<RawRspackFuture>,
#[napi(
ts_type = r#"RawExperimentCacheOptionsPersistent | RawExperimentCacheOptionsMemory | boolean"#
ts_type = r#"boolean | { type: "persistent" } & RawExperimentCacheOptionsPersistent | { type: "memory" }"#
)]
pub cache: RawExperimentCacheOptions,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,96 @@ mod raw_storage;

use core::panic;

use napi::bindgen_prelude::Either3;
use napi::{
bindgen_prelude::{FromNapiValue, Object, TypeName, ValidateNapiValue},
Either,
};
use napi_derive::napi;
use raw_snapshot::RawExperimentSnapshotOptions;
use raw_storage::RawStorageOptions;
use rspack_core::{cache::persistent::PersistentCacheOptions, ExperimentCacheOptions};

pub type RawExperimentCacheOptions =
Either3<bool, RawExperimentCacheOptionsMemory, RawExperimentCacheOptionsPersistent>;
pub type RawExperimentCacheOptions = Either<bool, RawExperimentCache>;

#[derive(Debug, Default)]
#[napi(object)]
pub struct RawExperimentCacheOptionsPersistent {
#[napi(ts_type = r#""persistent""#)]
pub r#type: String,
pub build_dependencies: Vec<String>,
pub version: String,
pub snapshot: RawExperimentSnapshotOptions,
pub storage: RawStorageOptions,
pub build_dependencies: Option<Vec<String>>,
pub version: Option<String>,
pub snapshot: Option<RawExperimentSnapshotOptions>,
pub storage: Option<RawStorageOptions>,
}

impl From<RawExperimentCacheOptionsPersistent> for PersistentCacheOptions {
fn from(value: RawExperimentCacheOptionsPersistent) -> Self {
Self {
build_dependencies: value
.build_dependencies
.unwrap_or_default()
.into_iter()
.map(Into::into)
.collect(),
version: value.version.unwrap_or_default(),
snapshot: value.snapshot.unwrap_or_default().into(),
storage: value.storage.unwrap_or_default().into(),
}
}
}

#[derive(Debug, Default)]
#[napi(object)]
pub struct RawExperimentCacheOptionsMemory {
#[napi(ts_type = r#""memory" | "disable""#)]
pub r#type: String,
pub enum RawExperimentCache {
#[default]
Memory,
Persistent(RawExperimentCacheOptionsPersistent),
}

impl TypeName for RawExperimentCache {
fn type_name() -> &'static str {
"RawExperimentCache"
}

fn value_type() -> napi::ValueType {
napi::ValueType::Object
}
}

impl ValidateNapiValue for RawExperimentCache {}

impl FromNapiValue for RawExperimentCache {
unsafe fn from_napi_value(
env: napi::sys::napi_env,
napi_val: napi::sys::napi_value,
) -> napi::Result<Self> {
let o = Object::from_napi_value(env, napi_val)?;
let t = o.get_named_property::<String>("type")?;

let v = match &*t {
"persistent" => {
let o = RawExperimentCacheOptionsPersistent::from_napi_value(env, napi_val)?;
Self::Persistent(o)
}
"memory" => Self::Memory,
_ => panic!("Unexpected cache type: {t}, expected 'persistent' or 'memory'"),
};

Ok(v)
}
}

pub fn normalize_raw_experiment_cache_options(
options: RawExperimentCacheOptions,
) -> ExperimentCacheOptions {
match options {
Either3::C(persistent_options) if persistent_options.r#type == "persistent" => {
ExperimentCacheOptions::Persistent(PersistentCacheOptions {
build_dependencies: persistent_options
.build_dependencies
.into_iter()
.map(Into::into)
.collect(),
version: persistent_options.version,
snapshot: persistent_options.snapshot.into(),
storage: persistent_options.storage.into(),
})
}
Either3::B(options) if options.r#type == "memory" => ExperimentCacheOptions::Memory,
Either3::B(options) if options.r#type == "disable" => ExperimentCacheOptions::Disabled,
Either3::A(options) => {
Either::A(options) => {
if options {
ExperimentCacheOptions::Memory
} else {
ExperimentCacheOptions::Disabled
}
}
_ => panic!("Invalid cache options"),
Either::B(options) => match options {
RawExperimentCache::Persistent(options) => ExperimentCacheOptions::Persistent(options.into()),
RawExperimentCache::Memory => ExperimentCacheOptions::Memory,
},
}
}
Loading