diff --git a/crates/swc/src/lib.rs b/crates/swc/src/lib.rs index 880b295f8f22..1b8ed41647ee 100644 --- a/crates/swc/src/lib.rs +++ b/crates/swc/src/lib.rs @@ -120,7 +120,10 @@ use std::{ use anyhow::{bail, Context, Error}; use base64::prelude::{Engine, BASE64_STANDARD}; -use common::{comments::SingleThreadedComments, errors::HANDLER}; +use common::{ + comments::{Comment, SingleThreadedComments}, + errors::HANDLER, +}; use jsonc_parser::{parse_to_serde_value, ParseOptions}; use once_cell::sync::Lazy; use serde_json::error::Category; @@ -242,6 +245,7 @@ impl Compiler { &self, fm: &SourceFile, input_src_map: &InputSourceMap, + comments: &[Comment], is_default: bool, ) -> Result, Error> { self.run(|| -> Result<_, Error> { @@ -364,22 +368,19 @@ impl Compiler { let read_sourcemap = || -> Option { let s = "sourceMappingURL="; - let idx = fm.src.rfind(s); - let data_url = idx.map(|idx| { - let data_idx = idx + s.len(); - if let Some(end) = fm.src[data_idx..].find('\n').map(|i| i + data_idx + 1) { - &fm.src[data_idx..end] - } else { - &fm.src[data_idx..] - } + let text = comments.iter().rev().find_map(|c| { + let idx = c.text.rfind(s)?; + let (_, url) = c.text.split_at(idx + s.len()); + + Some(url.trim()) }); - match read_inline_sourcemap(data_url) { + match read_inline_sourcemap(text) { Ok(r) => r, Err(err) => { // Load original source map if possible - match read_file_sourcemap(data_url) { + match read_file_sourcemap(text) { Ok(v) => v, Err(_) => { tracing::error!("failed to read input source map: {:?}", err); @@ -697,7 +698,16 @@ impl Compiler { let config = config.with_pass(|pass| chain!(pass, after_pass)); let orig = if config.source_maps.enabled() { - self.get_orig_src_map(&fm, &config.input_source_map, false)? + self.get_orig_src_map( + &fm, + &config.input_source_map, + config + .comments + .get_trailing(config.program.span_hi()) + .as_deref() + .unwrap_or_default(), + false, + )? } else { None }; diff --git a/crates/swc/tests/fixture/issues-8xxx/8869/input/.swcrc b/crates/swc/tests/fixture/issues-8xxx/8869/input/.swcrc new file mode 100644 index 000000000000..92ca8d486d8b --- /dev/null +++ b/crates/swc/tests/fixture/issues-8xxx/8869/input/.swcrc @@ -0,0 +1,9 @@ +{ + "sourceMaps": "inline", + "jsc": { + "target": "es2021", + "parser": { + "syntax": "ecmascript" + } + } +} \ No newline at end of file diff --git a/crates/swc/tests/fixture/issues-8xxx/8869/input/1.js b/crates/swc/tests/fixture/issues-8xxx/8869/input/1.js new file mode 100644 index 000000000000..a96026979290 --- /dev/null +++ b/crates/swc/tests/fixture/issues-8xxx/8869/input/1.js @@ -0,0 +1 @@ +var a = "//# sourceMappingURL=[file].map" diff --git a/crates/swc/tests/fixture/issues-8xxx/8869/output/1.js b/crates/swc/tests/fixture/issues-8xxx/8869/output/1.js new file mode 100644 index 000000000000..4d9de5a71397 --- /dev/null +++ b/crates/swc/tests/fixture/issues-8xxx/8869/output/1.js @@ -0,0 +1,3 @@ +var a = "//# sourceMappingURL=[file].map"; + +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL2lucHV0LzEuanMiXSwic291cmNlc0NvbnRlbnQiOlsidmFyIGEgPSBcIi8vIyBzb3VyY2VNYXBwaW5nVVJMPVtmaWxlXS5tYXBcIlxuIl0sIm5hbWVzIjpbImEiXSwicmFuZ2VNYXBwaW5ncyI6IiIsIm1hcHBpbmdzIjoiQUFBQSxJQUFJQSxJQUFJIn0=