Skip to content

Commit

Permalink
feat(build): Node.js adapter supports port option (#198)
Browse files Browse the repository at this point in the history
* feat(build): Node.js adapter supports `port` option

* add changeset
  • Loading branch information
yusukebe authored Dec 15, 2024
1 parent 57ecab3 commit f08c658
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/fuzzy-books-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hono/vite-build': minor
---

feat: Node.js adapter supports `port` option
2 changes: 2 additions & 0 deletions packages/build/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export default defineConfig({
build({
// Defaults are `src/index.ts`,`./src/index.tsx`,`./app/server.ts`
entry: './src/index.tsx',
// port option is only for Node.js adapter. Default is 3000
port: 3001,
}),
],
})
Expand Down
4 changes: 3 additions & 1 deletion packages/build/src/adapter/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import { serveStaticHook } from '../../entry/serve-static.js'

export type NodeBuildOptions = {
staticRoot?: string | undefined
port?: number | undefined
} & BuildOptions

const nodeBuildPlugin = (pluginOptions?: NodeBuildOptions): Plugin => {
const port = pluginOptions?.port ?? 3000
return {
...buildPlugin({
...{
Expand All @@ -26,7 +28,7 @@ const nodeBuildPlugin = (pluginOptions?: NodeBuildOptions): Plugin => {
async (appName) => {
// eslint-disable-next-line quotes
let code = "import { serve } from '@hono/node-server'\n"
code += `serve(${appName})`
code += `serve({ fetch: ${appName}.fetch, port: ${port.toString()} })`
return code
},
],
Expand Down
40 changes: 40 additions & 0 deletions packages/build/test/adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { existsSync, readFileSync, rmSync } from 'node:fs'
import bunBuildPlugin from '../src/adapter/bun'
import cloudflarePagesPlugin from '../src/adapter/cloudflare-pages'
import denoBuildPlugin from '../src/adapter/deno'
import nodeBuildPlugin from '../src/adapter/node'

describe('Build Plugin with Bun Adapter', () => {
const testDir = './test/mocks/app-static-files'
Expand Down Expand Up @@ -157,3 +158,42 @@ describe('Build Plugin with Deno Adapter', () => {
expect(outputJsClientJs).toContain("console.log('foo')")
})
})

describe('Build Plugin with Node.js Adapter', () => {
const testDir = './test/mocks/app-static-files'
const entry = './src/server.ts'

afterEach(() => {
rmSync(`${testDir}/dist`, { recursive: true, force: true })
})

it('Should build the project correctly with the plugin', async () => {
const outputFile = `${testDir}/dist/index.js`

await build({
root: testDir,
plugins: [
nodeBuildPlugin({
entry,
port: 3001,
minify: false,
}),
],
})

expect(existsSync(outputFile)).toBe(true)

const output = readFileSync(outputFile, 'utf-8')
expect(output).toContain('Hello World')
expect(output).toContain('use("/foo.txt"')
expect(output).toContain('use("/js/*"')
expect(output).toContain('serve({ fetch: mainApp.fetch, port: 3001 })')

const outputFooTxt = readFileSync(`${testDir}/dist/foo.txt`, 'utf-8')
expect(outputFooTxt).toContain('foo')

const outputJsClientJs = readFileSync(`${testDir}/dist/js/client.js`, 'utf-8')
// eslint-disable-next-line quotes
expect(outputJsClientJs).toContain("console.log('foo')")
})
})

0 comments on commit f08c658

Please sign in to comment.