fix(es): Allow missing .map file (#7141)

This commit is contained in:
limerick 2023-04-02 11:47:39 +08:00 committed by GitHub
parent f73f96dd94
commit 3e6a1869e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 125 additions and 32 deletions

View File

@ -324,43 +324,68 @@ impl Compiler {
} }
}; };
let path = match data_url { let map_path = match data_url {
Some(data_url) => dir.join(data_url).display().to_string(), Some(data_url) => {
None => { let mut map_path = dir.join(data_url);
format!("{}.map", dir.join(filename).display()) if !map_path.exists() {
} // Old behavior. This check would prevent
}; // regressions.
// Perhaps it shouldn't be supported. Sometimes
let file = File::open(&path) // developers don't want to expose their source code.
.or_else(|err| { // Map files are for internal troubleshooting
// Old behavior. This check would prevent // convenience.
// regressions. map_path = PathBuf::from(format!(
let f = format!("{}.map", filename.display()); "{}.map",
filename.display()
match File::open(f) { ));
Ok(v) => Ok(v), if !map_path.exists() {
Err(_) => Err(err), bail!("failed to find input source map file")
}
} }
})
.context("failed to open input source map file");
let file = if !is_default { Some(map_path)
file? }
} else { None => {
match file { // Old behavior.
Ok(v) => v, let map_path =
Err(_) => return Ok(None), PathBuf::from(format!("{}.map", filename.display()));
if map_path.exists() {
Some(map_path)
} else {
None
}
} }
}; };
Ok(Some(sourcemap::SourceMap::from_reader(file).with_context( match map_path {
|| { Some(map_path) => {
format!( let path = map_path.display().to_string();
"failed to read input source map from file at {}", let file = File::open(&path);
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"); tracing::error!("Failed to load source map for non-file input");

View File

@ -0,0 +1,14 @@
{
"module": {
"type": "commonjs"
},
"jsc": {
"parser": {
"syntax": "typescript",
"tsx": false
},
"target": "es2016"
},
"sourceMaps": true,
"inputSourceMap": true
}

View File

@ -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);
});
});

View File

@ -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);
});
});

View File

@ -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
}