Conversation
Build a optimized Hono class.
a948a56 to
88f8e03
Compare
|
@yusukebe |
|
Hi @usualoma ! Thank you for the PR. Looks great! But we have something to discuss. How can we use the generated file?In the current implementation, The source file import { Hono } from 'hono'
const app = new Hono()
app.get('/', (c) => c.text('Hi'))
export default appThe generated file: // This file is generated by `hono optimize`
import { HonoBase } from 'hono/hono-base'
import type { HonoOptions } from 'hono/hono-base'
import { PreparedRegExpRouter } from 'hono/router/reg-exp-router'
import type { BlankEnv, BlankSchema, Env, Schema } from 'hono/types'
export class Hono<
E extends Env = BlankEnv,
S extends Schema = BlankSchema,
BasePath extends string = '/',
> extends HonoBase<E, S, BasePath> {
constructor(options: HonoOptions<E> = {}) {
super(options)
const routerParams = [
{ ALL: [/^$/, [], { '/': [[], []] }] },
{ '/': [[[''], null]] },
] as unknown as ConstructorParameters<typeof PreparedRegExpRouter>
this.router = new PreparedRegExpRouter(...routerParams) as unknown as typeof this.router
}
}Then, how can I use this? Importing the // src/index.ts
import { Hono } from './hono-optimized'
const app = new Hono()
app.get('/', (c) => c.text('Hi'))
export default appOr, creating another file that imports the optimized I know we can do it in many ways. However, the above cases are troublesome because we need to rewrite the import path to Another way to avoid changing the code is generating the optimized file, which includes optimized // src/hono-optimized-with-app.ts (the naming should be fixed)
import { HonoBase } from 'hono/hono-base'
import type { HonoOptions } from 'hono/hono-base'
import { PreparedRegExpRouter } from 'hono/router/reg-exp-router'
import type { BlankEnv, BlankSchema, Env, Schema } from 'hono/types'
class Hono<
E extends Env = BlankEnv,
S extends Schema = BlankSchema,
BasePath extends string = '/',
> extends HonoBase<E, S, BasePath> {
constructor(options: HonoOptions<E> = {}) {
super(options)
const routerParams = [
{ ALL: [/^$/, [], { '/': [[], []] }] },
{ '/': [[[''], null]] },
] as unknown as ConstructorParameters<typeof PreparedRegExpRouter>
this.router = new PreparedRegExpRouter(...routerParams) as unknown as typeof this.router
}
}
const app = new Hono()
app.get('/', (c) => c.text('Hi'))
export default appIf so, the user can run the optimized file directly: I don't know if that is a good way to include the app in the optimized The bundle file includes
|
|
Hi @yusukebe, thanks for your comment! First, I will address the following points.
The cause was on the hono side. This is expected to be fixed in the following PR. |
I had envisioned a method where it would be rewritten as follows after generation. // src/index.ts
import { Hono } from './hono-optimized'
const app = new Hono()
app.get('/', (c) => c.text('Hi'))
export default appWhen // src/subapp.ts
import { Hono } from 'hono'
const app = new Hono()
app.get('/subapp', (c) => c.text('Hi'))
export default app// src/index.ts
import { Hono } from 'hono'
import SubApp from './subapp'
const app = new Hono()
app.get('/', (c) => c.text('Hi'))
app.route('/', SubApp)
export default appIf we go as far as bundling with esbuild within |
I indeed... It's not a good idea to replace the application files.
I think this "replace The word So I think these are better:
The use case will be:
This is simple. What do you think of it? |
8ac1525 to
529f9d6
Compare
|
@yusukebe |
| assignRouterStatement = 'this.router = new TrieRouter()' | ||
| } | ||
|
|
||
| await esbuild.build({ |
There was a problem hiding this comment.
How about displaying a message like "Generated the optimized file into dist" using something like console.log?
There was a problem hiding this comment.
Displaying the router name and the file size is super cool!!
And it's a matter of taste, but shall we go without using emojis? I've never used it in this Hono CLI project. So how about the following:
Diff:
diff --git a/src/commands/optimize/index.ts b/src/commands/optimize/index.ts
index 7262cd4..8a8dd04 100644
--- a/src/commands/optimize/index.ts
+++ b/src/commands/optimize/index.ts
@@ -71,7 +71,8 @@ export function optimizeCommand(program: Command) {
assignRouterStatement = 'this.router = new TrieRouter()'
}
- console.log(`⚡️Router: ${routerName}`)
+ console.log('[Optimized]')
+ console.log(` Router: ${routerName}`)
const outfile = resolve(process.cwd(), options.outfile)
await esbuild.build({
@@ -127,6 +128,6 @@ export class Hono extends HonoBase {
})
const outfileStat = statSync(outfile)
- console.log(`🔥App: ${options.outfile} (${(outfileStat.size / 1024).toFixed(2)} KB)`)
+ console.log(` Output: ${options.outfile} (${(outfileStat.size / 1024).toFixed(2)} KB)`)
})
}There was a problem hiding this comment.
Thanks for the comment.
Okay, let's do that.
75596ab
|
Cool! Almost are good! The bundle size became really small. I've added some comments. |
9c20eab to
026e426
Compare
Co-authored-by: Yusuke Wada <yusuke@kamawada.com>
|
Thank you so much! Let's go! |

No description provided.