Skip to content

Commit

Permalink
deno preact
Browse files Browse the repository at this point in the history
  • Loading branch information
narumincho committed Jul 7, 2024
1 parent c993945 commit c0d9fbe
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 28 deletions.
5 changes: 3 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"deno.enable": true,
"eslint.enable": false,
"prettier.enable": false,
"editor.tabSize": 2,
"editor.formatOnSave": true,
"[json]": {
"editor.defaultFormatter": "denoland.vscode-deno"
Expand All @@ -17,5 +18,5 @@
},
"[markdown]": {
"editor.defaultFormatter": "denoland.vscode-deno"
},
}
}
}
17 changes: 11 additions & 6 deletions client/main.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import React from "https://esm.sh/[email protected]";

export const App = () => {
const [count, setCount] = React.useState(0);
import {
Dispatch,
StateUpdater,
} from "https://esm.sh/[email protected]/hooks?pin=v135";
import { h } from "https://esm.sh/[email protected]?pin=v135";

export const App = (props: {
readonly state: number;
readonly setState: Dispatch<StateUpdater<number>>;
}) => {
return (
<div>
<h1>definy</h1>
<button
onClick={() => {
setCount(count + 1);
props.setState((prev) => prev + 1);
}}
>
count: {count}
count: {props.state}
</button>
</div>
);
Expand Down
13 changes: 9 additions & 4 deletions client/start.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { createRoot } from "https://esm.sh/[email protected]/client";
import { App } from "./main.tsx";
import React from "https://esm.sh/v128/@types/[email protected]/index.d.ts";
import { h, hydrate, JSX } from "https://esm.sh/[email protected]?pin=v135";
import { useState } from "https://esm.sh/[email protected]/hooks?pin=v135";

const root = document.getElementById("root");
if (root === null) {
throw new Error("root element not found");
throw new Error("root element not found");
}

createRoot(root).render(<App />);
const AppWithState = (): JSX.Element => {
const [state, setState] = useState(0);
return <App state={state} setState={setState} />;
};

hydrate(<AppWithState />, root);
3 changes: 2 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"dom.iterable",
"deno.ns",
"deno.unstable"
]
],
"jsxFactory": "h"
},
"tasks": {
"server-dev": "deno run --check --watch --allow-env --allow-net --allow-read --allow-write=./dist.json ./script/startInLocal.ts",
Expand Down
40 changes: 40 additions & 0 deletions deno.lock

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

2 changes: 1 addition & 1 deletion dist.json

Large diffs are not rendered by default.

20 changes: 16 additions & 4 deletions script/buildClient.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
import { bundle } from "https://deno.land/x/[email protected]/mod.ts";
import { bundle } from "jsr:@deno/emit";
import { encodeHex } from "jsr:@std/encoding/hex";

const { code } = await bundle(
new URL("../client/start.tsx", import.meta.url),
new URL("../client/start.tsx", import.meta.url),
{
compilerOptions: { jsxFactory: "h" },
},
);

await Deno.writeTextFile(
new URL("../dist.json", import.meta.url),
JSON.stringify({ code }),
new URL("../dist.json", import.meta.url),
JSON.stringify({
clientJavaScript: code,
clientJavaScriptHash: encodeHex(
await crypto.subtle.digest(
"SHA-256",
new TextEncoder().encode(code),
),
),
}),
);
20 changes: 10 additions & 10 deletions server/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ import { printSchema } from "npm:graphql";
import { createHandler } from "npm:graphql-http/lib/use/fetch";
import { Context, createContext } from "./context.ts";
import { schema } from "./schema.ts";
import { renderToString } from "https://esm.sh/[email protected]/server";
import * as React from "https://esm.sh/[email protected]";
import { renderToString } from "npm:preact-render-to-string";
import { h } from "https://esm.sh/[email protected]?pin=v135";
import { App } from "../client/main.tsx";
import dist from "../dist.json" with { type: "json" };

export const startDefinyServer = (parameter: {
readonly denoKvDatabasePath: string | undefined;
}) => {
Deno.serve(async (request) => {
const cors = supportCrossOriginResourceSharing(request);
if (cors.type === "skipMainProcess") {
return cors.response;
}
const pathname = new URL(request.url).pathname;
switch (pathname) {
case "/":
Expand All @@ -19,11 +23,11 @@ export const startDefinyServer = (parameter: {
<html>
<head>
<title>definy</title>
<script type="module" src="/script" />
<script type="module" src={`/${dist.clientJavaScriptHash}`} />
</head>
<body>
<div id="root">
<App />
<App state={0} setState={() => {}} />
</div>
</body>
</html>,
Expand All @@ -34,17 +38,13 @@ export const startDefinyServer = (parameter: {
},
},
);
case "/script":
return new Response(dist.code, {
case `/${dist.clientJavaScriptHash}`:
return new Response(dist.clientJavaScript, {
headers: {
"content-type": "application/javascript; charset=utf-8",
},
});
}
const cors = supportCrossOriginResourceSharing(request);
if (cors.type === "skipMainProcess") {
return cors.response;
}

if (request.headers.get("accept")?.includes("html")) {
return new Response(apolloStudioEmbeddedHtml(printSchema(schema)), {
Expand Down

0 comments on commit c0d9fbe

Please sign in to comment.