Skip to content

Commit

Permalink
chore: tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm committed Mar 13, 2024
1 parent 925b634 commit cb2a420
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 21 deletions.
3 changes: 1 addition & 2 deletions .scripts/postbuild.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { resolve } from 'node:path'
import glob from 'fast-glob'
import { copy, rename, remove } from 'fs-extra'
import { copy, rename } from 'fs-extra'

await copyDist()
await rewriteHonoJsx()
await prepareTemplates()

async function copyDist() {
await remove('./src/ui/index.html')
await copy('./src/ui', './src/_lib/ui')
}

Expand Down
17 changes: 7 additions & 10 deletions src/dev/devtools.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export type DevtoolsOptions = Pretty<
Pretty<ApiRoutesOptions> & {
assetsPath?: string
/**
* The base path for the devtools instance.
* The base path for the devtools instance off the Frog instances `basePath`.
*
* @default '/dev'
*/
Expand Down Expand Up @@ -44,7 +44,7 @@ export function devtools<
basePath extends string,
///
state = env['State'],
>(frog: FrogBase<env, schema, basePath, state>, options: DevtoolsOptions) {
>(frog: FrogBase<env, schema, basePath, state>, options: DevtoolsOptions = {}) {
const {
appFid,
appMnemonic,
Expand All @@ -60,12 +60,6 @@ export function devtools<
else if (serveStatic) publicPath = `.${basePath}`
else publicPath = frog.assetsPath === '/' ? '' : frog.assetsPath

// TODO:
// - vercel-build output to public dir (works with `publicPath`)
// - rewrite `@hono/vite-dev-server` (`@vite/client` to frames, `frog-client` to devtools)
// - route to /:path/dev
// - middleware?

app
.get('/', (c) => {
return c.html(
Expand All @@ -83,16 +77,19 @@ export function devtools<
<script type="module">
{html`globalThis.__FROG_BASE_URL__ = '${c.req.url}'`}
</script>
<script type="module">
{html`globalThis.__FROG_CLIENT__ = false`}
</script>

<script
type="module"
crossorigin=""
src={`${publicPath}/index.js`}
src={`${publicPath}/main.js`}
/>
<link
rel="stylesheet"
crossorigin=""
href={`${publicPath}/assets/index.css`}
href={`${publicPath}/assets/main.css`}
/>
</head>
<body>
Expand Down
1 change: 1 addition & 0 deletions src/node/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { serveStatic } from '@hono/node-server/serve-static'
5 changes: 5 additions & 0 deletions src/node/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "module",
"types": "../_lib/node/index.d.ts",
"module": "../_lib/node/index.js"
}
4 changes: 4 additions & 0 deletions src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
"types": "./_lib/next/index.d.ts",
"default": "./_lib/next/index.js"
},
"./node": {
"types": "./_lib/node/index.d.ts",
"default": "./_lib/node/index.js"
},
"./vercel": {
"types": "./_lib/vercel/index.d.ts",
"default": "./_lib/vercel/index.js"
Expand Down
29 changes: 21 additions & 8 deletions ui/src/lib/frog-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,30 @@ import { HMRPayload } from 'vite/types/hmrPayload.js'
import { client } from './api'
import { store } from './store'

console.debug('[frog] connecting...')
declare const __FROG_CLIENT__: string | undefined

const importMetaUrl = new URL(import.meta.url)
export function initFrogClient() {
const enabled = (() => {
try {
return typeof __FROG_CLIENT__ === 'boolean' && __FROG_CLIENT__ === true
} catch {
return false
}
})()
if (!enabled) return

console.debug('[frog] connecting...')

const socketProtocol = importMetaUrl.protocol === 'https:' ? 'wss' : 'ws'
const socketHost = `${importMetaUrl.hostname}:${importMetaUrl.port}/`
const importMetaUrl = new URL(import.meta.url)

try {
setupWebSocket(socketProtocol, socketHost)
} catch (error) {
console.error(`[frog] failed to connect to websocket (${error}). `)
const socketProtocol = importMetaUrl.protocol === 'https:' ? 'wss' : 'ws'
const socketHost = `${importMetaUrl.hostname}:${importMetaUrl.port}/`

try {
setupWebSocket(socketProtocol, socketHost)
} catch (error) {
console.error(`[frog] failed to connect to websocket (${error}). `)
}
}

function setupWebSocket(
Expand Down
4 changes: 3 additions & 1 deletion ui/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import React from 'react'
import ReactDOM from 'react-dom/client'

import { App } from './App.tsx'
import { initFrogClient } from './lib/frog-client.ts'

import './lib/frog-client.ts'
import './index.css'

ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<App />
</React.StrictMode>,
)

initFrogClient()
1 change: 1 addition & 0 deletions ui/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default defineConfig({
copyPublicDir: false,
emptyOutDir: true,
rollupOptions: {
input: ['src/main.tsx'],
output: {
assetFileNames: 'assets/[name].[ext]',
chunkFileNames: 'assets/[name].js',
Expand Down

0 comments on commit cb2a420

Please sign in to comment.