-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add kitajs/html example and tests (#6)
* docs: add kitajs/html example and tests * chore: update kitajs/html version
- Loading branch information
Showing
17 changed files
with
426 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# `prerender-macro` Kitajs/html Example | ||
|
||
This is an example with kitajs-html SSR without hotreloading and with a build process. | ||
|
||
To test it: | ||
|
||
- Clone the repo: `git clone git@github.com:aralroca/prerender-macro.git` | ||
- Install dependencies: `cd prerender-macro && bun install` | ||
- Run demo: `bun run demo:kitajs-html` | ||
- Open http://localhost:1234 to see the result | ||
- Look at `examples/kitajs-html/dist/index.js` to verify how the static parts have been converted to HTML in string. | ||
|
||
The static component is translated to html in string in build-time: | ||
|
||
```tsx | ||
"Static Component \uD83E\uDD76 Random number = 0.41381527597071954"; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "kitajs-example", | ||
"private": true, | ||
"module": "build.tsx", | ||
"type": "module", | ||
"scripts": { | ||
"start": "bun run src/build.ts && bun run dist/index.js" | ||
}, | ||
"devDependencies": { | ||
"@types/bun": "latest" | ||
}, | ||
"peerDependencies": { | ||
"typescript": "5.4.3" | ||
}, | ||
"dependencies": { | ||
"prerender-macro": "workspace:*" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { join } from "node:path"; | ||
import prerenderMacro from "prerender-macro"; | ||
|
||
const { success, logs } = await Bun.build({ | ||
entrypoints: [join(import.meta.dir, "index.tsx")], | ||
outdir: join(import.meta.dir, "..", "dist"), | ||
target: "bun", | ||
plugins: [ | ||
prerenderMacro({ | ||
prerenderConfigPath: join(import.meta.dir, "prerender.tsx"), | ||
}), | ||
], | ||
}); | ||
|
||
if (success) console.log("Build complete ✅"); | ||
else console.error("Build failed ❌", logs); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default function DynamicComponent({ name }: { name: string }) { | ||
return ( | ||
<div> | ||
{name} Component 🔥 Random number = {Math.random()} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default function StaticComponent({ name }: { name: string }) { | ||
return ( | ||
<div> | ||
{name} Component 🥶 Random number = {Math.random()} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import DynamicComponent from "./components/dynamic-component"; | ||
import StaticComponent from "./components/static-component" with { type: "prerender" }; | ||
|
||
Bun.serve({ | ||
port: 1234, | ||
fetch: async (request: Request) => { | ||
const page = await ( | ||
<html> | ||
<head> | ||
<title>Prerender Macro | Brisa example</title> | ||
<meta charSet="utf-8" /> | ||
</head> | ||
<body> | ||
<StaticComponent name="Static" /> | ||
<DynamicComponent name="Dynamic" /> | ||
<a href="/">Refresh</a> | ||
</body> | ||
</html> | ||
); | ||
|
||
return new Response(page, { | ||
headers: new Headers({ "Content-Type": "text/html" }), | ||
}); | ||
}, | ||
}); | ||
|
||
console.log("Server running at http://localhost:1234"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { createElement } from "@kitajs/html"; | ||
import prerenderMacroPlugin, { type PrerenderConfig } from "prerender-macro"; | ||
|
||
export const prerenderConfig = { | ||
render: createElement, | ||
} satisfies PrerenderConfig; | ||
|
||
export const plugin = prerenderMacroPlugin({ | ||
prerenderConfigPath: import.meta.url, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"compilerOptions": { | ||
"baseUrl": ".", | ||
"lib": ["dom", "dom.iterable", "esnext"], | ||
"module": "esnext", | ||
"target": "esnext", | ||
"moduleResolution": "bundler", | ||
"moduleDetection": "force", | ||
"jsx": "react-jsx", | ||
"jsxImportSource": "@kitajs/html", | ||
"plugins": [{ "name": "@kitajs/ts-html-plugin" }] | ||
}, | ||
"include": ["src"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export default function Foo({ | ||
name = "foo", | ||
nested = {}, | ||
}: { | ||
name: string; | ||
nested: { foo?: string }; | ||
}) { | ||
return ( | ||
<div> | ||
Foo, {name} | ||
{nested.foo}! | ||
</div> | ||
); | ||
} | ||
|
||
export function Bar({ name = "bar" }: { name: string }) { | ||
return <div>Bar, {name}!</div>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { createElement } from "@kitajs/html"; | ||
import prerenderMacroPlugin, { type PrerenderConfig } from "prerender-macro"; | ||
|
||
export const prerenderConfig = { | ||
render: createElement, | ||
} satisfies PrerenderConfig; | ||
|
||
export const plugin = prerenderMacroPlugin({ | ||
prerenderConfigPath: import.meta.url, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name": "test-kitajs-html", | ||
"private": true, | ||
"scripts": { | ||
"test": "bun test" | ||
}, | ||
"dependencies": { | ||
"prerender-macro": "workspace:*" | ||
} | ||
} |
Oops, something went wrong.