Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 30, 2025

What

Adds pnpm minify-node-modules script that uses esbuild to minify JavaScript files in node_modules/.pnpm at build time.

  • Uses minifyWhitespace: true to strip comments and whitespace
  • Preserves identifiers (minifyIdentifiers: false) for reflection-based code compatibility (NestJS decorators, etc.)
  • Skips in development mode by default; controllable via MISSKEY_MINIFY_NODE_MODULES env var
  • Excludes workspace packages via followSymbolicLinks: false
  • Processes ~39,000 files in parallel batches

Why

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)

# Production: run after pnpm install
pnpm minify-node-modules

# Force enable in development
MISSKEY_MINIFY_NODE_MODULES=true pnpm minify-node-modules

# Force disable in production
MISSKEY_MINIFY_NODE_MODULES=false pnpm minify-node-modules

Checklist

  • Read the contribution guide
  • Test working in a local environment
  • (If needed) Update CHANGELOG.md
  • (If possible) Add tests

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
    • Triggering command: /home/REDACTED/.nvm/versions/node/v22.15.0/bin/node node dist/index.js --exec install (dns block)
    • Triggering command: /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)
    • Triggering command: /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
    • Triggering command: /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?

  • Yes, I will implement this by myself and send a pull request</issue_description>

<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単体ではバンドルできない? Image @syuilo webpackの情報しか出ない @syuilo esbuild使えそう @syuilo いろいろエラー出る Image @syuilo Image

こんな感じに設定することでバンドルは成功した

ただ起動すると

Dynamic require of "util" is not supported

エラーがでる</comment_new>
<comment_new>@syuilo
Image</comment_new>
<comment_new>@syuilo
sentry全体をバンドルから除外したら解決した</comment_new>
<comment_new>@syuilo
今度は

Image</comment_new>
<comment_new>@syuilo
systeminformationをバンドルから除外したら解決した</comment_new>
<comment_new>@syuilo
今度は

Image</comment_new>
<comment_new>@syuilo
nestjs全体をバンドルから除外したら解決した</comment_new>
<comment_new>@syuilo
今度は

Image</comment_new>
<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.

Copilot AI changed the title [WIP] Minify backend code to reduce memory usage Add minify-node-modules script to reduce memory usage Nov 30, 2025
Copilot AI requested a review from syuilo November 30, 2025 03:13
@syuilo
Copy link
Member

syuilo commented Nov 30, 2025

少なくとも20MBくらいはメモリ使用量減った

@syuilo
Copy link
Member

syuilo commented Nov 30, 2025

でもなんか壊れるのかminify後にpnpm buildすると

import { build } from 'esbuild';                                                                                                                                                                                  
│          ^^^^^                                                                                                                                                                                                    
│ SyntaxError: Named export 'build' not found. The requested module 'esbuild' is a CommonJS module, which may not support all module.exports as named exports.  

が出る

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

バックエンドのコードもbundle+minifyする

2 participants