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 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 11 additions & 11 deletions Cargo.lock

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

13 changes: 10 additions & 3 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
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.2"
kdy1 marked this conversation as resolved.
Show resolved Hide resolved
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
@@ -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
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