fix(es/minifier): Fix usage via yarn resolution (#2158)

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.
This commit is contained in:
강동윤 2021-08-26 15:18:59 +09:00 committed by GitHub
parent 8c5daeec2a
commit e468752ebc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 148 additions and 6 deletions

2
Cargo.lock generated
View File

@ -2287,7 +2287,7 @@ checksum = "6446ced80d6c486436db5c078dde11a9f73d42b57fb273121e160b84f63d894c"
[[package]]
name = "swc"
version = "0.43.1"
version = "0.44.0"
dependencies = [
"ahash",
"anyhow",

View File

@ -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"

View File

@ -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,
@ -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,

View File

@ -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> {
@ -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)

7
scripts/link.sh Executable file
View File

@ -0,0 +1,7 @@
#!/usr/bin/env bash
set -eu
yarn run build:dev
yarn link
(cd swr && yarn run build)

View File

@ -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)]

View File

@ -1,8 +1,8 @@
use super::Merge;
use serde::{Deserialize, Serialize};
use super::Merge;
#[derive(Clone, Serialize, Deserialize, Debug)]
/// Note: `{}` (empty object) is treated as `true`.
#[derive(Clone, Serialize, Debug)]
#[serde(untagged)]
pub enum BoolOrObject<T> {
Bool(bool),
@ -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)?))
}
}
}
}