From c9bd056fcd9d82874d5ae97ad5050388c78df634 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Fri, 22 May 2020 12:34:02 +0200 Subject: [PATCH] add test case --- cli/js/compiler.ts | 5 +++++ cli/module_graph.rs | 13 +++++++++---- cli/tests/integration_tests.rs | 7 +++++++ test.ts | 1 - tools/http_server.py | 28 ++++++++++++++++++++++++++++ 5 files changed, 49 insertions(+), 5 deletions(-) delete mode 100644 test.ts diff --git a/cli/js/compiler.ts b/cli/js/compiler.ts index 79cb2d7f9b836e..06117413a1c2ef 100644 --- a/cli/js/compiler.ts +++ b/cli/js/compiler.ts @@ -595,6 +595,11 @@ function buildSourceFileCache( for (const importDesc of entry.imports) { let mappedUrl = importDesc.resolvedSpecifier; const importedFile = sourceFileMap[importDesc.resolvedSpecifier]; + // IMPORTANT: due to HTTP redirects we might end up in situation + // where URL points to a file with completely different URL. + // In that case we take value of `redirect` field and cache + // resolved specifier pointing to the value of the redirect. + // It's not very elegant solution and should be rethinked. assert(importedFile); if (importedFile.redirect) { mappedUrl = importedFile.redirect; diff --git a/cli/module_graph.rs b/cli/module_graph.rs index 46f0f20ade0ccd..0e4c9e815790b9 100644 --- a/cli/module_graph.rs +++ b/cli/module_graph.rs @@ -135,7 +135,8 @@ impl ModuleGraphLoader { self.download_module(specifier.clone(), None)?; loop { - let (specifier, source_file) = self.pending_downloads.next().await.unwrap()?; + let (specifier, source_file) = + self.pending_downloads.next().await.unwrap()?; self.visit_module(&specifier, source_file)?; if self.pending_downloads.is_empty() { break; @@ -354,8 +355,12 @@ impl ModuleGraphLoader { let mut type_headers = vec![]; // IMPORTANT: source_file.url might be different than requested - // module_specifier - this situation needs to be handled manually - if module_specifier.to_string() != source_file.url.to_string() { + // module_specifier because of HTTP redirects. In such + // situation we add an "empty" ModuleGraphFile with 'redirect' + // field set that will be later used in TS worker when building + // map of available source file. It will perform substitution + // for proper URL point to redirect target. + if module_specifier != source_file.url.to_string() { // TODO(bartlomieju): refactor, this is a band-aid self.graph.0.insert( module_specifier.to_string(), @@ -373,7 +378,7 @@ impl ModuleGraphLoader { type_headers: vec![], }, ); - } + } let module_specifier = ModuleSpecifier::from(source_file.url.clone()); let source_code = String::from_utf8(source_file.source_code)?; diff --git a/cli/tests/integration_tests.rs b/cli/tests/integration_tests.rs index cec081ea65254c..e014b3f74e8f9e 100644 --- a/cli/tests/integration_tests.rs +++ b/cli/tests/integration_tests.rs @@ -1565,6 +1565,13 @@ itest!(type_directives_js_main { exit_code: 0, }); +itest!(type_directives_redirect { + args: + "run --reload http://localhost:4545/cli/tests/type_directives_redirect.js", + output: "type_directives_redirect.ts.out", + http_server: true, +}); + itest!(types { args: "types", output: "types.out", diff --git a/test.ts b/test.ts deleted file mode 100644 index 307a916bc4c4cc..00000000000000 --- a/test.ts +++ /dev/null @@ -1 +0,0 @@ -import { parse } from "https://cdn.pika.dev/html5parser@^1.1.0"; \ No newline at end of file diff --git a/tools/http_server.py b/tools/http_server.py index e2e9f2d98ecdd2..346b319f801377 100755 --- a/tools/http_server.py +++ b/tools/http_server.py @@ -122,6 +122,34 @@ def do_GET(self): self.wfile.write(bytes("export const foo = 'foo';")) return + if "type_directives_redirect.js" in self.path: + self.protocol_version = "HTTP/1.1" + self.send_response(200, 'OK') + self.send_header('Content-type', 'application/javascript') + self.send_header( + 'X-TypeScript-Types', + 'http://localhost:4547/xTypeScriptTypesRedirect.d.ts') + self.end_headers() + self.wfile.write(bytes("export const foo = 'foo';")) + return + + if "xTypeScriptTypesRedirect.d.ts" in self.path: + self.protocol_version = "HTTP/1.1" + self.send_response(200, 'OK') + self.send_header('Content-type', 'application/typescript') + self.end_headers() + self.wfile.write( + bytes("import './xTypeScriptTypesRedirected.d.ts';")) + return + + if "xTypeScriptTypesRedirected.d.ts" in self.path: + self.protocol_version = "HTTP/1.1" + self.send_response(200, 'OK') + self.send_header('Content-type', 'application/typescript') + self.end_headers() + self.wfile.write(bytes("export const foo: 'foo';")) + return + if "xTypeScriptTypes.d.ts" in self.path: self.protocol_version = "HTTP/1.1" self.send_response(200, 'OK')