Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [jsx] remote jsx/tsx files were compiled as js/ts #3125

Merged
merged 8 commits into from
Oct 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 97 additions & 7 deletions cli/file_fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,12 +505,16 @@ fn map_content_type(path: &Path, content_type: Option<&str>) -> msg::MediaType {
| "text/typescript"
| "video/vnd.dlna.mpeg-tts"
| "video/mp2t"
| "application/x-typescript" => msg::MediaType::TypeScript,
| "application/x-typescript" => {
map_js_like_extension(path, msg::MediaType::TypeScript)
}
"application/javascript"
| "text/javascript"
| "application/ecmascript"
| "text/ecmascript"
| "application/x-javascript" => msg::MediaType::JavaScript,
| "application/x-javascript" => {
map_js_like_extension(path, msg::MediaType::JavaScript)
}
ry marked this conversation as resolved.
Show resolved Hide resolved
"application/json" | "text/json" => msg::MediaType::Json,
"text/plain" => map_file_extension(path),
_ => {
Expand All @@ -523,6 +527,21 @@ fn map_content_type(path: &Path, content_type: Option<&str>) -> msg::MediaType {
}
}

fn map_js_like_extension(
path: &Path,
default: msg::MediaType,
) -> msg::MediaType {
match path.extension() {
None => default,
Some(os_str) => match os_str.to_str() {
None => default,
Some("jsx") => msg::MediaType::JSX,
Some("tsx") => msg::MediaType::TSX,
Some(_) => default,
},
}
}

fn filter_shebang(bytes: Vec<u8>) -> Vec<u8> {
let string = str::from_utf8(&bytes).unwrap();
if let Some(i) = string.find('\n') {
Expand Down Expand Up @@ -1410,7 +1429,7 @@ mod tests {
}

#[test]
fn test_map_content_type() {
fn test_map_content_type_extension_only() {
// Extension only
assert_eq!(
map_content_type(Path::new("foo/bar.ts"), None),
Expand All @@ -1428,6 +1447,10 @@ mod tests {
map_content_type(Path::new("foo/bar.js"), None),
msg::MediaType::JavaScript
);
assert_eq!(
map_content_type(Path::new("foo/bar.txt"), None),
msg::MediaType::Unknown
);
assert_eq!(
map_content_type(Path::new("foo/bar.jsx"), None),
msg::MediaType::JSX
Expand All @@ -1436,15 +1459,14 @@ mod tests {
map_content_type(Path::new("foo/bar.json"), None),
msg::MediaType::Json
);
assert_eq!(
map_content_type(Path::new("foo/bar.txt"), None),
msg::MediaType::Unknown
);
assert_eq!(
map_content_type(Path::new("foo/bar"), None),
msg::MediaType::Unknown
);
}

#[test]
fn test_map_content_type_media_type_with_no_extension() {
// Media Type
assert_eq!(
map_content_type(Path::new("foo/bar"), Some("application/typescript")),
Expand Down Expand Up @@ -1494,6 +1516,10 @@ mod tests {
map_content_type(Path::new("foo/bar"), Some("text/json")),
msg::MediaType::Json
);
}

#[test]
fn test_map_file_extension_media_type_with_extension() {
assert_eq!(
map_content_type(Path::new("foo/bar.ts"), Some("text/plain")),
msg::MediaType::TypeScript
Expand All @@ -1502,6 +1528,70 @@ mod tests {
map_content_type(Path::new("foo/bar.ts"), Some("foo/bar")),
msg::MediaType::Unknown
);
assert_eq!(
map_content_type(
Path::new("foo/bar.tsx"),
Some("application/typescript")
),
msg::MediaType::TSX
);
assert_eq!(
map_content_type(
Path::new("foo/bar.tsx"),
Some("application/javascript")
),
msg::MediaType::TSX
);
assert_eq!(
map_content_type(
Path::new("foo/bar.tsx"),
Some("application/x-typescript")
),
msg::MediaType::TSX
);
assert_eq!(
map_content_type(
Path::new("foo/bar.tsx"),
Some("video/vnd.dlna.mpeg-tts")
),
msg::MediaType::TSX
);
assert_eq!(
map_content_type(Path::new("foo/bar.tsx"), Some("video/mp2t")),
msg::MediaType::TSX
);
assert_eq!(
map_content_type(
Path::new("foo/bar.jsx"),
Some("application/javascript")
),
msg::MediaType::JSX
);
assert_eq!(
map_content_type(
Path::new("foo/bar.jsx"),
Some("application/x-typescript")
),
msg::MediaType::JSX
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

assert_eq!(
map_content_type(
Path::new("foo/bar.jsx"),
Some("application/ecmascript")
),
msg::MediaType::JSX
);
assert_eq!(
map_content_type(Path::new("foo/bar.jsx"), Some("text/ecmascript")),
msg::MediaType::JSX
);
assert_eq!(
map_content_type(
Path::new("foo/bar.jsx"),
Some("application/x-javascript")
),
msg::MediaType::JSX
);
}

#[test]
Expand Down
23 changes: 23 additions & 0 deletions cli/tests/048_media_types_jsx.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// When run against the test HTTP server, it will serve different media types
// based on the URL containing `.t#.` strings, which exercises the different
// mapping of media types end to end.
import { loaded as loadedTsx1 } from "http://localhost:4545/cli/tests/subdir/mt_text_typescript_tsx.t1.tsx";
import { loaded as loadedTsx2 } from "http://localhost:4545/cli/tests/subdir/mt_video_vdn_tsx.t2.tsx";
import { loaded as loadedTsx3 } from "http://localhost:4545/cli/tests/subdir/mt_video_mp2t_tsx.t3.tsx";
import { loaded as loadedTsx4 } from "http://localhost:4545/cli/tests/subdir/mt_application_x_typescript_tsx.t4.tsx";
import { loaded as loadedJsx1 } from "http://localhost:4545/cli/tests/subdir/mt_text_javascript_jsx.j1.jsx";
import { loaded as loadedJsx2 } from "http://localhost:4545/cli/tests/subdir/mt_application_ecmascript_jsx.j2.jsx";
import { loaded as loadedJsx3 } from "http://localhost:4545/cli/tests/subdir/mt_text_ecmascript_jsx.j3.jsx";
import { loaded as loadedJsx4 } from "http://localhost:4545/cli/tests/subdir/mt_application_x_javascript_jsx.j4.jsx";

console.log(
"success",
loadedTsx1,
loadedTsx2,
loadedTsx3,
loadedTsx4,
loadedJsx1,
loadedJsx2,
loadedJsx3,
loadedJsx4
);
1 change: 1 addition & 0 deletions cli/tests/048_media_types_jsx.ts.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
success true true true true true true true true
14 changes: 14 additions & 0 deletions cli/tests/049_info_flag_script_jsx.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
local: [WILDCARD]048_media_types_jsx.ts
type: TypeScript
compiled: [WILDCARD]048_media_types_jsx.ts.js
map: [WILDCARD]048_media_types_jsx.ts.js.map
deps:
http://127.0.0.1:4545/cli/tests/048_media_types_jsx.ts
├── http://localhost:4545/cli/tests/subdir/mt_text_typescript_tsx.t1.tsx
├── http://localhost:4545/cli/tests/subdir/mt_video_vdn_tsx.t2.tsx
├── http://localhost:4545/cli/tests/subdir/mt_video_mp2t_tsx.t3.tsx
├── http://localhost:4545/cli/tests/subdir/mt_application_x_typescript_tsx.t4.tsx
├── http://localhost:4545/cli/tests/subdir/mt_text_javascript_jsx.j1.jsx
├── http://localhost:4545/cli/tests/subdir/mt_application_ecmascript_jsx.j2.jsx
├── http://localhost:4545/cli/tests/subdir/mt_text_ecmascript_jsx.j3.jsx
└── http://localhost:4545/cli/tests/subdir/mt_application_x_javascript_jsx.j4.jsx
12 changes: 12 additions & 0 deletions cli/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,18 @@ itest!(_047_jsx {
output: "047_jsx_test.jsx.out",
});

itest!(_048_media_types_jsx {
args: "run --reload 048_media_types_jsx.ts",
output: "048_media_types_jsx.ts.out",
http_server: true,
});

itest!(_049_info_flag_script_jsx {
args: "info http://127.0.0.1:4545/cli/tests/048_media_types_jsx.ts",
output: "049_info_flag_script_jsx.out",
http_server: true,
});

itest!(async_error {
exit_code: 1,
args: "run --reload async_error.ts",
Expand Down
5 changes: 5 additions & 0 deletions cli/tests/subdir/mt_application_ecmascript_jsx.j2.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const React = {
createElement() {}
}
const temp = <div></div>;
export const loaded = true;
5 changes: 5 additions & 0 deletions cli/tests/subdir/mt_application_x_javascript_jsx.j4.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const React = {
createElement() {}
}
const temp = <div></div>;
export const loaded = true;
5 changes: 5 additions & 0 deletions cli/tests/subdir/mt_application_x_typescript_tsx.t4.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const React = {
createElement() {}
}
const temp = <div></div>;
export const loaded = true;
5 changes: 5 additions & 0 deletions cli/tests/subdir/mt_javascript_jsx.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const React = {
createElement() {}
}
const temp = <div></div>;
export const loaded = true;
5 changes: 5 additions & 0 deletions cli/tests/subdir/mt_text_ecmascript_jsx.j3.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const React = {
createElement() {}
}
const temp = <div></div>;
export const loaded = true;
5 changes: 5 additions & 0 deletions cli/tests/subdir/mt_text_javascript_jsx.j1.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const React = {
createElement() {}
}
const temp = <div></div>;
export const loaded = true;
5 changes: 5 additions & 0 deletions cli/tests/subdir/mt_text_typescript_tsx.t1.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const React = {
createElement() {}
}
const temp = <div></div>;
export const loaded = true;
5 changes: 5 additions & 0 deletions cli/tests/subdir/mt_video_mp2t_tsx.t3.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const React = {
createElement() {}
}
const temp = <div></div>;
export const loaded = true;
5 changes: 5 additions & 0 deletions cli/tests/subdir/mt_video_vdn_tsx.t2.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const React = {
createElement() {}
}
const temp = <div></div>;
export const loaded = true;
2 changes: 2 additions & 0 deletions tools/http_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ def server():
Handler.extensions_map.update({
".ts": "application/typescript",
".js": "application/javascript",
".tsx": "application/typescript",
".jsx": "application/javascript",
".json": "application/json",
})
SocketServer.TCPServer.allow_reuse_address = True
Expand Down