@@ -20,7 +20,7 @@ export function serveCommand(program: Command) {
2020 program
2121 . command ( 'serve' )
2222 . description ( 'Start server' )
23- . argument ( '[entry]' , 'entry file' , './src/index.ts' )
23+ . argument ( '[entry]' , 'entry file' )
2424 . option ( '-p, --port <port>' , 'port number' )
2525 . option ( '--show-routes' , 'show registered routes' )
2626 . option (
@@ -32,43 +32,51 @@ export function serveCommand(program: Command) {
3232 [ ]
3333 )
3434 . action (
35- async ( entry : string , options : { port ?: string ; showRoutes ?: boolean ; use ?: string [ ] } ) => {
36- const appPath = resolve ( process . cwd ( ) , entry )
37-
35+ async (
36+ entry : string | undefined ,
37+ options : { port ?: string ; showRoutes ?: boolean ; use ?: string [ ] }
38+ ) => {
3839 let app : Hono
3940
40- if ( ! existsSync ( appPath ) ) {
41- // Create a default Hono app if entry file doesn't exist
41+ if ( ! entry ) {
42+ // Create a default Hono app if no entry is provided
4243 app = new Hono ( )
4344 } else {
44- const appFilePath = realpathSync ( appPath )
45- const ext = extname ( appFilePath )
46-
47- // TypeScript/JSX files need transformation and bundling
48- if ( [ '.ts' , '.tsx' , '.jsx' ] . includes ( ext ) ) {
49- // Use build API to resolve imports and bundle
50- const result = await esbuild . build ( {
51- entryPoints : [ appFilePath ] ,
52- bundle : true ,
53- write : false ,
54- format : 'esm' ,
55- target : 'node20' ,
56- jsx : 'automatic' ,
57- jsxImportSource : 'hono/jsx' ,
58- platform : 'node' ,
59- external : [ '@hono/node-server' ] , // Keep server external
60- sourcemap : 'inline' ,
61- } )
45+ const appPath = resolve ( process . cwd ( ) , entry )
6246
63- // Execute the bundled code using data URL
64- const code = result . outputFiles [ 0 ] . text
65- const dataUrl = `data:text/javascript;base64,${ Buffer . from ( code ) . toString ( 'base64' ) } `
66- const module = await import ( dataUrl )
67- app = module . default
47+ if ( ! existsSync ( appPath ) ) {
48+ // Create a default Hono app if entry file doesn't exist
49+ app = new Hono ( )
6850 } else {
69- // Regular JS files can be imported directly
70- const module = await import ( pathToFileURL ( appFilePath ) . href )
71- app = module . default
51+ const appFilePath = realpathSync ( appPath )
52+ const ext = extname ( appFilePath )
53+
54+ // TypeScript/JSX files need transformation and bundling
55+ if ( [ '.ts' , '.tsx' , '.jsx' ] . includes ( ext ) ) {
56+ // Use build API to resolve imports and bundle
57+ const result = await esbuild . build ( {
58+ entryPoints : [ appFilePath ] ,
59+ bundle : true ,
60+ write : false ,
61+ format : 'esm' ,
62+ target : 'node20' ,
63+ jsx : 'automatic' ,
64+ jsxImportSource : 'hono/jsx' ,
65+ platform : 'node' ,
66+ external : [ '@hono/node-server' ] , // Keep server external
67+ sourcemap : 'inline' ,
68+ } )
69+
70+ // Execute the bundled code using data URL
71+ const code = result . outputFiles [ 0 ] . text
72+ const dataUrl = `data:text/javascript;base64,${ Buffer . from ( code ) . toString ( 'base64' ) } `
73+ const module = await import ( dataUrl )
74+ app = module . default
75+ } else {
76+ // Regular JS files can be imported directly
77+ const module = await import ( pathToFileURL ( appFilePath ) . href )
78+ app = module . default
79+ }
7280 }
7381 }
7482
0 commit comments