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
+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 ---