pkg的使用

最近想做一個自動處理的流程工具,由于是要在window下能執(zhí)行的命令,之前是想用window 下的shell來完成,由于window shell 不是很方便處理多流程的事情, 想用nodejs腳本來進行,可是nodejs又依賴node包就顯示的很麻煩,有沒有不用安裝其他包能直接運行的腳本呢, 答案是有的,可以將nodejs代碼打包成可執(zhí)行的文件,就可以了, 下面是打包工具的使用

將Node.js打包為可執(zhí)行文件的工具有pkg、nexe、node-packer、enclose等

pkg的打包原理簡單來說,就是將js代碼以及相關(guān)的資源文件打包到可執(zhí)行文件中,然后劫持fs里面的一些函數(shù),使它能夠讀到可執(zhí)行文件中的代碼和資源文件。例如,原來的require(’./a.js’)會被劫持到一個虛擬目錄require(’/snapshot/a.js’)。

全局下載

npm install pkg -g

pkg使用比較簡單,執(zhí)行下

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

-t,–目標(biāo)逗號分隔的目標(biāo)列表,指定打包的目標(biāo)平臺和Node版本,如-t node6-win-x64,node6-linux-x64,node6-macos-x64可以同時打包3個平臺的可執(zhí)行程序

-c,–config package.json 或任何具有頂級配置的json文件,指定一個JSON配置文件,用來指定需要額外打包腳本和資源文件,通常使用package.json配置。

–option 將V8選項烘焙到可執(zhí)行文件中以在其上運行

-o,–指定輸出可執(zhí)行文件的名稱,但如果用-t指定了多個目標(biāo),那么就要用–out-path指定輸出的目錄;

----out-path 輸出一個或多個可執(zhí)行文件的輸出路徑

-d,–debug 在打包過程中顯示更多信息[off]

-build 不要下載預(yù)構(gòu)建的基本二進制文件,構(gòu)建它們。

–public 加速并披露頂級項目的來源

執(zhí)行pkg 的時候會在~/.pkg-cache 找對應(yīng)的pkg-fetch包,如果找不到會自動去下載
下載地址
由于是下載倉庫在國外,下載比較緩慢
可以通過github的國內(nèi)鏡像地址去下載
https://github.com 換成 https://hub.fastgit.xyz/
例如:https://github.com/vercel/pkg-fetch/releases/download/v3.4/node-v16.16.0-win-arm64
換成:
https://hub.fastgit.xyz/vercel/pkg-fetch/releases/download/v3.4/node-v16.16.0-win-arm64
這樣下載就比較快了

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容