mirror of
https://github.com/swc-project/swc.git
synced 2024-12-24 22:22:34 +03:00
fix(es): Allow missing .map
file (#7141)
This commit is contained in:
parent
f73f96dd94
commit
3e6a1869e8
@ -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");
|
||||
|
14
crates/swc/tests/fixture/sourcemap/015/input/.swcrc
Normal file
14
crates/swc/tests/fixture/sourcemap/015/input/.swcrc
Normal file
@ -0,0 +1,14 @@
|
||||
{
|
||||
"module": {
|
||||
"type": "commonjs"
|
||||
},
|
||||
"jsc": {
|
||||
"parser": {
|
||||
"syntax": "typescript",
|
||||
"tsx": false
|
||||
},
|
||||
"target": "es2016"
|
||||
},
|
||||
"sourceMaps": true,
|
||||
"inputSourceMap": true
|
||||
}
|
21
crates/swc/tests/fixture/sourcemap/015/input/index.js
Normal file
21
crates/swc/tests/fixture/sourcemap/015/input/index.js
Normal 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);
|
||||
});
|
||||
});
|
17
crates/swc/tests/fixture/sourcemap/015/output/index.js
Normal file
17
crates/swc/tests/fixture/sourcemap/015/output/index.js
Normal 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);
|
||||
});
|
||||
});
|
16
crates/swc/tests/fixture/sourcemap/015/output/index.map
Normal file
16
crates/swc/tests/fixture/sourcemap/015/output/index.map
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user