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 3 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
58 changes: 56 additions & 2 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 @@ -1502,6 +1521,41 @@ 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.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.

👍

}

#[test]
Expand Down
18 changes: 17 additions & 1 deletion cli/tests/019_media_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,38 @@
// mapping of media types end to end.

import { loaded as loadedTs1 } from "http://localhost:4545/cli/tests/subdir/mt_text_typescript.t1.ts";
import { loaded as loadedTsx1 } from "http://localhost:4545/cli/tests/subdir/mt_text_typescript_tsx.t1.tsx";
import { loaded as loadedTs2 } from "http://localhost:4545/cli/tests/subdir/mt_video_vdn.t2.ts";
import { loaded as loadedTsx2 } from "http://localhost:4545/cli/tests/subdir/mt_video_vdn_tsx.t2.tsx";
import { loaded as loadedTs3 } from "http://localhost:4545/cli/tests/subdir/mt_video_mp2t.t3.ts";
import { loaded as loadedTsx3 } from "http://localhost:4545/cli/tests/subdir/mt_video_mp2t_tsx.t3.tsx";
import { loaded as loadedTs4 } from "http://localhost:4545/cli/tests/subdir/mt_application_x_typescript.t4.ts";
import { loaded as loadedTsx4 } from "http://localhost:4545/cli/tests/subdir/mt_application_x_typescript_tsx.t4.tsx";
import { loaded as loadedJs1 } from "http://localhost:4545/cli/tests/subdir/mt_text_javascript.j1.js";
import { loaded as loadedJsx1 } from "http://localhost:4545/cli/tests/subdir/mt_text_javascript_jsx.j1.jsx";
import { loaded as loadedJs2 } from "http://localhost:4545/cli/tests/subdir/mt_application_ecmascript.j2.js";
import { loaded as loadedJsx2 } from "http://localhost:4545/cli/tests/subdir/mt_application_ecmascript_jsx.j2.jsx";
import { loaded as loadedJs3 } from "http://localhost:4545/cli/tests/subdir/mt_text_ecmascript.j3.js";
import { loaded as loadedJsx3 } from "http://localhost:4545/cli/tests/subdir/mt_text_ecmascript_jsx.j3.jsx";
import { loaded as loadedJs4 } from "http://localhost:4545/cli/tests/subdir/mt_application_x_javascript.j4.js";
import { loaded as loadedJsx4 } from "http://localhost:4545/cli/tests/subdir/mt_application_x_javascript_jsx.j4.jsx";

console.log(
"success",
loadedTs1,
loadedTsx1,
loadedTs2,
loadedTsx2,
loadedTs3,
loadedTsx3,
loadedTs4,
loadedTsx4,
loadedJs1,
loadedJsx1,
loadedJs2,
loadedJsx2,
loadedJs3,
loadedJs4
loadedJsx3,
loadedJs4,
loadedJsx4
);
2 changes: 1 addition & 1 deletion cli/tests/019_media_types.ts.out
Original file line number Diff line number Diff line change
@@ -1 +1 @@
success true true true true true true true true
success true true true true true true true true true true true true true true true true
10 changes: 9 additions & 1 deletion cli/tests/022_info_flag_script.out
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@ map: [WILDCARD].js.map
deps:
http://127.0.0.1:4545/cli/tests/019_media_types.ts
├── http://localhost:4545/cli/tests/subdir/mt_text_typescript.t1.ts
├── http://localhost:4545/cli/tests/subdir/mt_text_typescript_tsx.t1.tsx
├── http://localhost:4545/cli/tests/subdir/mt_video_vdn.t2.ts
├── http://localhost:4545/cli/tests/subdir/mt_video_vdn_tsx.t2.tsx
├── http://localhost:4545/cli/tests/subdir/mt_video_mp2t.t3.ts
├── http://localhost:4545/cli/tests/subdir/mt_video_mp2t_tsx.t3.tsx
├── http://localhost:4545/cli/tests/subdir/mt_application_x_typescript.t4.ts
├── http://localhost:4545/cli/tests/subdir/mt_application_x_typescript_tsx.t4.tsx
├── http://localhost:4545/cli/tests/subdir/mt_text_javascript.j1.js
├── http://localhost:4545/cli/tests/subdir/mt_text_javascript_jsx.j1.jsx
├── http://localhost:4545/cli/tests/subdir/mt_application_ecmascript.j2.js
├── http://localhost:4545/cli/tests/subdir/mt_application_ecmascript_jsx.j2.jsx
├── http://localhost:4545/cli/tests/subdir/mt_text_ecmascript.j3.js
└── http://localhost:4545/cli/tests/subdir/mt_application_x_javascript.j4.js
├── http://localhost:4545/cli/tests/subdir/mt_text_ecmascript_jsx.j3.jsx
├── http://localhost:4545/cli/tests/subdir/mt_application_x_javascript.j4.js
└── http://localhost:4545/cli/tests/subdir/mt_application_x_javascript_jsx.j4.jsx
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