-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
171 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,11 @@ | ||
// eslint-disable-next-line node/no-extraneous-import | ||
import 'hono' | ||
import type {} from 'hono' | ||
|
||
type Head = { | ||
type Props = { | ||
title?: string | ||
} | ||
|
||
declare module 'hono' { | ||
interface Env { | ||
Variables: {} | ||
Bindings: {} | ||
} | ||
interface ContextRenderer { | ||
(content: string | Promise<string>, head?: Head): Response | Promise<Response> | ||
(content: string | Promise<string>, props?: Props): Response | Promise<Response> | ||
} | ||
} |
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 |
---|---|---|
@@ -1,24 +1,22 @@ | ||
import { jsxRenderer } from 'hono/jsx-renderer' | ||
import { HAS_ISLANDS } from 'honox/server' | ||
|
||
export default jsxRenderer( | ||
({ children, title }) => { | ||
return ( | ||
<html lang='en'> | ||
<head> | ||
<meta charset='UTF-8' /> | ||
<meta name='viewport' content='width=device-width, initial-scale=1.0' /> | ||
{title ? <title>{title}</title> : <></>} | ||
{import.meta.env.PROD ? ( | ||
export default jsxRenderer(({ children, title }) => { | ||
return ( | ||
<html lang='en'> | ||
<head> | ||
<meta charset='UTF-8' /> | ||
<meta name='viewport' content='width=device-width, initial-scale=1.0' /> | ||
{title ? <title>{title}</title> : <></>} | ||
{import.meta.env.PROD ? ( | ||
<HAS_ISLANDS> | ||
<script type='module' src='/static/client.js'></script> | ||
) : ( | ||
<script type='module' src='/app/client.ts'></script> | ||
)} | ||
</head> | ||
<body>{children}</body> | ||
</html> | ||
) | ||
}, | ||
{ | ||
docType: true, | ||
} | ||
) | ||
</HAS_ISLANDS> | ||
) : ( | ||
<script type='module' src='/app/client.ts'></script> | ||
)} | ||
</head> | ||
<body>{children}</body> | ||
</html> | ||
) | ||
}) |
Binary file not shown.
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 |
---|---|---|
|
@@ -117,4 +117,4 @@ | |
"optionalDependencies": { | ||
"@rollup/rollup-linux-x64-gnu": "^4.9.6" | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export const COMPONENT_NAME = 'component-name' | ||
export const DATA_SERIALIZED_PROPS = 'data-serialized-props' | ||
export const IMPORTING_ISLANDS_ID = '__importing_islands' as const |
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,8 @@ | ||
import type { FC } from 'hono/jsx' | ||
import { useRequestContext } from 'hono/jsx-renderer' | ||
import { IMPORTING_ISLANDS_ID } from '../constants.js' | ||
|
||
export const HasIslands: FC = ({ children }) => { | ||
const c = useRequestContext() | ||
return <>{c.get(IMPORTING_ISLANDS_ID) ? children : <></>}</> | ||
} |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { createApp } from './server.js' | ||
export type { ServerOptions } from './server.js' | ||
export { HasIslands } from './components.js' |
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
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,60 @@ | ||
import _generate from '@babel/generator' | ||
import { parse } from '@babel/parser' | ||
import _traverse from '@babel/traverse' | ||
import type { Plugin } from 'vite' | ||
import { IMPORTING_ISLANDS_ID } from '../constants.js' | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
const traverse = (_traverse.default as typeof _traverse) ?? _traverse | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
const generate = (_generate.default as typeof _generate) ?? _generate | ||
|
||
export function injectImportingIslands(): Plugin { | ||
return { | ||
name: 'inject-importing-islands', | ||
transform(code, id) { | ||
if (id.endsWith('.tsx') || id.endsWith('.jsx')) { | ||
let hasIslandsImport = false | ||
const ast = parse(code, { | ||
sourceType: 'module', | ||
plugins: ['jsx'], | ||
}) | ||
|
||
traverse(ast, { | ||
ImportDeclaration(path) { | ||
// We have to make a note that `../components/islands/foo.tsx` is also a target. | ||
if (path.node.source.value.includes('islands/')) { | ||
hasIslandsImport = true | ||
} | ||
}, | ||
}) | ||
|
||
if (hasIslandsImport) { | ||
const hasIslandsNode = { | ||
type: 'ExportNamedDeclaration', | ||
declaration: { | ||
type: 'VariableDeclaration', | ||
declarations: [ | ||
{ | ||
type: 'VariableDeclarator', | ||
id: { type: 'Identifier', name: IMPORTING_ISLANDS_ID }, | ||
init: { type: 'BooleanLiteral', value: true }, | ||
}, | ||
], | ||
kind: 'const', | ||
}, | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
ast.program.body.push(hasIslandsNode as any) | ||
} | ||
|
||
const output = generate(ast, {}, code) | ||
return { | ||
code: output.code, | ||
map: output.map, | ||
} | ||
} | ||
}, | ||
} | ||
} |
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
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