feat: support JSX automatic mode (#610)

This commit adds support for JSX automatic mode instead of the default
mode that requires the presence of the JSX pragma in each file.

This commit also adds the config file to the `fresh.gen.ts` manifest,
which is used to determine if the client emit should use automatic mode.

The minimum Deno version has also been bumped to 1.25.0.
This commit is contained in:
Luca Casonato
2022-08-26 11:03:00 +02:00
committed by GitHub
parent bbe4277acb
commit b12867a582
66 changed files with 374 additions and 213 deletions
+2 -1
View File
@@ -4,8 +4,9 @@ import { assert, assertEquals, assertStringIncludes } from "./deps.ts";
import manifest from "./fixture_error/fresh.gen.ts";
const ctx = await ServerContext.fromManifest(manifest, {});
const handler = ctx.handler();
const router = (req: Request) => {
return ctx.handler()(req, {
return handler(req, {
localAddr: {
transport: "tcp",
hostname: "127.0.0.1",
+7
View File
@@ -0,0 +1,7 @@
{
"importMap": "./import_map.json",
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "preact"
}
}
+2
View File
@@ -2,6 +2,7 @@
// This file SHOULD be checked into source version control.
// This file is automatically updated during development when running `dev.ts`.
import config from "./deno.json" assert { type: "json" };
import * as $0 from "./routes/[name].tsx";
import * as $1 from "./routes/_404.tsx";
import * as $2 from "./routes/_500.tsx";
@@ -69,6 +70,7 @@ const manifest = {
"./islands/kebab-case-counter-test.tsx": $$2,
},
baseUrl: import.meta.url,
config,
};
export default manifest;
-2
View File
@@ -1,5 +1,3 @@
/** @jsx h */
import { h } from "preact";
import { useState } from "preact/hooks";
import { IS_BROWSER } from "$fresh/runtime.ts";
-3
View File
@@ -1,6 +1,3 @@
/** @jsx h */
import { h } from "preact";
export default function Test(props: { message: string }) {
return (
<div>
@@ -1,5 +1,3 @@
/** @jsx h */
import { h } from "preact";
import { useState } from "preact/hooks";
import { IS_BROWSER } from "$fresh/runtime.ts";
-3
View File
@@ -1,6 +1,3 @@
/** @jsx h */
import { h } from "preact";
interface Props {
params: Record<string, string | string[]>;
}
-2
View File
@@ -1,5 +1,3 @@
/** @jsx h */
import { h } from "preact";
import { UnknownPageProps } from "$fresh/server.ts";
export default function NotFoundPage({ url }: UnknownPageProps) {
-2
View File
@@ -1,5 +1,3 @@
/** @jsx h */
import { h } from "preact";
import { ErrorPageProps } from "$fresh/server.ts";
export default function Error500Page({ error }: ErrorPageProps) {
-3
View File
@@ -1,6 +1,3 @@
/** @jsx h */
/** @jsxFrag Fragment */
import { Fragment, h } from "preact";
import { Head } from "$fresh/runtime.ts";
import { AppProps } from "$fresh/server.ts";
@@ -1,5 +1,3 @@
/** @jsx h */
import { h } from "preact";
import Test from "../../islands/Test.tsx";
export default function Home() {
-2
View File
@@ -1,5 +1,3 @@
/** @jsx h */
import { h } from "preact";
import { PageProps, RouteConfig } from "$fresh/server.ts";
export default function Page(props: PageProps) {
-2
View File
@@ -1,5 +1,3 @@
/** @jsx h */
import { h } from "preact";
import { IS_BROWSER } from "$fresh/runtime.ts";
import Test from "../islands/Test.tsx";
-2
View File
@@ -1,5 +1,3 @@
/** @jsx h */
import { h } from "preact";
import { HandlerContext } from "$fresh/server.ts";
export default function Page() {
-2
View File
@@ -1,5 +1,3 @@
/** @jsx h */
import { h } from "preact";
import { Handlers, PageProps } from "$fresh/server.ts";
interface Data extends Record<string, unknown> {
-2
View File
@@ -1,5 +1,3 @@
/** @jsx h */
import { h } from "preact";
import Counter from "../../islands/Counter.tsx";
import KebabCaseFileNameTest from "../../islands/kebab-case-counter-test.tsx";
import Test from "../../islands/Test.tsx";
-1
View File
@@ -1,4 +1,3 @@
/** @jsx h */
import { HandlerContext, RouteConfig } from "$fresh/server.ts";
export const handler = {
-2
View File
@@ -1,5 +1,3 @@
/** @jsx h */
import { h } from "preact";
import { PageProps } from "$fresh/server.ts";
export default function Home(props: PageProps) {
-2
View File
@@ -1,5 +1,3 @@
/** @jsx h */
import { h } from "preact";
import { asset } from "$fresh/runtime.ts";
export default function StaticPage() {
-2
View File
@@ -1,5 +1,3 @@
/** @jsx h */
import { h } from "preact";
import { PageProps, RouteConfig } from "$fresh/server.ts";
export default function WildcardPage({ params }: PageProps) {
+7
View File
@@ -0,0 +1,7 @@
{
"importMap": "./import_map.json",
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "preact"
}
}
+6 -4
View File
@@ -2,16 +2,18 @@
// This file SHOULD be checked into source version control.
// This file is automatically updated during development when running `dev.ts`.
import * as $0 from "./routes/index.tsx";
import * as $1 from "./routes/[...all].ts";
import config from "./deno.json" assert { type: "json" };
import * as $0 from "./routes/[...all].ts";
import * as $1 from "./routes/index.tsx";
const manifest = {
routes: {
"./routes/index.tsx": $0,
"./routes/[...all].ts": $1,
"./routes/[...all].ts": $0,
"./routes/index.tsx": $1,
},
islands: {},
baseUrl: import.meta.url,
config,
};
export default manifest;
+3
View File
@@ -0,0 +1,3 @@
{
"importMap": "./import-map.json"
}
+5
View File
@@ -0,0 +1,5 @@
#!/usr/bin/env -S deno run -A --watch=static/,routes/
import dev from "$fresh/dev.ts";
await dev(import.meta.url, "./main.ts");
+20
View File
@@ -0,0 +1,20 @@
// DO NOT EDIT. This file is generated by fresh.
// This file SHOULD be checked into source version control.
// This file is automatically updated during development when running `dev.ts`.
import config from "./deno.json" assert { type: "json" };
import * as $0 from "./routes/index.tsx";
import * as $$0 from "./islands/Island.tsx";
const manifest = {
routes: {
"./routes/index.tsx": $0,
},
islands: {
"./islands/Island.tsx": $$0,
},
baseUrl: import.meta.url,
config,
};
export default manifest;
+8
View File
@@ -0,0 +1,8 @@
{
"imports": {
"$fresh/": "../../",
"preact": "https://esm.sh/preact@10.10.0",
"preact/": "https://esm.sh/preact@10.10.0/",
"preact-render-to-string": "https://esm.sh/preact-render-to-string@5.2.1?external=preact"
}
}
@@ -0,0 +1,12 @@
/** @jsx h */
import { h } from "preact";
import { IS_BROWSER } from "../../../src/runtime/utils.ts";
export default function Island() {
const id = IS_BROWSER ? "csr" : "ssr";
return (
<div>
<p id={id}>{id}</p>
</div>
);
}
+9
View File
@@ -0,0 +1,9 @@
/// <reference no-default-lib="true" />
/// <reference lib="dom" />
/// <reference lib="dom.asynciterable" />
/// <reference lib="deno.ns" />
/// <reference lib="deno.unstable" />
import { start } from "$fresh/server.ts";
import manifest from "./fresh.gen.ts";
await start(manifest);
+12
View File
@@ -0,0 +1,12 @@
/** @jsx h */
import { h } from "preact";
import Island from "../islands/Island.tsx";
export default function Home() {
return (
<div>
<h1>Hello World</h1>
<Island />
</div>
);
}
+7
View File
@@ -0,0 +1,7 @@
{
"importMap": "./import_map.json",
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "preact"
}
}
+2
View File
@@ -2,6 +2,7 @@
// This file SHOULD be checked into source version control.
// This file is automatically updated during development when running `dev.ts`.
import config from "./deno.json" assert { type: "json" };
import * as $0 from "./routes/static.tsx";
import * as $1 from "./routes/with-island.tsx";
import * as $$0 from "./islands/Island.tsx";
@@ -15,6 +16,7 @@ const manifest = {
"./islands/Island.tsx": $$0,
},
baseUrl: import.meta.url,
config,
};
export default manifest;
-2
View File
@@ -1,5 +1,3 @@
/** @jsx h */
import { h } from "preact";
import { IS_BROWSER } from "../../../src/runtime/utils.ts";
export default function Island() {
-2
View File
@@ -1,5 +1,3 @@
/** @jsx h */
import { h } from "preact";
import { inject } from "../utils/css-inject-plugin.ts";
export default function Home() {
@@ -1,5 +1,3 @@
/** @jsx h */
import { h } from "preact";
import Island from "../islands/Island.tsx";
import { inject } from "../utils/css-inject-plugin.ts";
+65 -2
View File
@@ -1,11 +1,19 @@
import { ServerContext, Status } from "../server.ts";
import { assert, assertEquals, assertStringIncludes } from "./deps.ts";
import {
assert,
assertEquals,
assertStringIncludes,
delay,
puppeteer,
TextLineStream,
} from "./deps.ts";
import manifest from "./fixture/fresh.gen.ts";
import options from "./fixture/options.ts";
const ctx = await ServerContext.fromManifest(manifest, options);
const handler = ctx.handler();
const router = (req: Request) => {
return ctx.handler()(req, {
return handler(req, {
localAddr: {
transport: "tcp",
hostname: "127.0.0.1",
@@ -434,3 +442,58 @@ Deno.test({
assertStringIncludes(body, "404 not found: /not_found");
},
});
Deno.test("jsx pragma works", {
sanitizeOps: false,
sanitizeResources: false,
}, async (t) => {
// Preparation
const serverProcess = Deno.run({
cmd: ["deno", "run", "-A", "./tests/fixture_jsx_pragma/main.ts"],
stdout: "piped",
stderr: "inherit",
});
const decoder = new TextDecoderStream();
const lines = serverProcess.stdout.readable
.pipeThrough(decoder)
.pipeThrough(new TextLineStream());
let started = false;
for await (const line of lines) {
if (line.includes("Listening on http://")) {
started = true;
break;
}
}
if (!started) {
throw new Error("Server didn't start up");
}
await delay(100);
await t.step("ssr", async () => {
const resp = await fetch("http://localhost:8000");
assertEquals(resp.status, Status.OK);
const text = await resp.text();
assertStringIncludes(text, "Hello World");
assertStringIncludes(text, "ssr");
});
const browser = await puppeteer.launch({ args: ["--no-sandbox"] });
const page = await browser.newPage();
await page.goto("http://localhost:8000", {
waitUntil: "networkidle2",
});
await t.step("island is revived", async () => {
await page.waitForSelector("#csr");
});
await browser.close();
await lines.cancel();
serverProcess.kill("SIGTERM");
serverProcess.close();
});
+2 -1
View File
@@ -11,8 +11,9 @@ import manifest from "./fixture_plugin/fresh.gen.ts";
import options from "./fixture_plugin/options.ts";
const ctx = await ServerContext.fromManifest(manifest, options);
const handler = ctx.handler();
const router = (req: Request) => {
return ctx.handler()(req, {
return handler(req, {
localAddr: {
transport: "tcp",
hostname: "127.0.0.1",