Skip to content

Commit

Permalink
add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju committed May 22, 2020
1 parent 41ee861 commit c9bd056
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 5 deletions.
5 changes: 5 additions & 0 deletions cli/js/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 9 additions & 4 deletions cli/module_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
Expand All @@ -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)?;
Expand Down
7 changes: 7 additions & 0 deletions cli/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 0 additions & 1 deletion test.ts

This file was deleted.

28 changes: 28 additions & 0 deletions tools/http_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit c9bd056

Please sign in to comment.