diff options
| author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2020-05-11 23:48:36 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-05-11 23:48:36 +0200 |
| commit | d062ffc1baeccca8bf168dc1ce4e94b929478142 (patch) | |
| tree | 17992f7781840b619ce528d70b80c9c798d9ce61 /cli/tsc.rs | |
| parent | 73d8fa74c656841703b51bf8d52d46acf3b97cc9 (diff) | |
fix: source maps in inspector (#5223)
This commit fixes problems with source maps in Chrome Devtools
by substituting source map URL generated by TS compiler with
actual file URL pointing to DENO_DIR.
Dummy value of "source_map_url" has been removed from
"ScriptOrigin".
Also fixes lock file which used compiled source code to generate
lock hash; it now uses source code of the file that is
being compiled.
Diffstat (limited to 'cli/tsc.rs')
| -rw-r--r-- | cli/tsc.rs | 38 |
1 files changed, 34 insertions, 4 deletions
diff --git a/cli/tsc.rs b/cli/tsc.rs index f207558a5..db9c19afe 100644 --- a/cli/tsc.rs +++ b/cli/tsc.rs @@ -619,6 +619,33 @@ impl TsCompiler { return Ok(()); } + // By default TSC output source map url that is relative; we need + // to substitute it manually to correct file URL in DENO_DIR. + let mut content_lines = contents + .split('\n') + .map(|s| s.to_string()) + .collect::<Vec<String>>(); + + if !content_lines.is_empty() { + let last_line = content_lines.pop().unwrap(); + if last_line.starts_with("//# sourceMappingURL=") { + let source_map_key = self.disk_cache.get_cache_filename_with_extension( + module_specifier.as_url(), + "js.map", + ); + let source_map_path = self.disk_cache.location.join(source_map_key); + let source_map_file_url = Url::from_file_path(source_map_path) + .expect("Bad file URL for source map"); + let new_last_line = + format!("//# sourceMappingURL={}", source_map_file_url.to_string()); + content_lines.push(new_last_line); + } else { + content_lines.push(last_line); + } + } + + let contents = content_lines.join("\n"); + let js_key = self .disk_cache .get_cache_filename_with_extension(module_specifier.as_url(), "js"); @@ -878,7 +905,7 @@ mod tests { types_url: None, }; let mock_state = - GlobalState::mock(vec![String::from("deno"), String::from("hello.js")]); + GlobalState::mock(vec![String::from("deno"), String::from("hello.ts")]); let result = mock_state .ts_compiler .compile( @@ -889,11 +916,14 @@ mod tests { ) .await; assert!(result.is_ok()); - assert!(result - .unwrap() - .code + let source_code = result.unwrap().code; + assert!(source_code .as_bytes() .starts_with(b"\"use strict\";\nconsole.log(\"Hello World\");")); + let mut lines: Vec<String> = + source_code.split('\n').map(|s| s.to_string()).collect(); + let last_line = lines.pop().unwrap(); + assert!(last_line.starts_with("//# sourceMappingURL=file://")); } #[tokio::test] |
