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

feat: support jsx and tsx out of the box #1303

Merged
merged 8 commits into from
Jun 20, 2023
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
1 change: 1 addition & 0 deletions examples/nano-jsx/nitro.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default defineNitroConfig({});
12 changes: 12 additions & 0 deletions examples/nano-jsx/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "example-nano-jsx",
"private": true,
"scripts": {
"dev": "nitro dev",
"build": "nitro build"
},
"devDependencies": {
"nano-jsx": "^0.0.37",
"nitropack": "latest"
}
}
7 changes: 7 additions & 0 deletions examples/nano-jsx/routes/[...path].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineEventHandler } from "h3";
import { h, renderSSR } from "nano-jsx";

export default defineEventHandler(() => {
const html = renderSSR(() => <h1>Nitro + nano-jsx works!</h1>);
return html;
});
14 changes: 14 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ declare module 'nitropack' {
moduleResolution: "Node",
allowJs: true,
resolveJsonModule: true,
jsx: "preserve",
jsxFactory: "h",
jsxFragmentFactory: "Fragment",
paths: nitro.options.typescript.internalPaths
? {
"#internal/nitro": [join(runtimeDir, "index")],
Expand Down
6 changes: 6 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ const NitroDefaults: NitroConfig = {
replace: {},
node: true,
sourceMap: true,
esbuild: {
options: {
jsxFactory: "h",
jsxFragment: "Fragment",
},
},

// Advanced
typescript: {
Expand Down
4 changes: 4 additions & 0 deletions src/rollup/plugins/esbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import type { FilterPattern } from "@rollup/pluginutils";
const defaultLoaders: { [ext: string]: Loader } = {
".ts": "ts",
".js": "js",
".tsx": "tsx",
".jsx": "jsx",
};

export type Options = {
Expand Down Expand Up @@ -81,6 +83,8 @@ export function esbuild(options: Options): Plugin {
const result = await transform(code, {
loader,
target: options.target,
jsxFactory: options.jsxFactory,
jsxFragment: options.jsxFragment,
define: options.define,
sourcemap:
options.sourceMap === "hidden" ? "external" : options.sourceMap,
Expand Down
2 changes: 1 addition & 1 deletion src/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { globby } from "globby";
import { withBase, withLeadingSlash, withoutTrailingSlash } from "ufo";
import type { Nitro, NitroEventHandler } from "./types";

export const GLOB_SCAN_PATTERN = "**/*.{ts,mjs,js,cjs}";
export const GLOB_SCAN_PATTERN = "**/*.{js,mjs,cjs,ts,mts,cts,tsx,jsx}";
type FileInfo = { dir: string; path: string; fullPath: string };

const httpMethodRegex =
Expand Down
10 changes: 10 additions & 0 deletions test/fixture/routes/jsx.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const h = (tag: string, props: any, ...children: any[]) => {
return `<${tag} ${Object.keys(props || {})
.map((key) => `${key}="${props[key]}"`)
.join(" ")
.trim()}>${children.join("")}</${tag}>`;
};

export default eventHandler(() => {
return <h1>Hello JSX!</h1>;
});
5 changes: 5 additions & 0 deletions test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ export function testNitro(
expect(obj.headers.location).toBe("https://nitro.unjs.io/");
});

it("render JSX", async () => {
const { data } = await callHandler({ url: "/jsx" });
expect(data).toMatch("<h1 >Hello JSX!</h1>");
});

it("handles route rules - headers", async () => {
const { headers } = await callHandler({ url: "/rules/headers" });
expect(headers["cache-control"]).toBe("s-maxage=60");
Expand Down
3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
"strict": false,
"allowJs": true,
"resolveJsonModule": true,
"jsx": "preserve",
"jsxFactory": "h",
"jsxFragmentFactory": "Fragment",
"paths": {
"nitropack": ["./src/index"],
"nitropack/config": ["./src/config"],
Expand Down