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 {
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");

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
}