Skip to content

Commit

Permalink
fix(es): Ignore sourceMappingURL in string literals (#8879)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #8869
  • Loading branch information
kdy1 committed Apr 22, 2024
1 parent 5d5cf43 commit d7188cd
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 12 deletions.
34 changes: 22 additions & 12 deletions crates/swc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -242,6 +245,7 @@ impl Compiler {
&self,
fm: &SourceFile,
input_src_map: &InputSourceMap,
comments: &[Comment],
is_default: bool,
) -> Result<Option<sourcemap::SourceMap>, Error> {
self.run(|| -> Result<_, Error> {
Expand Down Expand Up @@ -364,22 +368,19 @@ impl Compiler {

let read_sourcemap = || -> Option<sourcemap::SourceMap> {
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);
Expand Down Expand Up @@ -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
};
Expand Down
9 changes: 9 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8869/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"sourceMaps": "inline",
"jsc": {
"target": "es2021",
"parser": {
"syntax": "ecmascript"
}
}
}
1 change: 1 addition & 0 deletions crates/swc/tests/fixture/issues-8xxx/8869/input/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var a = "//# sourceMappingURL=[file].map"
3 changes: 3 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8869/output/1.js

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

0 comments on commit d7188cd

Please sign in to comment.