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
+3 -1
View File
@@ -1,5 +1,7 @@
{
".": "THIS FILE EXISTS ONLY FOR VSCODE! IT IS NOT USED AT RUNTIME.",
"scopes": {
"THIS FILE EXISTS ONLY FOR VSCODE! IT IS NOT USED AT RUNTIME": {}
},
"imports": {
"$fresh/": "../",
+5 -1
View File
@@ -5,5 +5,9 @@
"www": "deno run -A --watch=static/,routes/ www/dev.ts",
"screenshot": "deno run -A www/utils/screenshot.ts"
},
"importMap": "./tests/fixture/import_map.json"
"importMap": "./.vscode/import_map.json",
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "preact"
}
}
+5 -1
View File
@@ -2,5 +2,9 @@
"tasks": {
"start": "deno run -A --watch=static/,routes/ dev.ts"
},
"importMap": "./import_map.json"
"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/index.tsx";
import * as $$0 from "./islands/Counter.tsx";
@@ -13,6 +14,7 @@ const manifest = {
"./islands/Counter.tsx": $$0,
},
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";
-2
View File
@@ -1,5 +1,3 @@
/** @jsx h */
import { h } from "preact";
import Counter from "../islands/Counter.tsx";
export default function Home() {
+13 -30
View File
@@ -1,23 +1,8 @@
import { gte, join, parse, resolve } from "./src/dev/deps.ts";
import { join, parse, resolve } from "./src/dev/deps.ts";
import { error } from "./src/dev/error.ts";
import { collect, generate } from "./src/dev/mod.ts";
import { collect, ensureMinDenoVersion, generate } from "./src/dev/mod.ts";
const MIN_VERSION = "1.23.0";
// Check that the minimum supported Deno version is being used.
if (!gte(Deno.version.deno, MIN_VERSION)) {
let message =
`Deno version ${MIN_VERSION} or higher is required. Please update Deno.\n\n`;
if (Deno.execPath().includes("homebrew")) {
message +=
"You seem to have installed Deno via homebrew. To update, run: `brew upgrade deno`\n";
} else {
message += "To update, run: `deno upgrade`\n";
}
error(message);
}
ensureMinDenoVersion();
const help = `fresh-init
@@ -110,9 +95,7 @@ await Deno.writeTextFile(
IMPORT_MAP_JSON,
);
const ROUTES_INDEX_TSX = `/** @jsx h */
import { h } from "preact";
import Counter from "../islands/Counter.tsx";
const ROUTES_INDEX_TSX = `import Counter from "../islands/Counter.tsx";
export default function Home() {
return (
@@ -136,10 +119,10 @@ await Deno.writeTextFile(
ROUTES_INDEX_TSX,
);
const COMPONENTS_BUTTON_TSX = `/** @jsx h */
import { h } from "preact";
const COMPONENTS_BUTTON_TSX = `import { JSX } from "preact";
import { IS_BROWSER } from "$fresh/runtime.ts";
export function Button(props: h.JSX.HTMLAttributes<HTMLButtonElement>) {
export function Button(props: JSX.HTMLAttributes<HTMLButtonElement>) {
return (
<button
{...props}
@@ -157,9 +140,7 @@ await Deno.writeTextFile(
COMPONENTS_BUTTON_TSX,
);
const ISLANDS_COUNTER_TSX = `/** @jsx h */
import { h } from "preact";
import { useState } from "preact/hooks";
const ISLANDS_COUNTER_TSX = `import { useState } from "preact/hooks";
import { Button } from "../components/Button.tsx";
interface CounterProps {
@@ -182,9 +163,7 @@ await Deno.writeTextFile(
ISLANDS_COUNTER_TSX,
);
const ROUTES_GREET_TSX = `/** @jsx h */
import { h } from "preact";
import { PageProps } from "$fresh/server.ts";
const ROUTES_GREET_TSX = `import { PageProps } from "$fresh/server.ts";
export default function Greet(props: PageProps) {
return <div>Hello {props.params.name}</div>;
@@ -300,6 +279,10 @@ const config = {
start: "deno run -A --watch=static/,routes/ dev.ts",
},
importMap: "./import_map.json",
compilerOptions: {
jsx: "react-jsx",
jsxImportSource: "preact",
},
};
const DENO_CONFIG = JSON.stringify(config, null, 2) + "\n";
+24
View File
@@ -2,12 +2,32 @@ import {
dirname,
extname,
fromFileUrl,
gte,
join,
toFileUrl,
walk,
} from "./deps.ts";
import { error } from "./error.ts";
const MIN_DENO_VERSION = "1.25.0";
export function ensureMinDenoVersion() {
// Check that the minimum supported Deno version is being used.
if (!gte(Deno.version.deno, MIN_DENO_VERSION)) {
let message =
`Deno version ${MIN_DENO_VERSION} or higher is required. Please update Deno.\n\n`;
if (Deno.execPath().includes("homebrew")) {
message +=
"You seem to have installed Deno via homebrew. To update, run: `brew upgrade deno`\n";
} else {
message += "To update, run: `deno upgrade`\n";
}
error(message);
}
}
interface Manifest {
routes: string[];
islands: string[];
@@ -83,6 +103,7 @@ export async function generate(directory: string, manifest: Manifest) {
// 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" };
${
routes.map((file, i) => `import * as $${i} from "./routes${file}";`).join(
"\n",
@@ -107,6 +128,7 @@ const manifest = {
}
},
baseUrl: import.meta.url,
config,
};
export default manifest;
@@ -140,6 +162,8 @@ export default manifest;
}
export async function dev(base: string, entrypoint: string) {
ensureMinDenoVersion();
entrypoint = new URL(entrypoint, base).href;
const dir = dirname(fromFileUrl(base));
+16 -1
View File
@@ -3,12 +3,17 @@ import { BUILD_ID } from "./constants.ts";
import { denoPlugin, esbuild, toFileUrl } from "./deps.ts";
import { Island, Plugin } from "./types.ts";
export interface JSXConfig {
jsx: "react" | "react-jsx";
jsxImportSource?: string;
}
let esbuildInitialized: boolean | Promise<void> = false;
async function ensureEsbuildInitialized() {
if (esbuildInitialized === false) {
if (Deno.run === undefined) {
esbuildInitialized = esbuild.initialize({
wasmURL: "https://unpkg.com/esbuild-wasm@0.14.51/esbuild.wasm",
wasmURL: "https://deno.land/x/esbuild@v0.14.51/esbuild.wasm",
worker: false,
});
} else {
@@ -21,8 +26,14 @@ async function ensureEsbuildInitialized() {
}
}
const JSX_RUNTIME_MODE = {
"react": "transform",
"react-jsx": "automatic",
} as const;
export class Bundler {
#importMapURL: URL;
#jsxConfig: JSXConfig;
#islands: Island[];
#plugins: Plugin[];
#cache: Map<string, Uint8Array> | Promise<void> | undefined = undefined;
@@ -32,11 +43,13 @@ export class Bundler {
islands: Island[],
plugins: Plugin[],
importMapURL: URL,
jsxConfig: JSXConfig,
dev: boolean,
) {
this.#islands = islands;
this.#plugins = plugins;
this.#importMapURL = importMapURL;
this.#jsxConfig = jsxConfig;
this.#dev = dev;
}
@@ -82,6 +95,8 @@ export class Bundler {
target: ["chrome99", "firefox99", "safari15"],
treeShaking: true,
write: false,
jsx: JSX_RUNTIME_MODE[this.#jsxConfig.jsx],
jsxImportSource: this.#jsxConfig.jsxImportSource,
});
// const metafileOutputs = bundle.metafile!.outputs;
+32 -4
View File
@@ -11,9 +11,9 @@ import {
} from "./deps.ts";
import { h } from "preact";
import { Manifest } from "./mod.ts";
import { Bundler } from "./bundle.ts";
import { Bundler, JSXConfig } from "./bundle.ts";
import { ALIVE_URL, BUILD_ID, JS_PREFIX, REFRESH_JS_URL } from "./constants.ts";
import DefaultErrorHandler from "./default_error_page.tsx";
import DefaultErrorHandler from "./default_error_page.ts";
import {
AppModule,
ErrorPage,
@@ -31,7 +31,7 @@ import {
UnknownPage,
UnknownPageModule,
} from "./types.ts";
import { render as internalRender } from "./render.tsx";
import { render as internalRender } from "./render.ts";
import { ContentSecurityPolicyDirectives, SELF } from "../runtime/csp.ts";
import { ASSET_CACHE_BUST_KEY, INTERNAL_PREFIX } from "../runtime/utils.ts";
interface RouterState {
@@ -75,6 +75,7 @@ export class ServerContext {
error: ErrorPage,
plugins: Plugin[],
importMapURL: URL,
jsxConfig: JSXConfig,
) {
this.#routes = routes;
this.#islands = islands;
@@ -90,6 +91,7 @@ export class ServerContext {
this.#islands,
this.#plugins,
importMapURL,
jsxConfig,
this.#dev,
);
}
@@ -103,7 +105,32 @@ export class ServerContext {
): Promise<ServerContext> {
// Get the manifest' base URL.
const baseUrl = new URL("./", manifest.baseUrl).href;
const importMapURL = new URL("./import_map.json", manifest.baseUrl);
const config = manifest.config || { importMap: "./import_map.json" };
if (typeof config.importMap !== "string") {
throw new Error("deno.json must contain an 'importMap' property.");
}
const importMapURL = new URL(config.importMap, manifest.baseUrl);
config.compilerOptions ??= {};
let jsx: "react" | "react-jsx";
switch (config.compilerOptions.jsx) {
case "react":
case undefined:
jsx = "react";
break;
case "react-jsx":
jsx = "react-jsx";
break;
default:
throw new Error("Unknown jsx option: " + config.compilerOptions.jsx);
}
const jsxConfig: JSXConfig = {
jsx,
jsxImportSource: config.compilerOptions.jsxImportSource,
};
// Extract all routes, and prepare them into the `Page` structure.
const routes: Route[] = [];
@@ -271,6 +298,7 @@ export class ServerContext {
error,
opts.plugins ?? [],
importMapURL,
jsxConfig,
);
}
+58
View File
@@ -0,0 +1,58 @@
import { h } from "preact";
import { DEBUG } from "./constants.ts";
import type { ErrorPageProps } from "./types.ts";
export default function DefaultErrorPage(props: ErrorPageProps) {
const { error } = props;
let message = undefined;
if (DEBUG) {
if (error instanceof Error) {
message = error.stack;
} else {
message = String(error);
}
}
return h(
"div",
{
style: {
display: "flex",
justifyContent: "center",
alignItems: "center",
},
},
h(
"div",
{
style: {
border: "#f3f4f6 2px solid",
borderTop: "red 4px solid",
background: "#f9fafb",
margin: 16,
minWidth: "300px",
width: "50%",
},
},
h("p", {
style: {
margin: 0,
fontSize: "12pt",
padding: 16,
fontFamily: "sans-serif",
},
}, "An error occured during route handling or page rendering."),
message && h("pre", {
style: {
margin: 0,
fontSize: "12pt",
overflowY: "auto",
padding: 16,
paddingTop: 0,
fontFamily: "monospace",
},
}, message),
),
);
}
-62
View File
@@ -1,62 +0,0 @@
/** @jsx h */
import { h } from "preact";
import { DEBUG } from "./constants.ts";
import type { ErrorPageProps } from "./types.ts";
export default function DefaultErrorPage(props: ErrorPageProps) {
const { error } = props;
let message = undefined;
if (DEBUG) {
if (error instanceof Error) {
message = error.stack;
} else {
message = String(error);
}
}
return (
<div
style={{
display: "flex",
justifyContent: "center",
alignItems: "center",
}}
>
<div
style={{
border: "#f3f4f6 2px solid",
borderTop: "red 4px solid",
background: "#f9fafb",
margin: 16,
minWidth: "300px",
width: "50%",
}}
>
<p
style={{
margin: 0,
fontSize: "12pt",
padding: 16,
fontFamily: "sans-serif",
}}
>
An error occured during route handling or page rendering.
</p>
{message && <pre
style={{
margin: 0,
fontSize: "12pt",
overflowY: "auto",
padding: 16,
paddingTop: 0,
fontFamily: "monospace",
}}
>
{message}
</pre>}
</div>
</div>
);
}
+11 -2
View File
@@ -33,8 +33,8 @@ export type {
UnknownHandlerContext,
UnknownPageProps,
} from "./types.ts";
export { RenderContext } from "./render.tsx";
export type { InnerRenderFunction } from "./render.tsx";
export { RenderContext } from "./render.ts";
export type { InnerRenderFunction } from "./render.ts";
export interface Manifest {
routes: Record<
@@ -47,6 +47,15 @@ export interface Manifest {
>;
islands: Record<string, IslandModule>;
baseUrl: string;
config?: DenoConfig;
}
export interface DenoConfig {
importMap: string;
compilerOptions?: {
jsx?: string;
jsxImportSource?: string;
};
}
export { ServerContext };
+20 -16
View File
@@ -1,4 +1,3 @@
/** @jsx h */
import { renderToString } from "preact-render-to-string";
import { ComponentChildren, ComponentType, h, options } from "preact";
import {
@@ -18,7 +17,6 @@ import { CSP_CONTEXT, nonce, NONE, UNSAFE_INLINE } from "../runtime/csp.ts";
import { ContentSecurityPolicy } from "../runtime/csp.ts";
import { bundleAssetUrl } from "./constants.ts";
import { assetHashingHook } from "../runtime/utils.ts";
import { f } from "https://dev.jspm.io/npm:@jspm/core@1.1.1/nodelibs/chunk-0c2d1322.js";
export interface RenderOptions<Data> {
route: Route<Data> | UnknownPage | ErrorPage;
@@ -348,21 +346,27 @@ export interface TemplateOptions {
}
export function template(opts: TemplateOptions): string {
const page = (
<html lang={opts.lang}>
<head>
<meta charSet="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
{opts.preloads.map((src) => <link rel="modulepreload" href={src} />)}
{opts.imports.map(([src, nonce]) => (
<script src={src} nonce={nonce} type="module"></script>
))}
{opts.headComponents}
</head>
<body dangerouslySetInnerHTML={{ __html: opts.bodyHtml }} />
</html>
const page = h(
"html",
{ lang: opts.lang },
h(
"head",
null,
h("meta", { charSet: "UTF-8" }),
h("meta", {
name: "viewport",
content: "width=device-width, initial-scale=1.0",
}),
opts.preloads.map((src) =>
h("link", { rel: "modulepreload", href: src })
),
opts.imports.map(([src, nonce]) =>
h("script", { src: src, nonce: nonce, type: "module" })
),
opts.headComponents,
),
h("body", { dangerouslySetInnerHTML: { __html: opts.bodyHtml } }),
);
return "<!DOCTYPE html>" + renderToString(page);
}
+1 -1
View File
@@ -1,4 +1,4 @@
import { template } from "./render.tsx";
import { template } from "./render.ts";
import { assertStringIncludes } from "../../tests/deps.ts";
Deno.test("check lang", () => {
+1 -1
View File
@@ -1,6 +1,6 @@
import { ComponentType } from "preact";
import { ConnInfo, rutt, ServeInit } from "./deps.ts";
import { InnerRenderFunction, RenderContext } from "./render.tsx";
import { InnerRenderFunction, RenderContext } from "./render.ts";
// --- APPLICATION CONFIGURATION ---
+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",
+2 -3
View File
@@ -1,7 +1,6 @@
/** @jsx h */
import { h } from "preact";
import { JSX } from "preact";
export function RoundedButton(props: h.JSX.HTMLAttributes<HTMLButtonElement>) {
export function RoundedButton(props: JSX.HTMLAttributes<HTMLButtonElement>) {
return (
<button
style={{
-3
View File
@@ -1,6 +1,3 @@
/** @jsx h */
import { h } from "preact";
import {
CATEGORIES,
TableOfContentsCategory,
-3
View File
@@ -1,6 +1,3 @@
/** @jsx h */
import { h } from "preact";
export function NoBuild() {
return (
<svg
-3
View File
@@ -1,6 +1,3 @@
/** @jsx h */
import { h } from "preact";
const LINKS = [
{
title: "Source",
-3
View File
@@ -1,6 +1,3 @@
/** @jsx h */
import { h } from "preact";
export function IconMinus() {
return (
<svg
-3
View File
@@ -1,6 +1,3 @@
/** @jsx h */
import { h } from "preact";
export default function NavigationBar(props: { active: string }) {
const items = [
{
+5 -1
View File
@@ -2,5 +2,9 @@
"tasks": {
"start": "deno run -A --watch=static/,routes/ dev.ts"
},
"importMap": "./import_map.json"
"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/_404.tsx";
import * as $1 from "./routes/_500.tsx";
import * as $2 from "./routes/docs/[...slug].tsx";
@@ -27,6 +28,7 @@ const manifest = {
"./islands/LemonDrop.tsx": $$2,
},
baseUrl: import.meta.url,
config,
};
export default manifest;
+1 -2
View File
@@ -1,5 +1,4 @@
/** @jsx h */
import { ComponentChildren, h } from "preact";
import { ComponentChildren } from "preact";
import { useEffect, useState } from "preact/hooks";
import { IS_BROWSER } from "$fresh/runtime.ts";
import * as Icons from "../components/Icons.tsx";
-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";
import { RoundedButton } from "../components/Button.tsx";
-3
View File
@@ -1,6 +1,3 @@
/** @jsx h */
/** @jsxFrag Fragment */
import { Fragment, h } from "preact";
import { useEffect, useRef, useState } from "preact/hooks";
import { Spring, WaveTank } from "../components/WaveTank.ts";
+2 -5
View File
@@ -1,13 +1,10 @@
/** @jsx h */
/** @jsxFrag Fragment */
import { Fragment, h } from "preact";
import LemonDrop from "../islands/LemonDrop.tsx";
export function ServerCodePage(
props: { serverCode: number; codeDescription: string },
) {
return (
<Fragment>
<>
<section>
<div class="w-full flex justify-center items-center flex-col bg-green-300">
<LemonDrop />
@@ -26,7 +23,7 @@ export function ServerCodePage(
</p>
</div>
</section>
</Fragment>
</>
);
}
-3
View File
@@ -1,6 +1,3 @@
/** @jsx h */
/** @jsxFrag Fragment */
import { Fragment, h } from "preact";
import { Head } from "$fresh/runtime.ts";
import { Handlers, PageProps } from "$fresh/server.ts";
import { frontMatter, gfm } from "../../utils/markdown.ts";
+2 -5
View File
@@ -1,6 +1,3 @@
/** @jsx h */
/** @jsxFrag Fragment */
import { Fragment, h } from "preact";
import { asset, Head } from "$fresh/runtime.ts";
import { Handlers, PageProps } from "$fresh/server.ts";
import Counter from "../islands/Counter.tsx";
@@ -68,7 +65,7 @@ export default function MainPage(props: PageProps) {
function Hero() {
return (
<Fragment>
<>
<div class="flex justify-end items-center bg-green-300">
<a
href="/docs"
@@ -80,7 +77,7 @@ function Hero() {
<section class="w-full flex justify-center items-center flex-col bg-green-300">
<LemonDrop />
</section>
</Fragment>
</>
);
}