Skip to content

Commit

Permalink
feat(rust): only derive deserialize on BundlerOptions in testing (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
oddguan committed Apr 17, 2024
1 parent b5b06bf commit 06df20d
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 15 deletions.
3 changes: 3 additions & 0 deletions crates/rolldown_common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ schemars = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
sugar_path = { workspace = true }

[features]
deserialize_bundler_options = []
33 changes: 25 additions & 8 deletions crates/rolldown_common/src/inner_bundler_options/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::{fmt::Debug, path::PathBuf};

#[cfg(feature = "deserialize_bundler_options")]
use schemars::JsonSchema;
#[cfg(feature = "deserialize_bundler_options")]
use serde::{Deserialize, Deserializer};

use self::types::{
Expand All @@ -11,14 +13,21 @@ use self::types::{

pub mod types;

#[derive(Default, Debug, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[derive(Default, Debug)]
#[cfg_attr(
feature = "deserialize_bundler_options",
derive(Deserialize, JsonSchema),
serde(rename_all = "camelCase", deny_unknown_fields)
)]
pub struct BundlerOptions {
// --- options for input
pub input: Option<Vec<InputItem>>,
pub cwd: Option<PathBuf>,
#[serde(default, deserialize_with = "deserialize_external")]
#[schemars(with = "Option<Vec<String>>")]
#[cfg_attr(
feature = "deserialize_bundler_options",
serde(default, deserialize_with = "deserialize_external"),
schemars(with = "Option<Vec<String>>")
)]
pub external: Option<External>,
pub treeshake: Option<bool>,
pub platform: Option<Platform>,
Expand All @@ -29,16 +38,23 @@ pub struct BundlerOptions {
pub dir: Option<String>,
pub format: Option<OutputFormat>,
pub sourcemap: Option<SourceMapType>,
#[serde(default, deserialize_with = "deserialize_addon")]
#[schemars(with = "Option<String>")]
#[cfg_attr(
feature = "deserialize_bundler_options",
serde(default, deserialize_with = "deserialize_addon"),
schemars(with = "Option<String>")
)]
pub banner: Option<AddonOutputOption>,
#[serde(default, deserialize_with = "deserialize_addon")]
#[schemars(with = "Option<String>")]
#[cfg_attr(
feature = "deserialize_bundler_options",
serde(default, deserialize_with = "deserialize_addon"),
schemars(with = "Option<String>")
)]
pub footer: Option<AddonOutputOption>,
// --- options for resolve
pub resolve: Option<ResolveOptions>,
}

#[cfg(feature = "deserialize_bundler_options")]
fn deserialize_external<'de, D>(deserializer: D) -> Result<Option<External>, D::Error>
where
D: Deserializer<'de>,
Expand All @@ -47,6 +63,7 @@ where
Ok(deserialized.map(External::ArrayString))
}

#[cfg(feature = "deserialize_bundler_options")]
fn deserialize_addon<'de, D>(deserializer: D) -> Result<Option<AddonOutputOption>, D::Error>
where
D: Deserializer<'de>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#[cfg(feature = "deserialize_bundler_options")]
use schemars::JsonSchema;
#[cfg(feature = "deserialize_bundler_options")]
use serde::Deserialize;

#[derive(Debug, Deserialize, JsonSchema)]
#[derive(Debug)]
#[cfg_attr(feature = "deserialize_bundler_options", derive(Deserialize, JsonSchema))]
pub struct InputItem {
pub name: Option<String>,
pub import: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#[cfg(feature = "deserialize_bundler_options")]
use schemars::JsonSchema;
#[cfg(feature = "deserialize_bundler_options")]
use serde::Deserialize;

#[derive(Debug, Deserialize, JsonSchema)]
#[derive(Debug)]
#[cfg_attr(feature = "deserialize_bundler_options", derive(Deserialize, JsonSchema))]
pub enum OutputFormat {
Esm,
Cjs,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#[cfg(feature = "deserialize_bundler_options")]
use schemars::JsonSchema;
#[cfg(feature = "deserialize_bundler_options")]
use serde::Deserialize;

#[derive(Debug, Clone, Copy, Deserialize, JsonSchema)]
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "deserialize_bundler_options", derive(Deserialize, JsonSchema))]
pub enum Platform {
/// Represents the Node.js platform.
Node,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ use serde::Deserialize;

/// A simple wrapper around `oxc_resolver::ResolveOptions` to make it easier to use in the `rolldown_resolver` crate.
/// See [oxc_resolver::ResolveOptions](https://docs.rs/oxc_resolver/latest/oxc_resolver/struct.ResolveOptions.html) for more information.
#[derive(Debug, Default, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[derive(Debug, Default)]
#[cfg_attr(
feature = "deserialize_bundler_options",
derive(Deserialize, JsonSchema),
serde(rename_all = "camelCase", deny_unknown_fields)
)]
pub struct ResolveOptions {
pub alias: Option<Vec<(String, Vec<String>)>>,
pub alias_fields: Option<Vec<Vec<String>>>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use schemars::JsonSchema;
use serde::Deserialize;

#[derive(Debug, Deserialize, JsonSchema)]
#[derive(Debug)]
#[cfg_attr(feature = "deserialize_bundler_options", derive(Deserialize, JsonSchema))]
pub enum SourceMapType {
File,
Inline,
Expand Down
2 changes: 1 addition & 1 deletion crates/rolldown_testing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ path = "./bin/gen_test_config_schema.rs"
test = false

[dependencies]
rolldown_common = { workspace = true }
rolldown_common = { workspace = true, features = ["deserialize_bundler_options"] }
schemars = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }

0 comments on commit 06df20d

Please sign in to comment.