使用nw-builder构建跨平台桌面应用程序

NW.js应用自动打包的两种方式:

nwjs-builder-phoenix(推荐)
nw-builder

本文主要讲述使用nw-builder构建跨平台桌面应用程序,对NW.js项目进行自动化打包。

nw-builder打包与nwjs-builder-phoenix基本相同,只是依赖和最终build打包命令不同而已。

具体步骤如下:
(1)创建firstapp应用;
(2)修改package.json打包配置;
(3)安装nw-builder依赖、运行及打包;

通过nwbuild ./**/* --platforms win32,win64 --mode=build命令来实现不同平台的打包。

1、创建firstapp应用

1.1执行npm init初始化应用

D:\whw\study\nwjs\firstapp>npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help init` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (firstapp)
version: (1.0.0)
description: first Nw.js Demo
entry point: (index.js) index.html
test command:
git repository:
keywords:
author: whw
license: (ISC)
About to write to D:\whw\study\nwjs\firstapp\package.json:

{
  "name": "firstapp",
  "version": "1.0.0",
  "description": "first Nw.js Demo",
  "main": "index.html",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "whw",
  "license": "ISC"
}


Is this OK? (yes) yes

D:\whw\study\nwjs\firstapp>

1.2创建index.html文件

文件内容如下:

<!DOCTYPE html>
<html>
  <head>
    <title>Hello World!</title>
  </head>
  <body>
    <h1>Hello World!</h1>
  </body>
</html>

2、修改package.json打包脚本

新增以下依赖及构建配置:

"scripts": {
    "dev": "nw .",
    "build": "nwbuild ./**/* --platforms win32,win64 --mode=build"
},
"devDependencies": {
    "nw": "^0.18.3",
    "nw-builder": "^3.1.2"
}

完整的package.json内容如下所示:

{
    "name": "helloworld",
    "main": "index.html",
    "version": "0.0.1",
    "description": "first Nw.js Demo",
    "scripts": {
        "dev": "nw .",
        "build": "nwbuild ./**/* --platforms win32,win64 --mode=build"
    },
    "devDependencies": {
        "nw": "^0.18.3",
        "nw-builder": "^3.1.2"
    },
    "author": "",
    "license": "ISC"
}

3、安装依赖、运行及打包

安装应用依赖:npm i 或 npm install

运行NW.js应用:npm run dev

如果能正常打开如下窗口,则说明NW.js应用已成功运行。

nwjs-03.pngnwjs-03.png

打包NW.js应用:npm run build

打包日志如下:

D:\whw\study\nwjs\firstapp2>npm run build

> helloworld@0.0.1 build
> nwbuild ./**/* --platforms win32,win64 --buildDir dist/ --mode=build


D:\whw\study\nwjs\firstapp2>

打包完成之后,查看根目录下dist目录,已生成对应系统版本的安装包,如下图示:

nwjs-05.pngnwjs-05.png

进入系统目录,双击helloworld.exe即可查看运行效果。

至此打nw-builder打包即完成。

参考:
nw-builder
https://nwutils.io/nw-builder/getting-started

nwbuild命令参数说明:

D:\whw\study\nwjs\test2>nwbuild --help
nwbuild.cjs [command]

Commands:
  nwbuild.cjs [file(glob)] [options]

Run API
  --version    Version of NW.js you want to use.    [string] [default: "latest"]
  --flavor     sdk is recommended for development and normal is recommended for
               production.                             [string] [default: "sdk"]
  --cacheDir   Path to NW.js cache                 [string] [default: "./cache"]
  --platforms  Supported platforms are linux32, linux64, osx32, osx64, win32,
               win64                                  [array] [default: "win64"]

Build API
  --appName           Name of your application         [string] [default: false]
  --appVersion        Version of your application      [string] [default: false]
  --buildDir          Path to NW.js build          [string] [default: "./build"]
  --buildType         default is appName,
                      versioned is [appName] -v[appVersion],
                      timestamped is [appName] - [timestamp]
                                                   [string] [default: "default"]
  --forceDownload     Delete all cache and builds and redownload them
                                                      [boolean] [default: false]
  --macCredits        The path to your credits.html file. By default it uses the
                      one provided by NW.js            [string] [default: false]
  --macIcns           The path to your ICNS file. By default it uses the one
                      provided by NW.js                [string] [default: false]
  --macPlist          The path to your Plist file. By default a Plist file is
                      generated from the package.json  [string] [default: false]
  --winVersionString  Descriptors for Windows executable           [default: {}]
  --winIco            Path to your ICO file. By default it uses the one provided
                      by NW.js                          [string] [default: null]
  --useRcedit         If set to true, it allows you to set the Windows icon
                      using rcedit instead of winresourcer
                                                       [string] [default: false]

Package API
  --zip         Zip your NW.js application             [boolean] [default: null]
  --zipOptions  Configure the underling zip library parameters, like store or
                compression ratio                      [boolean] [default: null]
  --mergeZip    Merge the source file package with the NW.js executable
                                                       [boolean] [default: true]

Options:
  --help   Show help                                                   [boolean]
  --mode   Choose between run and build mode           [string] [default: "run"]
  --quiet  Choose your level of logging between error, warn, info, debug and off
                                                      [string] [default: "info"]

(完)

最后修改于:2022年09月24日 10:46

添加新评论