Skip to content

Hono のクライアントの補完を高速化#19

Merged
harsssh merged 3 commits intomainfrom
perf-hono-client
Nov 27, 2024
Merged

Hono のクライアントの補完を高速化#19
harsssh merged 3 commits intomainfrom
perf-hono-client

Conversation

@harsssh
Copy link
Copy Markdown
Collaborator

@harsssh harsssh commented Nov 21, 2024

Hono のドキュメントにもあるとおり、型を事前にコンパイルするとクライアントの補完が早くなる。
TypeScript の Project References を使う方法を採用した。

https://hono.dev/docs/guides/rpc#compile-your-code-before-using-it-recommended

設定は面倒ですが、これくらい爆速になります。

fast-hono.mov

詳細はコメントで補足します。

@harsssh harsssh requested a review from smatsuodev November 21, 2024 18:21
Comment thread package.json
"lint": "biome check",
"lint:fix": "biome check --fix"
"lint:fix": "biome check --fix",
"watch:server-types": "tsc -b -w ./src/server"
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ソースコードを更新したら再コンパイルが必要なので、tsc --watch で更新が必要です

Comment thread src/server/tsconfig.json
Comment on lines +5 to +6
"composite": true,
"declaration": true,
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread src/server/tsconfig.json
"declarationMap": true,
"emitDeclarationOnly": true,
"outDir": "./types",
"rootDir": "../"
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

サーバーのコードが src/db など、親ディレクトリのものを参照するので必要です

Comment thread tsconfig.base.json
"paths": {
"@/*": ["./src/*"]
},
"skipLibCheck": true,
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

依存パッケージで型エラーが起きたりするので、チェックを無効化

Comment thread tsconfig.json
},
"noUncheckedIndexedAccess": true
}
"disableSourceOfProjectReferenceRedirect": true
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

project reference とこのオプションを有効化すると、型情報はコンパイルした *.d.ts を参照するようになる。

詳しくは以下の PR のコメントを参照。

microsoft/TypeScript#32028 (comment)

Comment thread package.json
"type": "module",
"scripts": {
"dev": "vite dev",
"dev": "concurrently -r -k yarn:watch:server-types vite",
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-r, --raw は余計な prefix などを付けない、-k, --kill-others はどれかが終了したら全て終了させる。

@harsssh
Copy link
Copy Markdown
Collaborator Author

harsssh commented Nov 25, 2024

concurrently で起動したコマンドが止まらないことがあるかも
後で調査

@harsssh
Copy link
Copy Markdown
Collaborator Author

harsssh commented Nov 25, 2024

concurrently で起動したコマンドが止まらないことがあるかも

問題なさそう

Base automatically changed from hono-split-routes to main November 27, 2024 14:04
Comment thread src/server/tsconfig.json Outdated
"declaration": true,
"declarationMap": true,
"emitDeclarationOnly": true,
"outDir": "./types",
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nits
build/typesの方がより一般的に感じるのですがどうでしょうか?
生成物かつリポジトリの管理下に置かないコードはbuildディレクトリ以下に置いておくことで、.gitignorebuild/**を無視するようにかけたりもするかなと思いました

@harsssh harsssh merged commit 2c40f21 into main Nov 27, 2024
@harsssh harsssh deleted the perf-hono-client branch November 27, 2024 14:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants