fix(es/config): Add jsc.output.charset (#4708)

This commit is contained in:
Donny/강동윤 2022-05-19 11:03:46 +09:00 committed by GitHub
parent 494343456d
commit bf5ba28471
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 54 additions and 7 deletions

View File

@ -633,6 +633,7 @@ impl Options {
comments: comments.cloned(),
preserve_comments,
emit_source_map_columns: cfg.emit_source_map_columns.into_bool(),
output: cfg.jsc.output,
})
}
}
@ -1099,6 +1100,8 @@ pub struct BuiltInput<P: swc_ecma_visit::Fold> {
pub inline_sources_content: bool,
pub emit_source_map_columns: bool,
pub output: JscOutputConfig,
}
/// `jsc` in `.swcrc`.
@ -1143,6 +1146,31 @@ pub struct JscConfig {
#[serde(default)]
pub preserve_all_comments: BoolConfig<false>,
#[serde(default)]
pub output: JscOutputConfig,
}
#[derive(Debug, Default, Clone, Serialize, Deserialize, Merge)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct JscOutputConfig {
#[serde(default)]
pub charset: Option<OutputCharset>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub enum OutputCharset {
#[serde(rename = "utf8")]
Utf8,
#[serde(rename = "ascii")]
Ascii,
}
impl Default for OutputCharset {
fn default() -> Self {
OutputCharset::Utf8
}
}
/// `jsc.experimental` in `.swcrc`

View File

@ -121,7 +121,7 @@ use std::{
use anyhow::{bail, Context, Error};
use atoms::JsWord;
use common::{collections::AHashMap, comments::SingleThreadedComments, errors::HANDLER};
use config::{IsModule, JsMinifyCommentOption, JsMinifyOptions};
use config::{IsModule, JsMinifyCommentOption, JsMinifyOptions, OutputCharset};
use json_comments::StripComments;
use once_cell::sync::Lazy;
use serde::Serialize;
@ -884,6 +884,7 @@ impl Compiler {
inline_sources_content: config.inline_sources_content,
comments: config.comments,
emit_source_map_columns: config.emit_source_map_columns,
output: config.output,
};
let orig = if config.source_maps.enabled() {
@ -1129,7 +1130,11 @@ impl Compiler {
config.minify,
config.comments.as_ref().map(|v| v as _),
config.emit_source_map_columns,
false,
config
.output
.charset
.map(|v| matches!(v, OutputCharset::Ascii))
.unwrap_or(false),
)
})
}

View File

@ -0,0 +1,8 @@
{
"jsc": {
"target": "es2021",
"output": {
"charset": "ascii"
}
}
}

View File

@ -0,0 +1 @@
const foo = "❤️";

View File

@ -0,0 +1 @@
const foo = "\u2764\uFE0F";

View File

@ -0,0 +1,5 @@
{
"jsc": {
"target": "es2021"
}
}

View File

@ -0,0 +1 @@
const foo = "❤️";

View File

@ -0,0 +1 @@
const foo = "❤️";

View File

@ -755,6 +755,7 @@ fn should_visit() {
inline_sources_content: config.inline_sources_content,
comments: config.comments,
emit_source_map_columns: config.emit_source_map_columns,
output: config.output,
};
if config.minify {

View File

@ -143,13 +143,9 @@ fn shopify_2_same_opt() {
target: Some(EsVersion::Es5),
loose: false.into(),
keep_class_names: false.into(),
base_url: Default::default(),
paths: Default::default(),
minify: None,
experimental: Default::default(),
lints: Default::default(),
assumptions: Default::default(),
preserve_all_comments: false.into(),
..Default::default()
},
module: None,
minify: false.into(),