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

feat(es/codegen): Add an option to print assert for import attributes #7914

Merged
merged 22 commits into from
Sep 5, 2023
1 change: 1 addition & 0 deletions bindings/binding_core_node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ tracing-subscriber = { version = "0.3.9", features = ["env-filter"] }
swc_core = { version = "0.82.10", features = [
"allocator_node",
"ecma_ast",
"ecma_codegen",
"ecma_ast_serde",
"common_concurrent",
"bundler",
Expand Down
6 changes: 3 additions & 3 deletions bindings/binding_core_node/src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,16 @@ impl Task for BundleTask {
None,
None,
true,
codegen_target,
SourceMapsConfig::Bool(true),
// TODO
&Default::default(),
None,
minify,
None,
true,
false,
Default::default(),
swc_core::ecma::codegen::Config::default()
.with_target(codegen_target)
.with_minify(minify),
)?;

Ok((k, output))
Expand Down
12 changes: 6 additions & 6 deletions bindings/binding_core_node/src/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,18 @@ impl Task for PrintTask {
None,
options.output_path.clone(),
true,
options.config.jsc.target.unwrap_or(EsVersion::Es2020),
options
.source_maps
.clone()
.unwrap_or(SourceMapsConfig::Bool(false)),
&Default::default(),
None,
options.config.minify.into_bool(),
None,
options.config.emit_source_map_columns.into_bool(),
false,
Default::default(),
swc_core::ecma::codegen::Config::default()
.with_target(options.config.jsc.target.unwrap_or(EsVersion::Es2020))
.with_minify(options.config.minify.into_bool()),
)
.convert_err()
})
Expand Down Expand Up @@ -102,18 +102,18 @@ pub fn print_sync(program: String, options: Buffer) -> napi::Result<TransformOut
None,
options.output_path,
true,
codegen_target,
options
.source_maps
.clone()
.unwrap_or(SourceMapsConfig::Bool(false)),
&Default::default(),
None,
options.config.minify.into_bool(),
None,
options.config.emit_source_map_columns.into_bool(),
false,
Default::default(),
swc_core::ecma::codegen::Config::default()
.with_target(codegen_target)
.with_minify(options.config.minify.into_bool()),
)
.convert_err()
})
Expand Down
1 change: 1 addition & 0 deletions bindings/binding_core_wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ serde = { version = "1", features = ["derive"] }
serde-wasm-bindgen = "0.4.5"
swc_core = { version = "0.82.10", features = [
"ecma_ast_serde",
"ecma_codegen",
"binding_macro_wasm",
"ecma_transforms",
"ecma_visit",
Expand Down
6 changes: 3 additions & 3 deletions crates/binding_macros/src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,17 +248,17 @@ macro_rules! build_print_sync {
None,
None,
true,
opts.codegen_target().unwrap_or($crate::wasm::EsVersion::Es2020),
opts.source_maps
.clone()
.unwrap_or($crate::wasm::SourceMapsConfig::Bool(false)),
&Default::default(),
None,
opts.config.minify.into(),
None,
opts.config.emit_source_map_columns.into_bool(),
false,
Default::default(),
swc_core::ecma::codegen::Config::default()
.with_target(opts.codegen_target().unwrap_or($crate::wasm::EsVersion::Es2020))
.with_minify(opts.config.minify.into())
),"failed to print code")?;

serde_wasm_bindgen::to_value(&s)
Expand Down
5 changes: 1 addition & 4 deletions crates/dbg-swc/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,7 @@ pub fn print_js(cm: Arc<SourceMap>, m: &Module, minify: bool) -> Result<String>
}

let mut e = swc_ecma_codegen::Emitter {
cfg: swc_ecma_codegen::Config {
minify,
..Default::default()
},
cfg: swc_ecma_codegen::Config::default().with_minify(true),
cm,
comments: None,
wr,
Expand Down
4 changes: 1 addition & 3 deletions crates/swc/benches/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,13 @@ fn bench_codegen(b: &mut Bencher, _target: EsVersion) {
None,
None,
false,
EsVersion::Es2020,
SourceMapsConfig::Bool(false),
&Default::default(),
None,
false,
None,
false,
false,
Default::default(),
swc_ecma_codegen::Config::default().with_target(EsVersion::Es2020),
)
.unwrap()
}));
Expand Down
44 changes: 41 additions & 3 deletions crates/swc/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ impl Options {
comments.map(|v| v as _),
);

let keep_import_assertions = experimental.keep_import_assertions.into_bool();
let keep_import_attributes = experimental.keep_import_attributes.into_bool();

#[cfg(feature = "plugin")]
let plugin_transforms = {
Expand Down Expand Up @@ -801,7 +801,7 @@ impl Options {
),
// The transform strips import assertions, so it's only enabled if
// keep_import_assertions is false.
Optional::new(import_assertions(), !keep_import_assertions),
Optional::new(import_assertions(), !keep_import_attributes),
Optional::new(
typescript::strip_with_jsx::<Option<&dyn Comments>>(
cm.clone(),
Expand Down Expand Up @@ -873,6 +873,9 @@ impl Options {
preserve_comments,
emit_source_map_columns: cfg.emit_source_map_columns.into_bool(),
output: JscOutputConfig { charset, preamble },
emit_assert_for_import_attributes: experimental
.emit_assert_for_import_attributes
.into_bool(),
})
}
}
Expand Down Expand Up @@ -1354,6 +1357,7 @@ impl Config {
}

/// One `BuiltConfig` per a directory with swcrc
#[non_exhaustive]
pub struct BuiltInput<P: swc_ecma_visit::Fold> {
pub program: Program,
pub pass: P,
Expand All @@ -1377,6 +1381,37 @@ pub struct BuiltInput<P: swc_ecma_visit::Fold> {
pub emit_source_map_columns: bool,

pub output: JscOutputConfig,
pub emit_assert_for_import_attributes: bool,
}

impl<P> BuiltInput<P>
where
P: swc_ecma_visit::Fold,
{
pub fn with_pass<N>(self, map: impl FnOnce(P) -> N) -> BuiltInput<N>
where
N: swc_ecma_visit::Fold,
{
BuiltInput {
program: self.program,
pass: map(self.pass),
syntax: self.syntax,
target: self.target,
minify: self.minify,
external_helpers: self.external_helpers,
source_maps: self.source_maps,
input_source_map: self.input_source_map,
is_module: self.is_module,
output_path: self.output_path,
source_file_name: self.source_file_name,
preserve_comments: self.preserve_comments,
inline_sources_content: self.inline_sources_content,
comments: self.comments,
emit_source_map_columns: self.emit_source_map_columns,
output: self.output,
emit_assert_for_import_attributes: self.emit_assert_for_import_attributes,
}
}
}

/// `jsc` in `.swcrc`.
Expand Down Expand Up @@ -1460,7 +1495,10 @@ pub struct JscExperimental {
pub plugins: Option<Vec<PluginConfig>>,
/// If true, keeps import assertions in the output.
#[serde(default)]
pub keep_import_assertions: BoolConfig<false>,
pub keep_import_attributes: BoolConfig<false>,

#[serde(default)]
pub emit_assert_for_import_attributes: BoolConfig<false>,
/// Location where swc may stores its intermediate cache.
/// Currently this is only being used for wasm plugin's bytecache.
/// Path should be absolute directory, which will be created if not exist.
Expand Down
60 changes: 20 additions & 40 deletions crates/swc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,15 +501,13 @@ impl Compiler {
source_file_name: Option<&str>,
output_path: Option<PathBuf>,
inline_sources_content: bool,
target: EsVersion,
source_map: SourceMapsConfig,
source_map_names: &AHashMap<BytePos, JsWord>,
orig: Option<&sourcemap::SourceMap>,
minify: bool,
comments: Option<&dyn Comments>,
emit_source_map_columns: bool,
ascii_only: bool,
preamble: &str,
codegen_config: swc_ecma_codegen::Config,
) -> Result<TransformOutput, Error>
where
T: Node + VisitWith<IdentCollector>,
Expand All @@ -535,17 +533,12 @@ impl Compiler {
w.preamble(preamble).unwrap();
let mut wr = Box::new(w) as Box<dyn WriteJs>;

if minify {
if codegen_config.minify {
wr = Box::new(swc_ecma_codegen::text_writer::omit_trailing_semi(wr));
}

let mut emitter = Emitter {
cfg: swc_ecma_codegen::Config {
minify,
target,
ascii_only,
..Default::default()
},
cfg: codegen_config,
comments,
cm: self.cm.clone(),
wr,
Expand Down Expand Up @@ -970,26 +963,9 @@ impl Compiler {
}
};

let pass = chain!(config.pass, custom_after_pass(&config.program));

let config = BuiltInput {
program: config.program,
pass,
syntax: config.syntax,
target: config.target,
minify: config.minify,
external_helpers: config.external_helpers,
source_maps: config.source_maps,
input_source_map: config.input_source_map,
is_module: config.is_module,
output_path: config.output_path,
source_file_name: config.source_file_name,
preserve_comments: config.preserve_comments,
inline_sources_content: config.inline_sources_content,
comments: config.comments,
emit_source_map_columns: config.emit_source_map_columns,
output: config.output,
};
let after_pass = custom_after_pass(&config.program);

let config = config.with_pass(|pass| chain!(pass, after_pass));

let orig = if config.source_maps.enabled() {
self.get_orig_src_map(&fm, &config.input_source_map, false)?
Expand Down Expand Up @@ -1170,15 +1146,16 @@ impl Compiler {
Some(&fm.name.to_string()),
opts.output_path.clone().map(From::from),
opts.inline_sources_content,
target,
source_map,
&source_map_names,
orig.as_ref(),
true,
Some(&comments),
opts.emit_source_map_columns,
opts.format.ascii_only,
&opts.format.preamble,
swc_ecma_codegen::Config::default()
.with_target(target)
.with_minify(true)
.with_ascii_only(opts.format.ascii_only),
)
})
}
Expand Down Expand Up @@ -1245,19 +1222,22 @@ impl Compiler {
config.source_file_name.as_deref(),
config.output_path,
config.inline_sources_content,
config.target,
config.source_maps,
&source_map_names,
orig,
config.minify,
config.comments.as_ref().map(|v| v as _),
config.emit_source_map_columns,
config
.output
.charset
.map(|v| matches!(v, OutputCharset::Ascii))
.unwrap_or(false),
&config.output.preamble,
swc_ecma_codegen::Config::default()
.with_target(config.target)
.with_minify(config.minify)
.with_ascii_only(
config
.output
.charset
.map(|v| matches!(v, OutputCharset::Ascii))
.unwrap_or(false),
),
)
})
}
Expand Down
17 changes: 17 additions & 0 deletions crates/swc/tests/fixture/issues-7xxx/7908/input/1/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"$schema": "https://json.schemastore.org/swcrc",
"jsc": {
"parser": {
"syntax": "typescript"
},
"target": "es2022",
"experimental": {
"keepImportAttributes": true,
"emitAssertForImportAttributes": true
}
},
"module": {
"type": "es6"
},
"isModule": true
}
3 changes: 3 additions & 0 deletions crates/swc/tests/fixture/issues-7xxx/7908/input/1/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import packageJSON from "./package.json" assert { type: "json" };

console.log(packageJSON)
16 changes: 16 additions & 0 deletions crates/swc/tests/fixture/issues-7xxx/7908/input/2/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$schema": "https://json.schemastore.org/swcrc",
"jsc": {
"parser": {
"syntax": "typescript"
},
"target": "es2022",
"experimental": {
"keepImportAttributes": true
}
},
"module": {
"type": "es6"
},
"isModule": true
}
4 changes: 4 additions & 0 deletions crates/swc/tests/fixture/issues-7xxx/7908/input/2/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import packageJSON from "./package.json" assert { type: "json" };


console.log(packageJSON)
4 changes: 4 additions & 0 deletions crates/swc/tests/fixture/issues-7xxx/7908/output/1/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import packageJSON from "./package.json" with {
type: "json"
};
console.log(packageJSON);
4 changes: 4 additions & 0 deletions crates/swc/tests/fixture/issues-7xxx/7908/output/2/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import packageJSON from "./package.json" with {
type: "json"
};
console.log(packageJSON);
Loading