From b12867a5823a338b318b14f1b6e399e61064dce9 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Fri, 26 Aug 2022 11:03:00 +0200 Subject: [PATCH] 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. --- .vscode/import_map.json | 4 +- deno.jsonc => deno.json | 6 +- examples/counter/deno.json | 6 +- examples/counter/fresh.gen.ts | 2 + examples/counter/islands/Counter.tsx | 2 - examples/counter/routes/index.tsx | 2 - init.ts | 43 ++++-------- src/dev/mod.ts | 24 +++++++ src/server/bundle.ts | 17 ++++- src/server/context.ts | 36 ++++++++-- src/server/default_error_page.ts | 58 ++++++++++++++++ src/server/default_error_page.tsx | 62 ----------------- src/server/mod.ts | 13 +++- src/server/{render.tsx => render.ts} | 36 +++++----- src/server/render_test.ts | 2 +- src/server/types.ts | 2 +- tests/error_test.ts | 3 +- tests/fixture/deno.json | 7 ++ tests/fixture/fresh.gen.ts | 2 + tests/fixture/islands/Counter.tsx | 2 - tests/fixture/islands/Test.tsx | 3 - .../islands/kebab-case-counter-test.tsx | 2 - tests/fixture/routes/[name].tsx | 3 - tests/fixture/routes/_404.tsx | 2 - tests/fixture/routes/_500.tsx | 2 - tests/fixture/routes/_app.tsx | 3 - tests/fixture/routes/assetsCaching/index.tsx | 2 - tests/fixture/routes/books/[id].tsx | 2 - tests/fixture/routes/index.tsx | 2 - tests/fixture/routes/intercept.tsx | 2 - tests/fixture/routes/intercept_args.tsx | 2 - tests/fixture/routes/islands/index.tsx | 2 - tests/fixture/routes/params.tsx | 1 - tests/fixture/routes/props/[id].tsx | 2 - tests/fixture/routes/static.tsx | 2 - tests/fixture/routes/wildcard.tsx | 2 - tests/fixture_error/deno.json | 7 ++ tests/fixture_error/fresh.gen.ts | 10 +-- tests/fixture_jsx_pragma/deno.json | 3 + tests/fixture_jsx_pragma/dev.ts | 5 ++ tests/fixture_jsx_pragma/fresh.gen.ts | 20 ++++++ tests/fixture_jsx_pragma/import-map.json | 8 +++ tests/fixture_jsx_pragma/islands/Island.tsx | 12 ++++ tests/fixture_jsx_pragma/main.ts | 9 +++ tests/fixture_jsx_pragma/routes/index.tsx | 12 ++++ tests/fixture_plugin/deno.json | 7 ++ tests/fixture_plugin/fresh.gen.ts | 2 + tests/fixture_plugin/islands/Island.tsx | 2 - tests/fixture_plugin/routes/static.tsx | 2 - tests/fixture_plugin/routes/with-island.tsx | 2 - tests/main_test.ts | 67 ++++++++++++++++++- tests/plugin_test.ts | 3 +- www/components/Button.tsx | 5 +- www/components/DocsSidebar.tsx | 3 - www/components/FeatureIcons.tsx | 3 - www/components/Footer.tsx | 3 - www/components/Icons.tsx | 3 - www/components/NavigationBar.tsx | 3 - www/deno.json | 6 +- www/fresh.gen.ts | 2 + www/islands/CopyArea.tsx | 3 +- www/islands/Counter.tsx | 2 - www/islands/LemonDrop.tsx | 3 - www/routes/_404.tsx | 7 +- www/routes/docs/[...slug].tsx | 3 - www/routes/index.tsx | 7 +- 66 files changed, 374 insertions(+), 213 deletions(-) rename deno.jsonc => deno.json (71%) create mode 100644 src/server/default_error_page.ts delete mode 100644 src/server/default_error_page.tsx rename src/server/{render.tsx => render.ts} (94%) create mode 100644 tests/fixture/deno.json create mode 100644 tests/fixture_error/deno.json create mode 100644 tests/fixture_jsx_pragma/deno.json create mode 100755 tests/fixture_jsx_pragma/dev.ts create mode 100644 tests/fixture_jsx_pragma/fresh.gen.ts create mode 100644 tests/fixture_jsx_pragma/import-map.json create mode 100644 tests/fixture_jsx_pragma/islands/Island.tsx create mode 100644 tests/fixture_jsx_pragma/main.ts create mode 100644 tests/fixture_jsx_pragma/routes/index.tsx create mode 100644 tests/fixture_plugin/deno.json diff --git a/.vscode/import_map.json b/.vscode/import_map.json index 26d19a63..34db80f5 100644 --- a/.vscode/import_map.json +++ b/.vscode/import_map.json @@ -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/": "../", diff --git a/deno.jsonc b/deno.json similarity index 71% rename from deno.jsonc rename to deno.json index cf82765a..062b8d16 100644 --- a/deno.jsonc +++ b/deno.json @@ -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" + } } diff --git a/examples/counter/deno.json b/examples/counter/deno.json index 673d9eb5..8d9ac336 100644 --- a/examples/counter/deno.json +++ b/examples/counter/deno.json @@ -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" + } } diff --git a/examples/counter/fresh.gen.ts b/examples/counter/fresh.gen.ts index d11e969a..8d5fc01d 100644 --- a/examples/counter/fresh.gen.ts +++ b/examples/counter/fresh.gen.ts @@ -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; diff --git a/examples/counter/islands/Counter.tsx b/examples/counter/islands/Counter.tsx index d0878396..8f78ff43 100644 --- a/examples/counter/islands/Counter.tsx +++ b/examples/counter/islands/Counter.tsx @@ -1,5 +1,3 @@ -/** @jsx h */ -import { h } from "preact"; import { useState } from "preact/hooks"; import { IS_BROWSER } from "$fresh/runtime.ts"; diff --git a/examples/counter/routes/index.tsx b/examples/counter/routes/index.tsx index 2e3b4b28..46a70ce1 100644 --- a/examples/counter/routes/index.tsx +++ b/examples/counter/routes/index.tsx @@ -1,5 +1,3 @@ -/** @jsx h */ -import { h } from "preact"; import Counter from "../islands/Counter.tsx"; export default function Home() { diff --git a/init.ts b/init.ts index 7c73315a..3626387c 100644 --- a/init.ts +++ b/init.ts @@ -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) { + +export function Button(props: JSX.HTMLAttributes) { return (