From 3e6a1869e84ec01125cf115c3d5f5c5c89645e1a Mon Sep 17 00:00:00 2001 From: limerick Date: Sun, 2 Apr 2023 11:47:39 +0800 Subject: [PATCH] fix(es): Allow missing `.map` file (#7141) --- crates/swc/src/lib.rs | 89 ++++++++++++------- .../tests/fixture/sourcemap/015/input/.swcrc | 14 +++ .../fixture/sourcemap/015/input/index.js | 21 +++++ .../fixture/sourcemap/015/output/index.js | 17 ++++ .../fixture/sourcemap/015/output/index.map | 16 ++++ 5 files changed, 125 insertions(+), 32 deletions(-) create mode 100644 crates/swc/tests/fixture/sourcemap/015/input/.swcrc create mode 100644 crates/swc/tests/fixture/sourcemap/015/input/index.js create mode 100644 crates/swc/tests/fixture/sourcemap/015/output/index.js create mode 100644 crates/swc/tests/fixture/sourcemap/015/output/index.map diff --git a/crates/swc/src/lib.rs b/crates/swc/src/lib.rs index d333fdb11d8..b0d1f3b2246 100644 --- a/crates/swc/src/lib.rs +++ b/crates/swc/src/lib.rs @@ -324,43 +324,68 @@ impl Compiler { } }; - let path = match data_url { - Some(data_url) => dir.join(data_url).display().to_string(), - None => { - format!("{}.map", dir.join(filename).display()) - } - }; - - let file = File::open(&path) - .or_else(|err| { - // Old behavior. This check would prevent - // regressions. - let f = format!("{}.map", filename.display()); - - match File::open(f) { - Ok(v) => Ok(v), - Err(_) => Err(err), + let map_path = match data_url { + Some(data_url) => { + let mut map_path = dir.join(data_url); + if !map_path.exists() { + // Old behavior. This check would prevent + // regressions. + // Perhaps it shouldn't be supported. Sometimes + // developers don't want to expose their source code. + // Map files are for internal troubleshooting + // convenience. + map_path = PathBuf::from(format!( + "{}.map", + filename.display() + )); + if !map_path.exists() { + bail!("failed to find input source map file") + } } - }) - .context("failed to open input source map file"); - let file = if !is_default { - file? - } else { - match file { - Ok(v) => v, - Err(_) => return Ok(None), + Some(map_path) + } + None => { + // Old behavior. + let map_path = + PathBuf::from(format!("{}.map", filename.display())); + if map_path.exists() { + Some(map_path) + } else { + None + } } }; - Ok(Some(sourcemap::SourceMap::from_reader(file).with_context( - || { - format!( - "failed to read input source map from file at {}", - path - ) - }, - )?)) + match map_path { + Some(map_path) => { + let path = map_path.display().to_string(); + let file = File::open(&path); + + // Old behavior. + let file = if !is_default { + file? + } else { + match file { + Ok(v) => v, + Err(_) => return Ok(None), + } + }; + + Ok(Some( + sourcemap::SourceMap::from_reader(file).with_context( + || { + format!( + "failed to read input source map + from file at {}", + path + ) + }, + )?, + )) + } + None => Ok(None), + } } _ => { tracing::error!("Failed to load source map for non-file input"); diff --git a/crates/swc/tests/fixture/sourcemap/015/input/.swcrc b/crates/swc/tests/fixture/sourcemap/015/input/.swcrc new file mode 100644 index 00000000000..13cad06a200 --- /dev/null +++ b/crates/swc/tests/fixture/sourcemap/015/input/.swcrc @@ -0,0 +1,14 @@ +{ + "module": { + "type": "commonjs" + }, + "jsc": { + "parser": { + "syntax": "typescript", + "tsx": false + }, + "target": "es2016" + }, + "sourceMaps": true, + "inputSourceMap": true +} diff --git a/crates/swc/tests/fixture/sourcemap/015/input/index.js b/crates/swc/tests/fixture/sourcemap/015/input/index.js new file mode 100644 index 00000000000..3f9227fabd2 --- /dev/null +++ b/crates/swc/tests/fixture/sourcemap/015/input/index.js @@ -0,0 +1,21 @@ +describe("multiline comments", () => { + it("test1", () => { + expect(false).toBe(true); + }); + + it("test2", () => { + /**/ + expect(false).toBe(true); + }); + + it("test3", () => { + /* + * + */ + expect(false).toBe(true); + }); + + it("test4", () => { + expect(false).toBe(true); + }); +}); diff --git a/crates/swc/tests/fixture/sourcemap/015/output/index.js b/crates/swc/tests/fixture/sourcemap/015/output/index.js new file mode 100644 index 00000000000..a7e215e4ebb --- /dev/null +++ b/crates/swc/tests/fixture/sourcemap/015/output/index.js @@ -0,0 +1,17 @@ +"use strict"; +describe("multiline comments", ()=>{ + it("test1", ()=>{ + expect(false).toBe(true); + }); + it("test2", ()=>{ + /**/ expect(false).toBe(true); + }); + it("test3", ()=>{ + /* + * + */ expect(false).toBe(true); + }); + it("test4", ()=>{ + expect(false).toBe(true); + }); +}); diff --git a/crates/swc/tests/fixture/sourcemap/015/output/index.map b/crates/swc/tests/fixture/sourcemap/015/output/index.map new file mode 100644 index 00000000000..877c2495380 --- /dev/null +++ b/crates/swc/tests/fixture/sourcemap/015/output/index.map @@ -0,0 +1,16 @@ +{ + "mappings": ";AAAAA,SAAS,sBAAsB,IAAM;IACjCC,GAAG,SAAS,IAAM;QACdC,OAAO,KAAK,EAAEC,IAAI,CAAC,IAAI;IAC3B;IAEAF,GAAG,SAAS,IAAM;QACd,EAAE,GACFC,OAAO,KAAK,EAAEC,IAAI,CAAC,IAAI;IAC3B;IAEAF,GAAG,SAAS,IAAM;QACd;;SAEC,GACDC,OAAO,KAAK,EAAEC,IAAI,CAAC,IAAI;IAC3B;IAEAF,GAAG,SAAS,IAAM;QACdC,OAAO,KAAK,EAAEC,IAAI,CAAC,IAAI;IAC3B;AACJ", + "names": [ + "describe", + "it", + "expect", + "toBe" + ], + "sources": [ + "../../input/index.js" + ], + "sourcesContent": [ + "describe(\"multiline comments\", () => {\n it(\"test1\", () => {\n expect(false).toBe(true);\n });\n\n it(\"test2\", () => {\n /**/\n expect(false).toBe(true);\n });\n\n it(\"test3\", () => {\n /*\n *\n */\n expect(false).toBe(true);\n });\n\n it(\"test4\", () => {\n expect(false).toBe(true);\n });\n});\n" + ], + "version": 3 +}