Skip to content

Commit

Permalink
feat: add .jsx and .tsx support
Browse files Browse the repository at this point in the history
  • Loading branch information
justinawrey committed Aug 24, 2022
1 parent b8438c9 commit 0f1fb31
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 18 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,39 @@ export default (req) => {
};
```

As well as `.jsx` and `.tsx` files, with jsx runtime modules from whichever
source you wish:

```tsx
// my-app/pages/blog/post.tsx

/** @jsx h */
import { h, renderSSR } from "https://deno.land/x/nano_jsx@v0.0.33/mod.ts";

function App() {
return (
<html>
<head>
<title>Hello from JSX</title>
</head>
<body>
<h1>Hello world</h1>
</body>
</html>
);
}

export default (_req: Request) => {
const html = renderSSR(<App />);

return new Response(html, {
headers: {
"content-type": "text/html",
},
});
};
```

Initialize a server by calling `fsRouter`:

```typescript
Expand Down
25 changes: 25 additions & 0 deletions _scripts/pages/blog/author.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/** @jsx h */
import { h, renderSSR } from "https://deno.land/x/nano_jsx@v0.0.33/mod.ts";

function App() {
return (
<html>
<head>
<title>Hello from JSX</title>
</head>
<body>
<h1>Hello world</h1>
</body>
</html>
);
}

export default (_req: Request) => {
const html = renderSSR(<App />);

return new Response(html, {
headers: {
"content-type": "text/html",
},
});
};
2 changes: 1 addition & 1 deletion mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export async function fsRouter(
// Only allow typescript files because they are the only files which
// will have actual handler definitions.
// TODO: maybe act as a static file server for all other files?
exts: [".ts", ".js"],
exts: [".ts", ".js", ".jsx", ".tsx"],
};

for await (const filePath of walk(rootDir, walkOpts)) {
Expand Down
1 change: 1 addition & 0 deletions private/deps/std/path.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export {
extname,
relative,
resolve,
toFileUrl,
Expand Down
35 changes: 18 additions & 17 deletions private/parse.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
import { relative } from "./deps/std/path.ts";
import { extname, relative } from "./deps/std/path.ts";

function removeExtension(path: string) {
const ext = extname(path);
return path.slice(0, -ext.length);
}

function removeIndex(path: string) {
if (!path.endsWith("/index")) {
return path;
}

return path.slice(0, -6);
}

// parseRoute takes an absolute file path and transforms it into
// a valid route to which requests can be routed
Expand All @@ -8,21 +21,9 @@ export function parseRoute(
): string {
let route = "/" + relative(absoluteRootDir, absolutePath);

// TODO: this feels janky, although this should work just fine
if (route.endsWith(".ts")) {
route = route.slice(0, -3);
} else if (route.endsWith(".js")) {
route = route.slice(0, -3);
}

if (route.endsWith("index")) {
route = route.slice(0, -5);
}

// Strip trailing '/' from folders acting as index.ts
if (route !== "/" && route.endsWith("/")) {
route = route.slice(0, -1);
}
// Do some processing on the file path to turn it into a valid route
route = removeExtension(route);
route = removeIndex(route);

return route;
return route || "/";
}

0 comments on commit 0f1fb31

Please sign in to comment.