-
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.
feat: add
Link
component for external (built) css (#181)
* feat: add `Link` component for external (built) css * fix: prod as optional
- Loading branch information
Showing
7 changed files
with
108 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { jsxRenderer } from 'hono/jsx-renderer' | ||
import { Link } from '../../../src/server' | ||
|
||
export default jsxRenderer( | ||
({ children }) => { | ||
return ( | ||
<html> | ||
<head> | ||
<Link | ||
href='/app/globals.css' | ||
rel='stylesheet' | ||
prod={true} | ||
manifest={{ 'app/globals.css': { file: 'static/globals-abc.css' } }} | ||
/> | ||
</head> | ||
<body>{children}</body> | ||
</html> | ||
) | ||
}, | ||
{ | ||
docType: false, | ||
} | ||
) |
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,13 @@ | ||
import { Hono } from 'hono' | ||
|
||
const app = new Hono() | ||
|
||
app.get('/', (c) => { | ||
return c.render( | ||
<main> | ||
<div /> | ||
</main> | ||
) | ||
}) | ||
|
||
export default app |
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 Hello() { | ||
return ( | ||
<main> | ||
<div /> | ||
</main> | ||
) | ||
} |
Empty file.
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 { HasIslands } from './has-islands.js' | ||
export { Script } from './script.js' | ||
export { Link } from './link.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import type { FC } from 'hono/jsx' | ||
import type { Manifest } from 'vite' | ||
|
||
type Options = { manifest?: Manifest; prod?: boolean } & JSX.IntrinsicElements['link'] | ||
|
||
export const Link: FC<Options> = async (options) => { | ||
let { href, prod, manifest, ...rest } = options | ||
if (href) { | ||
if (prod ?? import.meta.env.PROD) { | ||
if (!manifest) { | ||
const MANIFEST = import.meta.glob<{ default: Manifest }>('/dist/.vite/manifest.json', { | ||
eager: true, | ||
}) | ||
for (const [, manifestFile] of Object.entries(MANIFEST)) { | ||
if (manifestFile['default']) { | ||
manifest = manifestFile['default'] | ||
break | ||
} | ||
} | ||
} | ||
if (manifest) { | ||
const assetInManifest = manifest[href.replace(/^\//, '')] | ||
if (assetInManifest) { | ||
if (href.startsWith('/')) { | ||
return <link href={`/${assetInManifest.file}`} {...rest}></link> | ||
} | ||
|
||
return <link href={assetInManifest.file} {...rest}></link> | ||
} | ||
} | ||
return <></> | ||
} else { | ||
return <link href={href} {...rest}></link> | ||
} | ||
} | ||
|
||
return <link {...rest} /> | ||
} |
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