Skip to content

feat: update build commands #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/basic/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ node_modules

test-results

/dist
/dist-server

/.cache
/build
.env
Expand Down
2 changes: 1 addition & 1 deletion examples/basic/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ test('Should include script and stylesheet tags', async ({ page }) => {
const scriptTag = page.locator('script[type="module"][src="/src/client/index.tsx"]')
await expect(scriptTag).toHaveCount(1)

const linkTag = page.locator('link[rel="stylesheet"][href="/src/style.css"]')
const linkTag = page.locator('link[rel="stylesheet"][href="/src/styles.css"]')
await expect(linkTag).toHaveCount(1)
})

Expand Down
2 changes: 1 addition & 1 deletion examples/basic/src/server/renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const renderer = reactRenderer(({ children }) => {
<meta charSet='UTF-8' />
<meta name='viewport' content='width=device-width, initial-scale=1.0' />
<Script />
<Link href='/src/style.css' rel='stylesheet' />
<Link href='/src/styles.css' rel='stylesheet' />
</head>
<body>{children}</body>
</html>
Expand Down
File renamed without changes.
94 changes: 94 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@types/node": "^22.14.0",
"@types/react": "^19.1.0",
"@types/react-dom": "^19.1.1",
"glob": "^11.0.1",
"np": "^10.2.0",
"prettier": "^3.5.3",
"publint": "^0.3.9",
Expand Down
83 changes: 59 additions & 24 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,80 @@ import { cloudflare } from '@cloudflare/vite-plugin'
import build from '@hono/vite-build/cloudflare-workers'
import tailwindcss from '@tailwindcss/vite'
import react from '@vitejs/plugin-react'
import type { PluginOption } from 'vite'
import type { Plugin, PluginOption } from 'vite'
import ssrHotReload from 'vite-plugin-ssr-hot-reload'

interface Config {
serverEntry: string
buildOutputDir: string
clientEntry: string
cssEntry: string
serverDirectories: string[]
}

const defaultConfig: Config = {
serverEntry: 'src/server.ts',
clientEntry: 'src/client.tsx',
serverEntry: 'src/server/index.tsx',
buildOutputDir: 'dist-server',
clientEntry: 'src/client/index.tsx',
cssEntry: 'src/styles.css',
serverDirectories: ['src/server'],
serverDirectories: ['src/server/*'],
}

export function reactStack(config: Config = defaultConfig): PluginOption[] {
const basePlugins: PluginOption[] = [...cloudflare(), ...tailwindcss()]

return [
{
name: 'hono-vite-react-stack',
config: (_config, env) => {
const isBuild = env.command === 'build'
const isSsrBuild = env.isSsrBuild === true
if (isBuild && isSsrBuild) {
basePlugins.push(build({ entry: config.serverEntry }))
} else if (!isBuild && env.command === 'serve') {
basePlugins.push(
ssrHotReload({
entry: config.serverDirectories,
injectReactRefresh: true,
}),
react()
)
const configPlugin: Plugin = {
name: 'hono-vite-react-stack',
config: (_, env) => {
if (env.command === 'build' && !env.isSsrBuild) {
return {
esbuild: {
exclude: ['react', 'react-dom'],
},
build: {
manifest: true,
rollupOptions: {
input: [config.clientEntry, config.cssEntry],
},
},
}
}
if (env.command === 'build' && env.isSsrBuild) {
return {
resolve: {
alias: {
'react-dom/server': 'react-dom/server.edge',
},
},
}
},
}
},
...basePlugins,
}

const cloudflarePlugins = cloudflare()
for (const plugin of cloudflarePlugins) {
plugin.apply = (_config, env) => env.command === 'serve'
}

const ssrHotReloadPlugin = ssrHotReload({
entry: config.serverDirectories,
injectReactRefresh: true,
})
ssrHotReloadPlugin.apply = (_config, env) => env.command === 'serve'

const reactPlugins = react() as Plugin[]
for (const plugin of reactPlugins) {
plugin.apply = (_config, env) => env.command === 'serve'
}

const buildPlugin = build({ entry: config.serverEntry, outputDir: config.buildOutputDir })

buildPlugin.apply = (_config, env) => env.command === 'build' && env.isSsrBuild === true

return [
configPlugin,
cloudflarePlugins,
reactPlugins,
ssrHotReloadPlugin,
buildPlugin,
tailwindcss(),
]
}