Skip to content

Commit 566bc7c

Browse files
authored
feat(es/codegen): Support sourceMap.url option of terser (#10346)
**Related issue:** - vercel/ncc#1258
1 parent 54302b1 commit 566bc7c

File tree

9 files changed

+52
-7
lines changed

9 files changed

+52
-7
lines changed

.changeset/forty-tables-obey.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
swc_compiler_base: major
3+
---
4+
5+
feat(es/codegen): Support `sourceMap.url` option of `terser`

crates/swc/src/config/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ impl Options {
767767
output: JscOutputConfig {
768768
charset,
769769
preamble,
770-
preserve_annotations: cfg.jsc.output.preserve_annotations,
770+
..cfg.jsc.output
771771
},
772772
emit_assert_for_import_attributes: experimental
773773
.emit_assert_for_import_attributes
@@ -1184,6 +1184,9 @@ pub struct JscOutputConfig {
11841184

11851185
#[serde(default)]
11861186
pub preserve_annotations: BoolConfig<false>,
1187+
1188+
#[serde(default)]
1189+
pub source_map_url: Option<String>,
11871190
}
11881191

11891192
#[derive(Debug, Clone, Serialize, Deserialize, Default)]

crates/swc/src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -760,18 +760,18 @@ impl Compiler {
760760

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

763-
let (source_map, orig) = opts
763+
let (source_map, orig, source_map_url) = opts
764764
.source_map
765765
.as_ref()
766766
.map(|obj| -> Result<_, Error> {
767767
let orig = obj.content.as_ref().map(|s| s.to_sourcemap()).transpose()?;
768768

769-
Ok((SourceMapsConfig::Bool(true), orig))
769+
Ok((SourceMapsConfig::Bool(true), orig, obj.url.as_deref()))
770770
})
771771
.unwrap_as_option(|v| {
772772
Some(Ok(match v {
773-
Some(true) => (SourceMapsConfig::Bool(true), None),
774-
_ => (SourceMapsConfig::Bool(false), None),
773+
Some(true) => (SourceMapsConfig::Bool(true), None, None),
774+
_ => (SourceMapsConfig::Bool(false), None, None),
775775
}))
776776
})
777777
.unwrap()?;
@@ -924,6 +924,7 @@ impl Compiler {
924924
.reduce_escaped_newline,
925925
),
926926
output: None,
927+
source_map_url,
927928
},
928929
);
929930

@@ -1073,6 +1074,7 @@ impl Compiler {
10731074
} else {
10741075
Some(output)
10751076
},
1077+
source_map_url: config.output.source_map_url.as_deref(),
10761078
},
10771079
)
10781080
})
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"sourceMaps": true,
3+
"jsc": {
4+
"output": {
5+
"sourceMapUrl": "input.js.map"
6+
}
7+
}
8+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
export const foo = 1;

crates/swc/tests/fixture/issues-10xxx/10346/output/input.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"mappings": "AACA,OAAO,IAAMA,MAAM,EAAE",
3+
"names": [
4+
"foo"
5+
],
6+
"sources": [
7+
"../../input/input.js"
8+
],
9+
"sourcesContent": [
10+
"\nexport const foo = 1;"
11+
],
12+
"version": 3
13+
}

crates/swc/tests/minify/issue-7475/issue-8372/1/output.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/swc_compiler_base/src/lib.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ pub struct PrintArgs<'a> {
119119
pub preamble: &'a str,
120120
pub codegen_config: swc_ecma_codegen::Config,
121121
pub output: Option<FxHashMap<String, String>>,
122+
pub source_map_url: Option<&'a str>,
122123
}
123124

124125
impl Default for PrintArgs<'_> {
@@ -138,6 +139,7 @@ impl Default for PrintArgs<'_> {
138139
preamble: "",
139140
codegen_config: Default::default(),
140141
output: None,
142+
source_map_url: None,
141143
}
142144
}
143145
}
@@ -168,6 +170,7 @@ pub fn print<T>(
168170
preamble,
169171
codegen_config,
170172
output,
173+
source_map_url,
171174
}: PrintArgs,
172175
) -> Result<TransformOutput, Error>
173176
where
@@ -177,7 +180,7 @@ where
177180

178181
let mut src_map_buf = Vec::new();
179182

180-
let src = {
183+
let mut src = {
181184
let mut buf = std::vec::Vec::new();
182185
{
183186
let mut w = swc_ecma_codegen::text_writer::JsWriter::new(
@@ -251,13 +254,18 @@ where
251254
.to_writer(&mut buf)
252255
.context("failed to write source map")?;
253256
let map = String::from_utf8(buf).context("source map is not utf-8")?;
257+
258+
if let Some(source_map_url) = source_map_url {
259+
src.push_str("\n//# sourceMappingURL=");
260+
src.push_str(source_map_url);
261+
}
262+
254263
(src, Some(map))
255264
} else {
256265
(src, None)
257266
}
258267
}
259268
SourceMapsConfig::Str(_) => {
260-
let mut src = src;
261269
let mut buf = std::vec::Vec::new();
262270

263271
map.unwrap()

0 commit comments

Comments
 (0)