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

fix(cli): Use the source-file-name and source-root options #6973

Merged
merged 8 commits into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 9 additions & 16 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions bindings/Cargo.lock

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

1 change: 1 addition & 0 deletions bindings/swc_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ rayon = "1"
relative-path = "1.6.1"
serde = { version = "1", features = ["derive"] }
serde_json = { version = "1", features = ["unbounded_depth"] }
sourcemap = "6.2"
swc_core = { version = "0.59.36", features = [
"trace_macro",
"common_concurrent",
Expand Down
80 changes: 65 additions & 15 deletions bindings/swc_cli/src/commands/compile.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{
borrow::Cow,
fs::{self, File},
io::{self, Read, Write},
path::{Component, Path, PathBuf},
Expand Down Expand Up @@ -212,10 +213,22 @@ fn resolve_output_file_path(

fn emit_output(
mut output: TransformOutput,
source_file_name: &Option<String>,
source_root: &Option<String>,
out_dir: &Option<PathBuf>,
file_path: &Path,
file_extension: PathBuf,
) -> anyhow::Result<()> {
let source_map = if let Some(ref source_map) = &output.map {
Some(extend_source_map(
source_map.to_owned(),
source_file_name,
source_root,
)?)
} else {
None
};

if let Some(out_dir) = out_dir {
let output_file_path = resolve_output_file_path(out_dir, file_path, file_extension)?;
let output_dir = output_file_path
Expand All @@ -226,7 +239,7 @@ fn emit_output(
fs::create_dir_all(output_dir)?;
}

if let Some(source_map) = &output.map {
if let Some(ref source_map) = source_map {
let source_map_path = output_file_path.with_extension("js.map");

output.code.push_str("\n//# sourceMappingURL=");
Expand All @@ -239,16 +252,13 @@ fn emit_output(

fs::write(output_file_path, &output.code)?;
} else {
println!(
"{}\n{}\n{}",
file_path.display(),
output.code,
output
.map
.as_ref()
.map(|m| m.to_string())
.unwrap_or_default()
);
let source_map = if let Some(ref source_map) = source_map {
String::from_utf8_lossy(source_map)
} else {
Cow::Borrowed("")
};

println!("{}\n{}\n{}", file_path.display(), output.code, source_map,);
};
Ok(())
}
Expand Down Expand Up @@ -324,6 +334,9 @@ impl CompileOptions {
"true" => SourceMapsConfig::Bool(true),
value => SourceMapsConfig::Str(value.to_string()),
});

options.source_file_name = self.source_file_name.to_owned();
options.source_root = self.source_root.to_owned();
}

Ok(options)
Expand Down Expand Up @@ -469,10 +482,17 @@ impl CompileOptions {
buf_srcmap = Some(File::create(map_out_file)?);
}

let source_map = extend_source_map(
src_map.to_owned(),
&self.source_file_name,
&self.source_root,
)
.unwrap();

buf_srcmap
.as_ref()
.expect("Srcmap buffer should be available")
.write(src_map.as_bytes())
.write(&source_map)
.and(Ok(()))?;
}

Expand All @@ -498,9 +518,14 @@ impl CompileOptions {
let result = execute(compiler, fm, options);

match result {
Ok(output) => {
emit_output(output, &self.out_dir, &file_path, file_extension)
}
Ok(output) => emit_output(
output,
&self.source_file_name,
&self.source_root,
&self.out_dir,
&file_path,
file_extension,
),
Err(e) => Err(e),
}
},
Expand All @@ -509,6 +534,31 @@ impl CompileOptions {
}
}

// TODO: remove once fixed in core https://github.com/swc-project/swc/issues/1388
fn extend_source_map(
source_map: String,
source_file_name: &Option<String>,
source_root: &Option<String>,
) -> anyhow::Result<Vec<u8>> {
let mut source_map = sourcemap::SourceMap::from_reader(source_map.as_bytes())
.context("failed to encode source map")?;

if let Some(ref source_file_name) = source_file_name {
source_map.set_source(0u32, source_file_name);
}

if source_root.is_some() {
source_map.set_source_root(source_root.clone());
}

let mut buf = vec![];
source_map
.to_writer(&mut buf)
.context("failed to decode source map")?;

Ok(buf)
}

#[swc_trace]
impl super::CommandRunner for CompileOptions {
fn execute(&self) -> anyhow::Result<()> {
Expand Down
6 changes: 5 additions & 1 deletion bindings/swc_cli/src/commands/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,11 @@ build-wasm32 = "build --target wasm32-unknown-unknown"
.context("failed to write config toml file")?;

// Create package.json for npm package publishing.
let dist_output_path = format!("target/{}/release/{}.wasm", build_target, name.replace("-", "_"));
let dist_output_path = format!(
"target/{}/release/{}.wasm",
build_target,
name.replace("-", "_")
);
fs::write(
&path.join("package.json"),
format!(
Expand Down
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/sourcemap/004/output/index.map
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"mappings": "AAACA,CAAAA,KAAKC,gBAAmB,GAAGD,KAAKC,gBAAmB,IAAI,EAAC,AAAD,EAAIC,IAAI,CAAC;IAC7D;QACI;KACH;IACD;QACU,MAAY,SAAUC,CAAuB,EAAEC,CAAmB,EAAEC,CAAmB,EAAE;YAC3F;YACA,IAAIC,IAAa,SAAoBC,CAAI,EAAE;gBACvC,IAAIC,IAAOD,EAAKC,IAAI;gBACpB,OAAsB,CAAA,GAAGC,EAA+CC,GAAE,AAAFA,EAAK,OAAO;oBAChFC,UAAUH,EAAKI,GAAG;gBACtB;YACJ;YACAP,EAAoBQ,CAAC,CAACT,IACDC,EAAoBS,CAAC,CAACV,GAAqB;gBACvCW,SAAS,WAAoB;oBAC9C,OAAqBA;gBACzB;gBACqBC,SAAS,WAAoB;oBAC9C,OAAqBV;gBACzB;YACJ,EAAA;YACqB,IAAIG,IAAiDJ,EAAoB,OAC1FU,IAAU,CAAA;QAE1B;QACc,MAAY,SAAUZ,CAAuB,EAAEc,CAAwB,EAAEZ,CAAmB,EAAE;YAC/Fa,CAAAA,OAAOC,QAAQ,GAAGD,OAAOC,QAAQ,IAAI,EAAC,AAAD,EAAIjB,IAAI,C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,EAAoBe,CAAC,EAAA;IAGhE;CACC",
"names": [
"self",
"webpackChunk_N_E",
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ rkyv-latest = { package = "rkyv-test", version = "=0.7.38-test.2", opti
rustc-hash = "1.1.0"
serde = { version = "1.0.119", features = ["derive"] }
siphasher = "0.3.9"
sourcemap = { version = "6", optional = true }
sourcemap = { version = "6.2", optional = true }
string_cache = "0.8.4"
swc_atoms = { version = "0.4.36", path = "../swc_atoms" }
swc_eq_ignore_macros = { version = "0.1.1", path = "../swc_eq_ignore_macros" }
Expand Down