Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/forty-tables-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
swc_compiler_base: major
---

feat(es/codegen): Support `sourceMap.url` option of `terser`
5 changes: 4 additions & 1 deletion crates/swc/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ impl Options {
output: JscOutputConfig {
charset,
preamble,
preserve_annotations: cfg.jsc.output.preserve_annotations,
..cfg.jsc.output
},
emit_assert_for_import_attributes: experimental
.emit_assert_for_import_attributes
Expand Down Expand Up @@ -1184,6 +1184,9 @@ pub struct JscOutputConfig {

#[serde(default)]
pub preserve_annotations: BoolConfig<false>,

#[serde(default)]
pub source_map_url: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
Expand Down
10 changes: 6 additions & 4 deletions crates/swc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -760,18 +760,18 @@ impl Compiler {

let target = opts.ecma.clone().into();

let (source_map, orig) = opts
let (source_map, orig, source_map_url) = opts
.source_map
.as_ref()
.map(|obj| -> Result<_, Error> {
let orig = obj.content.as_ref().map(|s| s.to_sourcemap()).transpose()?;

Ok((SourceMapsConfig::Bool(true), orig))
Ok((SourceMapsConfig::Bool(true), orig, obj.url.as_deref()))
})
.unwrap_as_option(|v| {
Some(Ok(match v {
Some(true) => (SourceMapsConfig::Bool(true), None),
_ => (SourceMapsConfig::Bool(false), None),
Some(true) => (SourceMapsConfig::Bool(true), None, None),
_ => (SourceMapsConfig::Bool(false), None, None),
}))
})
.unwrap()?;
Expand Down Expand Up @@ -924,6 +924,7 @@ impl Compiler {
.reduce_escaped_newline,
),
output: None,
source_map_url,
},
);

Expand Down Expand Up @@ -1073,6 +1074,7 @@ impl Compiler {
} else {
Some(output)
},
source_map_url: config.output.source_map_url.as_deref(),
},
)
})
Expand Down
8 changes: 8 additions & 0 deletions crates/swc/tests/fixture/issues-10xxx/10346/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"sourceMaps": true,
"jsc": {
"output": {
"sourceMapUrl": "input.js.map"
}
}
}
2 changes: 2 additions & 0 deletions crates/swc/tests/fixture/issues-10xxx/10346/input/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

export const foo = 1;
3 changes: 3 additions & 0 deletions crates/swc/tests/fixture/issues-10xxx/10346/output/input.js

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

13 changes: 13 additions & 0 deletions crates/swc/tests/fixture/issues-10xxx/10346/output/input.map
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"mappings": "AACA,OAAO,IAAMA,MAAM,EAAE",
"names": [
"foo"
],
"sources": [
"../../input/input.js"
],
"sourcesContent": [
"\nexport const foo = 1;"
],
"version": 3
}

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

12 changes: 10 additions & 2 deletions crates/swc_compiler_base/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ pub struct PrintArgs<'a> {
pub preamble: &'a str,
pub codegen_config: swc_ecma_codegen::Config,
pub output: Option<FxHashMap<String, String>>,
pub source_map_url: Option<&'a str>,
}

impl Default for PrintArgs<'_> {
Expand All @@ -138,6 +139,7 @@ impl Default for PrintArgs<'_> {
preamble: "",
codegen_config: Default::default(),
output: None,
source_map_url: None,
}
}
}
Expand Down Expand Up @@ -168,6 +170,7 @@ pub fn print<T>(
preamble,
codegen_config,
output,
source_map_url,
}: PrintArgs,
) -> Result<TransformOutput, Error>
where
Expand All @@ -177,7 +180,7 @@ where

let mut src_map_buf = Vec::new();

let src = {
let mut src = {
let mut buf = std::vec::Vec::new();
{
let mut w = swc_ecma_codegen::text_writer::JsWriter::new(
Expand Down Expand Up @@ -251,13 +254,18 @@ where
.to_writer(&mut buf)
.context("failed to write source map")?;
let map = String::from_utf8(buf).context("source map is not utf-8")?;

if let Some(source_map_url) = source_map_url {
src.push_str("\n//# sourceMappingURL=");
src.push_str(source_map_url);
}

(src, Some(map))
} else {
(src, None)
}
}
SourceMapsConfig::Str(_) => {
let mut src = src;
let mut buf = std::vec::Vec::new();

map.unwrap()
Expand Down
Loading