天天看點

pkg學習--使用pkg打包應用

pkg的學習之旅 ----使用pkg打包Node.js服務小demo

将Node.js打包為可執行檔案的工具有pkg、nexe、node-packer、enclose等,面對項目需要,是以在學習pkg,看到相關文章,覺得這個很不錯,自己記錄下,以便下次使用便捷。

pkg的打包原理簡單來說,就是将js代碼以及相關的資源檔案打包到可執行檔案中,然後劫持fs裡面的一些函數,使它能夠讀到可執行檔案中的代碼和資源檔案。例如,原來的require(’./a.js’)會被劫持到一個虛拟目錄require(’/snapshot/a.js’)。

安裝

npm install pkg --save-dev
           

使用

pkg使用比較簡單,執行下pkg -h

pkg --help
           

就可以基本了解用法,基本文法是:

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

  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 node6-alpine-x64 index.js
  – Makes executables for target machines of your choice
    $ pkg -t node4-linux,node6-linux,node6-win index.js
  – Bakes '--expose-gc' into executable
    $ pkg --options expose-gc index.js
           

-t,–目标逗号分隔的目标清單,指定打包的目标平台和Node版本,如-t node6-win-x64,node6-linux-x64,node6-macos-x64可以同時打包3個平台的可執行程式

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

–option 将V8選項烘焙到可執行檔案中以在其上運作

-o,–指定輸出可執行檔案的名稱,但如果用-t指定了多個目标,那麼就要用–out-path指定輸出的目錄;

----out-path 輸出一個或多個可執行檔案的輸出路徑

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

-build 不要下載下傳預建構的基本二進制檔案,建構它們。

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

<input>可以通過三種方式指定:

1.一個腳本檔案,例如pkg index.js;
2.package.json,例如pkg package.json,
這時會使用package.json中的bin字段作為入口檔案;
3.一個目錄,例如pkg .,這時會尋找指定目錄下的package.json檔案,
然後在找bin字段作為入口檔案。
           

使用pkg的最佳實踐是:在package.json中的pkg字段中指定打包參數,使用npm scripts來執行打包過程,

{
  ...
  "bin": "./bin/www",
  "scripts": {
    "pkg": "pkg . --out-path=dist/"
  },
  "pkg": {
    "scripts": "build/**/*.js",
    "assets": "views/**/*",
    "targets": [...]
  },
  ...
}
           

scripts和assets用來配置未打包進可執行檔案的腳本和資源檔案,檔案路徑可以使用glob通配符*。

連結,參考執行個體連結

npm官網

執行個體:

使用vue-cli建立項目,并使用npm run build将你的項目編譯生成靜态檔案到dist目錄下。

在項目目錄下建立一個service.js檔案,并添加以下代碼,在本地起一個express靜态伺服器,使你能夠在本地通路你的網站(部署到線上也是類似)

pkg學習--使用pkg打包應用

service.js代碼示例:

//service.js
const express = require('express');
const app = express();
const path = require('path');
 
app.on('ready',function(){
	console.log('進入頁面了')
})

 //注意這裡使用path.join(__dirname, 'dist')而不是'dist'
 // 雖然在指令行中執行起來效果是一樣的,不過pkg打包會無法識别到dist目錄
app.use(express.static(path.join(__dirname, 'dist')));
 

var server = app.listen(8081, function () {
    var host = server.address().address
    var port = server.address().port
    console.log(`AIbuy agents server start successfully on http://${host}:${port}`)
    console.log('打包出來了')
})
           

儲存後,就可以輸入指令行,來啟動伺服器了,啟動完成後,浏覽器通路http://localhost:8081/即可檢視本地網站。

node service.js

           

接下來我們使用将service.js和dist目錄打包成一個exe檔案,友善他人使用

首先全局安裝pkg

npm install pkg -g
           

然後修改package.json,添加bin(如果不是service.js的話)和pkg項

package.json:

pkg學習--使用pkg打包應用

然後在項目目錄下執行

pkg -t win package.json
           

完成後即生成一個exe檔案,輕按兩下啟動即相當于執行node service.js,然後你浏覽器裡(http://localhost:8081/)就能通路打包好的項目了!

見下圖:

pkg學習--使用pkg打包應用

輕按兩下打開這個exe檔案,會出現如下界面:

pkg學習--使用pkg打包應用

然後就可以根據自己的項目需求進行編碼了,這是pkg打包…

繼續閱讀