summaryrefslogtreecommitdiff
path: root/cli/file_fetcher.rs
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2020-05-18 12:59:29 +0200
committerGitHub <noreply@github.com>2020-05-18 12:59:29 +0200
commit9d63772fe5bacc8fa1e0a8cbb152a2f107ae268f (patch)
treef6f547b3b052d101df196850a5cf2cfb56f06f5c /cli/file_fetcher.rs
parentce81064e4c78a5d6213aa19351281c6b86e3e1cb (diff)
refactor: rewrite TS dependency analysis in Rust (#5029)
This commit completely overhauls how module analysis is performed in TS compiler by moving the logic to Rust. In the current setup module analysis is performed using "ts.preProcessFile" API in a special TS compiler worker running on a separate thread. "ts.preProcessFile" allowed us to build a lot of functionality in CLI including X-TypeScript-Types header support and @deno-types directive support. Unfortunately at the same time complexity of the ops required to perform supporting tasks exploded and caused some hidden permission escapes. This PR introduces "ModuleGraphLoader" which can parse source and load recursively all dependent source files; as well as declaration files. All dependencies used in TS compiler and now fetched and collected upfront in Rust before spinning up TS compiler. To achieve feature parity with existing APIs this commit includes a lot of changes: * add "ModuleGraphLoader" - can fetch local and remote sources - parses source code using SWC and extracts imports, exports, file references, special headers - this struct inherited all of the hidden complexity and cruft from TS version and requires several follow up PRs * rewrite cli/tsc.rs to perform module analysis upfront and send all required source code to TS worker in one message * remove op_resolve_modules and op_fetch_source_files from cli/ops/compiler.rs * run TS worker on the same thread
Diffstat (limited to 'cli/file_fetcher.rs')
-rw-r--r--cli/file_fetcher.rs7
1 files changed, 7 insertions, 0 deletions
diff --git a/cli/file_fetcher.rs b/cli/file_fetcher.rs
index 2e75517e6..cbfb340dd 100644
--- a/cli/file_fetcher.rs
+++ b/cli/file_fetcher.rs
@@ -34,6 +34,7 @@ pub struct SourceFile {
pub url: Url,
pub filename: PathBuf,
pub types_url: Option<Url>,
+ pub types_header: Option<String>,
pub media_type: msg::MediaType,
pub source_code: Vec<u8>,
}
@@ -323,6 +324,7 @@ impl SourceFileFetcher {
media_type,
source_code,
types_url,
+ types_header: None,
})
}
@@ -380,6 +382,7 @@ impl SourceFileFetcher {
&fake_filepath,
headers.get("content-type").map(|e| e.as_str()),
);
+ let types_header = headers.get("x-typescript-types").map(|e| e.to_string());
let types_url = match media_type {
msg::MediaType::JavaScript | msg::MediaType::JSX => get_types_url(
&module_url,
@@ -394,6 +397,7 @@ impl SourceFileFetcher {
media_type,
source_code,
types_url,
+ types_header,
}))
}
@@ -502,6 +506,8 @@ impl SourceFileFetcher {
headers.get("content-type").map(String::as_str),
);
+ let types_header =
+ headers.get("x-typescript-types").map(String::to_string);
let types_url = match media_type {
msg::MediaType::JavaScript | msg::MediaType::JSX => get_types_url(
&module_url,
@@ -517,6 +523,7 @@ impl SourceFileFetcher {
media_type,
source_code: source,
types_url,
+ types_header,
};
Ok(source_file)