Skip to content

Commit

Permalink
fix(es/minifier): Fix usage via yarn resolution (#2158)
Browse files Browse the repository at this point in the history
swc_common:
 - Deprecate some unused types.

swc:
 - `BoolOrObject`: Improve the error message for deserialization failure.
 - `BoolOrObject`: Treat an empty object as `true`.
 - `JsMinifyFormatOptions`: Add fields. (#2153)

node_swc:
 - Improve error message for deserialization failure.
  • Loading branch information
kdy1 authored Aug 26, 2021
1 parent 8c5daee commit e468752
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ edition = "2018"
license = "Apache-2.0/MIT"
name = "swc"
repository = "https://github.com/swc-project/swc.git"
version = "0.43.1"
version = "0.44.0"

[lib]
name = "swc"
Expand Down
4 changes: 4 additions & 0 deletions common/src/serializer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#![allow(deprecated)]
#![deprecated = "Not used by swc, and this will be removed with next breaking change"]
use serde::Deserialize;

#[derive(Deserialize)]
#[deprecated = "Not used by swc, and this will be removed with next breaking change"]
pub struct Node<T> {
#[serde(default, rename = "type")]
pub ty: String,
Expand All @@ -9,6 +12,7 @@ pub struct Node<T> {
}

#[derive(Deserialize)]
#[deprecated = "Not used by swc, and this will be removed with next breaking change"]
pub struct Type {
#[serde(rename = "type")]
pub ty: String,
Expand Down
10 changes: 9 additions & 1 deletion node/binding/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anyhow::Context;
use napi::{CallContext, JsBuffer, Status};
use serde::de::DeserializeOwned;
use std::any::type_name;

pub trait MapErr<T>: Into<Result<T, anyhow::Error>> {
fn convert_err(self) -> napi::Result<T> {
Expand All @@ -25,7 +26,14 @@ impl CtxtExt for CallContext<'_> {
{
let buffer = self.get::<JsBuffer>(index)?.into_value()?;
let v = serde_json::from_slice(&buffer)
.with_context(|| format!("Argument at `{}` is not JsBuffer", index))
.with_context(|| {
format!(
"Failed to deserialize argument at `{}` as {}\nJSON: {}",
index,
type_name::<T>(),
String::from_utf8_lossy(&buffer)
)
})
.convert_err()?;

Ok(v)
Expand Down
7 changes: 7 additions & 0 deletions scripts/link.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash
set -eu

yarn run build:dev
yarn link

(cd swr && yarn run build)
81 changes: 81 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,88 @@ pub struct JsMinifyOptions {
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct JsMinifyFormatOptions {
/// Not implemented yet.
#[serde(default, alias = "ascii_only")]
pub ascii_only: bool,

/// Not implemented yet.
#[serde(default)]
pub beautify: bool,

/// Not implemented yet.
#[serde(default)]
pub braces: bool,

#[serde(default)]
pub comments: BoolOrObject<JsMinifyCommentOption>,

/// Not implemented yet.
#[serde(default)]
pub ecma: usize,

/// Not implemented yet.
#[serde(default, alias = "indent_level")]
pub indent_level: usize,

/// Not implemented yet.
#[serde(default, alias = "indent_start")]
pub indent_start: bool,

/// Not implemented yet.
#[serde(default, alias = "inline_script")]
pub inline_script: bool,

/// Not implemented yet.
#[serde(default, alias = "keep_numbers")]
pub keep_numbers: bool,

/// Not implemented yet.
#[serde(default, alias = "keep_quoted_props")]
pub keep_quoted_props: bool,

/// Not implemented yet.
#[serde(default, alias = "max_line_len")]
pub max_line_len: BoolOrObject<usize>,

/// Not implemented yet.
#[serde(default)]
pub preamble: String,

/// Not implemented yet.
#[serde(default, alias = "quote_keys")]
pub quote_keys: bool,

/// Not implemented yet.
#[serde(default, alias = "quote_style")]
pub quote_style: usize,

/// Not implemented yet.
#[serde(default, alias = "preserve_annotations")]
pub preserve_annotations: bool,

/// Not implemented yet.
#[serde(default)]
pub safari10: bool,

/// Not implemented yet.
#[serde(default)]
pub semicolons: bool,

/// Not implemented yet.
#[serde(default)]
pub shebang: bool,

/// Not implemented yet.
#[serde(default)]
pub webkit: bool,

/// Not implemented yet.
#[serde(default, alias = "warp_iife")]
pub wrap_iife: bool,

/// Not implemented yet.
#[serde(default, alias = "wrap_func_args")]
pub wrap_func_args: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down
48 changes: 45 additions & 3 deletions src/config/util.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use serde::{Deserialize, Serialize};

use super::Merge;
use serde::{Deserialize, Serialize};

#[derive(Clone, Serialize, Deserialize, Debug)]
/// Note: `{}` (empty object) is treated as `true`.
#[derive(Clone, Serialize, Debug)]
#[serde(untagged)]
pub enum BoolOrObject<T> {
Bool(bool),
Expand Down Expand Up @@ -57,3 +57,45 @@ where
}
}
}

impl<'de, T> Deserialize<'de> for BoolOrObject<T>
where
T: Deserialize<'de>,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
#[derive(Deserialize)]
#[serde(untagged)]
enum Deser<T> {
Bool(bool),
Obj(T),
EmptyObject(EmptyStruct),
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct EmptyStruct {}

let content = swc_common::private::serde::de::Content::deserialize(deserializer)?;

let deserializer =
swc_common::private::serde::de::ContentRefDeserializer::<D::Error>::new(&content);

let res = Deser::deserialize(deserializer);

match res {
Ok(v) => Ok(match v {
Deser::Bool(v) => BoolOrObject::Bool(v),
Deser::Obj(v) => BoolOrObject::Obj(v),
Deser::EmptyObject(_) => BoolOrObject::Bool(true),
}),
Err(..) => {
let d =
swc_common::private::serde::de::ContentDeserializer::<D::Error>::new(content);
Ok(BoolOrObject::Obj(T::deserialize(d)?))
}
}
}
}

0 comments on commit e468752

Please sign in to comment.