Add minify-node-modules script to reduce memory usage #16904
+120
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What
Adds
pnpm minify-node-modulesscript that uses esbuild to minify JavaScript files innode_modules/.pnpmat build time.minifyWhitespace: trueto strip comments and whitespaceminifyIdentifiers: false) for reflection-based code compatibility (NestJS decorators, etc.)MISSKEY_MINIFY_NODE_MODULESenv varfollowSymbolicLinks: falseWhy
V8 keeps script source code in memory. On inspection, this consumes ~100MB for unminified node_modules code. Minifying removes comments and whitespace, reducing memory footprint without bundling (which causes dynamic require issues).
Additional info (optional)
Checklist
Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
download.cypress.io/home/REDACTED/.nvm/versions/node/v22.15.0/bin/node node dist/index.js --exec install(dns block)/home/REDACTED/.nvm/versions/node/v22.15.0/bin/node node dist/index.js --exec install sh -c packages" && echo node_modules/.pnpm/node_modules/@storybook/addon-docs aws-sdk+middleware-h�� readlink -f r/work/misskey/misskey/packages" && echo node_module�� ckages" && echo node�� sh home/REDACTED/work/misskey/misskey/packages" && echo node_modules/.pnpm/node_modules/m�� /misskey/misskey/packages" && echo node_modules/.pnp��(dns block)/home/REDACTED/.nvm/versions/node/v22.15.0/bin/node node dist/index.js --exec install sh -c ey/packages" && echo node_modules/.pnpm/node_modules/@storybook/source-loader ages" && echo node_m�� sh -c readlink -f node_modules/.pnpm/@storybook+addon-esse�� ackages" && echo nod�� sh k/misskey/misskey/packages" && echo node_modules/.pnpm/node_modules/p-queue /misskey/misskey/packages" && echo node_modules/.pnp��(dns block)iojs.org/usr/bin/curl curl -q --fail --compressed -L -s REDACTED -o -(dns block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
This section details on the original issue you should resolve
<issue_title>バックエンドのコードもbundle+minifyする</issue_title>
<issue_description>### Summary
インスペクタで確認したところ、V8はコメントなども全て含むスクリプトコードそのものもメモリに載せてるっぽく、手元の検証ではそれだけで100MB近くメモリを消費している
そのため、(node_modules内のコードも含め)バックエンドのコードもminifyすればメモリ使用量を大幅に減らせる可能性がある
Purpose
メモリ使用量削減
Do you want to implement this feature yourself?
<agent_instructions>バンドルはせずに、ビルド時にnode_modules内のコードをesbuildでminifyするように改修してください。</agent_instructions>
Comments on the Issue (you are @copilot in this section)
@samunohito (本番稼働時、例外発生時のスタックトレースをログに吐いてる箇所がどうなるか(難読化されて追いにくくならないか)がちょっと心配です) @syuilo 難読化ではなくminifyだから読みにくくはなるけど普通に追うことは可能だわね @syuilo あれだったらソースマップ生成しても良いとは思う @samunohito .swcrcのminifyをtrueにして試してみたんですが、変数名・クラス名を短縮するまではされてなくて、改行スペースを除いて1行にするだけみたいでした。それなら…まあ @syuilo ソースマップなしでminifyはちょっとと思われた理由って何でしょう? @samunohito ちなみに、ソースマップはインライン(ファイル末尾にエンコードして埋め込み)で既についてそうです @syuilo Misskeyで発生してキャッチされなかったJavaScriptエラーは、そのMisskeyサーバーの運営者が見て対処してもらうものではなくて、(そのまま)Issueなどで報告してもらうことを想定しています なのでサーバー運営者にとってキャッチされなかったJavaScriptのエラーが読みやすいかどうか(変数名などがそのまま見えるかどうか)は重要でないと思ってます。 (仮に運営者がスタックトレースを見て何らかの理解を行うとしても、minifyされていたからといって調査が困難になるかというとそんなことはないと思います。スタックトレースを理解できるほどの運営者であれば尚更) @syuilo エラーが報告された側の立場(我々)からしても、少なくとも自分はスタックトレースがminifyされていたからといって調査が困難になるということはないので大丈夫と思います @syuilo (フロントエンドはすでにminifyされて提供されていますが、特にエラーの調査に支障をきたしているということも感じてないです) @syuilo スクリプトそのままメモリに載せているというのはやっぱり事実っぽい https://stackoverflow.com/a/78796916 @syuilo (とはいえdevelopment環境などでminifyしないオプションは欲しい) @syuilo 多分バンドルも同時に行わないとnode_modulesのコードをminifyはできないわね @syuilo swc単体ではバンドルできない?こんな感じに設定することでバンドルは成功した
ただ起動すると
Dynamic require of "util" is not supported
エラーがでる</comment_new>
</comment_new>
<comment_new>@syuilo
<comment_new>@syuilo
sentry全体をバンドルから除外したら解決した</comment_new>
<comment_new>@syuilo
今度は
<comment_new>@syuilo
systeminformationをバンドルから除外したら解決した</comment_new>
<comment_new>@syuilo
今度は
<comment_new>@syuilo
nestjs全体をバンドルから除外したら解決した</comment_new>
<comment_new>@syuilo
今度は
<comment_new>@syuilo
なんか結局ほぼバンドルできないみたいなことにならない?</comment_new>
<comment_new>@syuilo
なんか設定でDynamic requireをいい感じにしてくれる設定あったりするのかしら</comment_new>
<comment_new>@syuilo
TASUKETE</comment_new>
<comment_new>@syuilo
とりあえずバンドルが可能(起動時にエラーがでる)な最小の設定をコミット
https://github.com/misskey-dev/misskey/tree/minify-backend</comment_new>
<comment_new>@syuilo
(最悪別にバンドルしなくても個々のnode_modules内のコードをminifyできさえすればまあ良いんだけど)</comment_new>
<comment_new>@syuilo
banner設定で互換のためのコード注入したらDynamic require関連のエラーは出なくなった
https://zenn.dev/junkor/articles/2bcd22ca08d21d</comment...
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.