0
0

Node single executable applicationsでnpmパッケージを使う

Posted at

Node single executable applications を使うと簡単にNodeのスクリプトをexe化できる。

しかし、その名の通り、exe化できるのは1つのJavaScriptのみ❗
つまり、 import で自作ライブラリを使うことはもちろん、npmパッケージも使えないのだ❗
なんて限定的な機能なんだ❗クソッ❗

...とまぁ、分かってる人はこの時点で「アホだな〜」と思われてると思う。

そう、解決策は簡単で、バンドラーを使えば良い。
WebPackとか、esbuildとか、そういうやつ。
バンドラーは複数のJavaScriptファイルを1つにまとめてくれるので、
SEAを作るときにはもってこいってわけだね。

っていうか、ドキュメント↓にそう書いてあるんだわ。ちゃんと読もう。

Node.js supports the creation of single executable applications by allowing the injection of a blob prepared by Node.js, which can contain a bundled script, into the node binary.

実践: TypeScriptでSEAのexeを作る

肝心のSEA作るとこまでできてないけど、
esbuildでTypeScriptをバンドルするだけのシンプルなサンプルプロジェクトを作った↓。

(VSCodeでブレークポイントを貼ってデバッグすることもできる。)

↑のサンプルプロジェクトをビルドすると、
サンプルソースをバンドルして dist/index.cjs ができる。

ポイントはesbuildのビルド設定で --platform=node にすること。
あとは --format=cjs かな。
ともかく、CommonJSの形式にしないと今のところSEAを使えないので、ここはマスト。

あとは、SEAのドキュメントに従って、 dist/index.cjs をexe化していけば良い。
全部まとめると↓。

echo '{ "main": "dist/index.cjs", "output": "sea-prep.blob" }' > sea-config.json
node --experimental-sea-config sea-config.json
node -e "require('fs').copyFileSync(process.execPath, 'index.exe')" 
npx postject index.exe NODE_SEA_BLOB sea-prep.blob `
    --sentinel-fuse NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2 

ちなみに、今確認したらWindowsだけ動いた。
Macでは postject するときにエラーを吐いた。
どうも このissue と同じ症状のようだ。残念。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0