カテゴリー
【nodejsシェルアプリ開発】vercel/pkgでCLI版スネークゲームを作ってみる
※ 当ページには【広告/PR】を含む場合があります。
2022/12/23
2024/08/21
pkgプロジェクトの準備
$ node --version
v18.12.1
$ npm --version
9.1.2
$ yarn --version
1.22.19
package.json
{
"name": "snake_pkg",
"version": "0.0.1",
"description": "To execise to use pkg with Snake Game."
}
index.js
build
pkg
bin
package.json
pkg
$ yarn add pkg -D
pkg
package.json
scripts
pkg
{
"name": "snake_pkg",
"version": "0.0.1",
"description": "To execise to use pkg with Snake Game.",
"bin": "build/index.js",
"scripts": {
"pkg": "pkg"
},
"devDependencies": {
"pkg": "^5.8.0"
}
}
pkg
$ yarn pkg -h
pkg [options] <input>
Options:
-h, --help output usage information
-v, --version output pkg version
-t, --targets comma-separated list of targets (see examples)
-c, --config package.json or any json file with top-level config
--options bake v8 options into executable to run with them on
-o, --output output file name or template for several files
--out-path path to save output one or more executables
-d, --debug show more information during packaging process [off]
-b, --build don't download prebuilt base binaries, build them
--public speed up and disclose the sources of top-level project
--public-packages force specified packages to be considered public
--no-bytecode skip bytecode generation and include source files as plain js
--no-native-build skip native addons build
--no-dict comma-separated list of packages names to ignore dictionaries. Use --no-dict * to disable all dictionaries
-C, --compress [default=None] compression algorithm = Brotli or GZip
Examples:
– Makes executables for Linux, macOS and Windows
$ pkg index.js
– Takes package.json from cwd and follows 'bin' entry
$ pkg .
– Makes executable for particular target machine
$ pkg -t node14-win-arm64 index.js
– Makes executables for target machines of your choice
$ pkg -t node12-linux,node14-linux,node14-win index.js
– Bakes '--expose-gc' and '--max-heap-size=34' into executable
$ pkg --options "expose-gc,max-heap-size=34" index.js
– Consider packageA and packageB to be public
$ pkg --public-packages "packageA,packageB" index.js
– Consider all packages to be public
$ pkg --public-packages "*" index.js
– Bakes '--expose-gc' into executable
$ pkg --options expose-gc index.js
– reduce size of the data packed inside the executable with GZip
$ pkg --compress GZip index.js
pkg
package.json
pkg
{
"name": "snake_pkg",
"version": "0.0.1",
"description": "To execise to use pkg with Snake Game.",
"bin": "build/index.js",
"scripts": {
"pkg": "pkg"
},
"pkg": {
"scripts": "build/**/*.js",
"assets": [
"assets/**/*",
"static/**/*"
],
"targets": ["latest-linux-x64"],
"outputPath": "dist"
}
"devDependencies": {
"pkg": "^5.8.0"
}
}
index.js
pkg
scripts
assets
targets
outputPath
ビルドターゲット
pkg
nodeRange:
(node8)
node10
node12
node14
node16
latest
platform:
alpine
linux
linuxstatic
win
macos
(freebsd)
arch:
x86
x64
arm64
pkg
[nodeRange]-[platform]-[arch]
pkgのビルド
$ tree -L 2 -I node_modules
.
├── build
│ └── index.js
├── package.json
└── yarn.lock
package.json
$ yarn pkg .
dist
package.json
$ tree -L 2 -I node_modules
.
├── build
│ └── index.js
├── dist
│ └── snake_pkg #👈バイナリファイル
├── package.json
└── yarn.lock
pkgでビルドしたバイナリファイルの実行
nexeバイナリとの比較
$ du -sSk snake_nexe
72392 snake_nexe
72.4MB
linux-x64-14.15.3
$ du -sSk snake_pkg
45220 snake_pkg
45.2MB
v18.5.0-linux-x64
参考サイト
記事を書いた人
ナンデモ系エンジニア
主にAngularでフロントエンド開発することが多いです。 開発環境はLinuxメインで進めているので、シェルコマンドも多用しております。 コツコツとプログラミングするのが好きな人間です。
カテゴリー