Skip to content

Commit

Permalink
fix(es/codegen): Set sourceRoot of sourcemaps (#6086)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #2362
  • Loading branch information
kdy1 authored Jan 24, 2024
1 parent 6429c8d commit ae78669
Show file tree
Hide file tree
Showing 12 changed files with 247 additions and 231 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

24 changes: 10 additions & 14 deletions bindings/binding_core_node/src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use swc_core::{
base::{
config::SourceMapsConfig,
resolver::{environment_resolver, paths_resolver},
Compiler, TransformOutput,
Compiler, PrintArgs, TransformOutput,
},
bundler::{BundleKind, Bundler, Load, ModuleRecord, Resolve},
common::{collections::AHashMap, Globals, Span, GLOBALS},
Expand Down Expand Up @@ -126,19 +126,15 @@ impl Task for BundleTask {

let output = self.swc.print(
&m,
None,
None,
true,
SourceMapsConfig::Bool(true),
// TODO
&Default::default(),
None,
None,
true,
Default::default(),
swc_core::ecma::codegen::Config::default()
.with_target(codegen_target)
.with_minify(minify),
PrintArgs {
inline_sources_content: true,
source_map: SourceMapsConfig::Bool(true),
emit_source_map_columns: true,
codegen_config: swc_core::ecma::codegen::Config::default()
.with_target(codegen_target)
.with_minify(minify),
..Default::default()
},
)?;

Ok((k, output))
Expand Down
58 changes: 27 additions & 31 deletions bindings/binding_core_node/src/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use napi::{
use swc_core::{
base::{
config::{Options, SourceMapsConfig},
Compiler, TransformOutput,
Compiler, PrintArgs, TransformOutput,
},
common::GLOBALS,
ecma::ast::{EsVersion, Program},
Expand Down Expand Up @@ -37,21 +37,19 @@ impl Task for PrintTask {
self.c
.print(
&program,
None,
options.output_path.clone(),
true,
options
.source_maps
.clone()
.unwrap_or(SourceMapsConfig::Bool(false)),
&Default::default(),
None,
None,
options.config.emit_source_map_columns.into_bool(),
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()),
PrintArgs {
output_path: options.output_path.clone(),
inline_sources_content: true,
source_map: options
.source_maps
.clone()
.unwrap_or(SourceMapsConfig::Bool(false)),
emit_source_map_columns: options.config.emit_source_map_columns.into_bool(),
codegen_config: swc_core::ecma::codegen::Config::default()
.with_target(options.config.jsc.target.unwrap_or(EsVersion::Es2020))
.with_minify(options.config.minify.into_bool()),
..Default::default()
},
)
.convert_err()
})
Expand Down Expand Up @@ -99,21 +97,19 @@ pub fn print_sync(program: String, options: Buffer) -> napi::Result<TransformOut
GLOBALS.set(&Default::default(), || {
c.print(
&program,
None,
options.output_path,
true,
options
.source_maps
.clone()
.unwrap_or(SourceMapsConfig::Bool(false)),
&Default::default(),
None,
None,
options.config.emit_source_map_columns.into_bool(),
Default::default(),
swc_core::ecma::codegen::Config::default()
.with_target(codegen_target)
.with_minify(options.config.minify.into_bool()),
PrintArgs {
output_path: options.output_path,
inline_sources_content: true,
source_map: options
.source_maps
.clone()
.unwrap_or(SourceMapsConfig::Bool(false)),
emit_source_map_columns: options.config.emit_source_map_columns.into_bool(),
codegen_config: swc_core::ecma::codegen::Config::default()
.with_target(codegen_target)
.with_minify(options.config.minify.into_bool()),
..Default::default()
},
)
.convert_err()
})
Expand Down
51 changes: 25 additions & 26 deletions bindings/binding_minifier_node/src/minify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use napi::{
};
use serde::Deserialize;
use swc_compiler_base::{
minify_file_comments, parse_js, IdentCollector, IsModule, SourceMapsConfig, TransformOutput,
minify_file_comments, parse_js, IdentCollector, IsModule, PrintArgs, SourceMapsConfig,
TransformOutput,
};
use swc_config::config_types::BoolOr;
use swc_core::{
Expand Down Expand Up @@ -76,15 +77,10 @@ fn do_work(input: MinifyTarget, options: JsMinifyOptions) -> napi::Result<Transf
.source_map
.as_ref()
.map(|obj| -> Result<_, Error> {
let orig = obj
.content
.as_ref()
.map(|s| sourcemap::SourceMap::from_slice(s.as_bytes()));
let orig = match orig {
Some(v) => Some(v?),
None => None,
};
Ok((SourceMapsConfig::Bool(true), orig))
Ok((
SourceMapsConfig::Bool(true),
obj.content.as_ref().map(|s| s.to_sourcemap()).transpose()?,
))
})
.unwrap_as_option(|v| {
Some(Ok(match v {
Expand Down Expand Up @@ -209,22 +205,25 @@ fn do_work(input: MinifyTarget, options: JsMinifyOptions) -> napi::Result<Transf
swc_compiler_base::print(
cm.clone(),
&module,
Some(&fm.name.to_string()),
options.output_path.clone().map(From::from),
options.inline_sources_content,
source_map,
&source_map_names,
orig.as_ref(),
Some(&comments),
options.emit_source_map_columns,
&options.format.preamble,
swc_core::ecma::codegen::Config::default()
.with_target(target)
.with_minify(true)
.with_ascii_only(options.format.ascii_only)
.with_emit_assert_for_import_attributes(
options.format.emit_assert_for_import_attributes,
),
PrintArgs {
source_file_name: Some(&fm.name.to_string()),
output_path: options.output_path.clone().map(From::from),
inline_sources_content: options.inline_sources_content,
source_map,
source_map_names: &source_map_names,
orig: orig.as_ref(),
comments: Some(&comments),
emit_source_map_columns: options.emit_source_map_columns,
preamble: &options.format.preamble,
codegen_config: swc_core::ecma::codegen::Config::default()
.with_target(target)
.with_minify(true)
.with_ascii_only(options.format.ascii_only)
.with_emit_assert_for_import_attributes(
options.format.emit_assert_for_import_attributes,
),
..Default::default()
},
)
})
.convert_err()
Expand Down
30 changes: 16 additions & 14 deletions crates/binding_macros/src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use once_cell::sync::Lazy;
#[doc(hidden)]
pub use serde_wasm_bindgen;
use serde_wasm_bindgen::Serializer;
#[doc(hidden)]
pub use swc::PrintArgs;
use swc::{config::ErrorFormat, Compiler, HandlerOpts};
#[doc(hidden)]
pub use swc::{
Expand Down Expand Up @@ -149,6 +151,7 @@ macro_rules! build_parse_sync {
pub fn parse_sync(s: $crate::wasm::js_sys::JsString, opts: $crate::wasm::JsValue) -> Result<$crate::wasm::JsValue, $crate::wasm::JsValue> {
use serde::Serialize;
use $crate::wasm::VisitMutWith;
use $crate::wasm::PrintArgs;

let c = $crate::wasm::compiler();

Expand Down Expand Up @@ -226,6 +229,8 @@ macro_rules! build_print_sync {
($(#[$m:meta])*, $opt: expr) => {
$(#[$m])*
pub fn print_sync(s: $crate::wasm::JsValue, opts: $crate::wasm::JsValue) -> Result<$crate::wasm::JsValue, $crate::wasm::JsValue> {
use $crate::wasm::PrintArgs;

let c = $crate::wasm::compiler();

$crate::wasm::try_with_handler_globals(
Expand All @@ -245,20 +250,17 @@ macro_rules! build_print_sync {
let s = $crate::wasm::anyhow::Context::context(c
.print(
&program,
None,
None,
true,
opts.source_maps
.clone()
.unwrap_or($crate::wasm::SourceMapsConfig::Bool(false)),
&Default::default(),
None,
None,
opts.config.emit_source_map_columns.into_bool(),
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())
PrintArgs {
inline_sources_content: true,
source_map: opts.source_maps
.clone()
.unwrap_or($crate::wasm::SourceMapsConfig::Bool(false)),
emit_source_map_columns: opts.config.emit_source_map_columns.into_bool(),
codegen_config: swc_core::ecma::codegen::Config::default()
.with_target(opts.codegen_target().unwrap_or($crate::wasm::EsVersion::Es2020))
.with_minify(opts.config.minify.into()),
..Default::default()
},
),"failed to print code")?;

serde_wasm_bindgen::to_value(&s)
Expand Down
18 changes: 7 additions & 11 deletions crates/swc/benches/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ use std::{
};

use criterion::{black_box, criterion_group, criterion_main, Bencher, Criterion};
use swc::config::{Config, IsModule, JscConfig, Options, SourceMapsConfig};
use swc::config::{Config, IsModule, JscConfig, Options};
use swc_common::{
errors::Handler, FileName, FilePathMapping, Mark, SourceFile, SourceMap, GLOBALS,
};
use swc_compiler_base::PrintArgs;
use swc_ecma_ast::{EsVersion, Program};
use swc_ecma_parser::Syntax;
use swc_ecma_transforms::{fixer, hygiene, resolver, typescript};
Expand Down Expand Up @@ -112,16 +113,11 @@ fn bench_codegen(b: &mut Bencher, _target: EsVersion) {
black_box(GLOBALS.set(&Default::default(), || {
c.print(
&module,
None,
None,
false,
SourceMapsConfig::Bool(false),
&Default::default(),
None,
None,
false,
Default::default(),
swc_ecma_codegen::Config::default().with_target(EsVersion::Es2020),
PrintArgs {
codegen_config: swc_ecma_codegen::Config::default()
.with_target(EsVersion::Es2020),
..Default::default()
},
)
.unwrap()
}));
Expand Down
4 changes: 4 additions & 0 deletions crates/swc/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ impl Options {
base: &FileName,
parse: impl FnOnce(Syntax, EsVersion, IsModule) -> Result<Program, Error>,
output_path: Option<&Path>,
source_root: Option<String>,
source_file_name: Option<String>,
handler: &Handler,
config: Option<Config>,
Expand Down Expand Up @@ -769,6 +770,7 @@ impl Options {
inline_sources_content: cfg.inline_sources_content.into_bool(),
input_source_map: cfg.input_source_map.clone().unwrap_or_default(),
output_path: output_path.map(|v| v.to_path_buf()),
source_root,
source_file_name,
comments: comments.cloned(),
preserve_comments,
Expand Down Expand Up @@ -1076,6 +1078,7 @@ pub struct BuiltInput<P: swc_ecma_visit::Fold> {
pub is_module: IsModule,
pub output_path: Option<PathBuf>,

pub source_root: Option<String>,
pub source_file_name: Option<String>,

pub comments: Option<SingleThreadedComments>,
Expand Down Expand Up @@ -1107,6 +1110,7 @@ where
input_source_map: self.input_source_map,
is_module: self.is_module,
output_path: self.output_path,
source_root: self.source_root,
source_file_name: self.source_file_name,
preserve_comments: self.preserve_comments,
inline_sources_content: self.inline_sources_content,
Expand Down
Loading

0 comments on commit ae78669

Please sign in to comment.