跳到内容

通用配置

electron-builder 配置 可以定义

  • 在项目的 package.json 文件中使用顶层 build
    "build": {
      "appId": "com.example.app"
    }
    
  • 或通过 --config <path/to/yml-or-json5-or-toml-or-js> 选项。默认为 electron-builder.yml

    appId: "com.example.app"
    

    也支持 jsonjson5tomljs/ts(导出的配置或生成配置的函数)格式。

    提示

    如果您想使用 js 文件,请不要将其命名为 electron-builder.js。它将与 electron-builder 包名冲突

    提示

    如果您想使用 toml,请安装 yarn add toml --dev

大多数选项接受 null — 例如,要显式设置 DMG 图标必须是来自操作系统的默认卷图标,并且不应用默认规则(即使用应用程序图标作为 DMG 图标),请将 dmg.icon 设置为 null

产物文件名模板

除了 文件宏 之外,还支持 ${ext} 宏。

来自文件的环境变量

当前目录中的 Env 文件 electron-builder.env示例)。仅支持 CLI 用法。

如何阅读文档

  • 可选属性的名称是正常的,必需的是粗体。
  • 类型在属性名称后指定:Array<String> | String。这样的联合表示您可以指定字符串 (**/*) 或字符串数组 (["**/*", "!foo.js"])。

通用配置

Electron-Builder / app-builder-lib / CommonConfiguration

配置选项

扩展自
属性
apk?

readonly optional apk: null | LinuxTargetSpecificOptions


appId?

readonly optional appId: null | string

应用程序 ID。在 MacOS 中用作 CFBundleIdentifier,在 Windows 中用作 应用程序用户模型 ID(仅限 NSIS 目标,不支持 Squirrel.Windows)。强烈建议设置显式 ID。

# 默认
com.electron.${name}

appImage?

readonly optional appImage: null | AppImageOptions

AppImage 选项。


appx?

readonly optional appx: null | AppXOptions


buildDependenciesFromSource?

optional buildDependenciesFromSource: boolean

是否从源代码构建应用程序原生依赖项。

# 默认
false

buildNumber?

readonly optional buildNumber: null | string

构建编号。映射到 Linux 上使用 FPM 构建的 --iteration 标志。如果未定义,则会回退到 BUILD_NUMBERTRAVIS_BUILD_NUMBERAPPVEYOR_BUILD_NUMBERCIRCLE_BUILD_NUMBUILD_BUILDNUMBERCI_PIPELINE_IID 环境变量。


buildVersion?

readonly optional buildVersion: null | string

构建版本。映射到 macOS 上的 CFBundleVersion 和 Windows 上的 FileVersion 元数据属性。默认为 version。如果未定义 buildVersion 且定义了 buildNumber(或其中一个 buildNumber 环境变量),它将用作构建版本 (version.buildNumber)。


readonly optional copyright: null | string

应用程序的人类可读的版权行。

# 默认
Copyright © year ${author}

deb?

readonly optional deb: null | DebOptions

Debian 包选项。


directories?

readonly optional directories: null | MetadataDirectories

构建资源的目录


dmg?

readonly optional dmg: null | DmgOptions

macOS DMG 选项。


downloadAlternateFFmpeg?

readonly optional downloadAlternateFFmpeg: boolean

是否从 Electron 的发布资产下载备用 FFmpeg 库,并在签名之前替换默认的 FFmpeg 库


electronFuses?

readonly optional electronFuses: null | FuseOptionsV1

传递给 @electron/fuses 的选项 参考:https://github.com/electron/fuses


extraMetadata?

readonly optional extraMetadata: any

将属性注入到 package.json 中。


flatpak?

readonly optional flatpak: null | FlatpakOptions

Flatpak 选项。


forceCodeSigning?

readonly optional forceCodeSigning: boolean

如果应用程序未签名是否失败(以防止在代码签名配置不正确时出现未签名的应用程序)。

# 默认
false

freebsd?

readonly optional freebsd: null | LinuxTargetSpecificOptions


includePdb?

readonly optional includePdb: boolean

是否包含 PDB 文件。

# 默认
false

linux?

readonly optional linux: null | LinuxConfiguration

与如何构建 Linux 目标相关的选项。


mac?

readonly optional mac: null | MacConfiguration

与如何构建 macOS 目标相关的选项。


mas?

readonly optional mas: null | MasConfiguration

MAS (Mac Application Store) 选项。


masDev?

readonly optional masDev: null | MasConfiguration

MAS (Mac Application Store) 开发选项 (mas-dev 目标)。


nativeRebuilder?

readonly optional nativeRebuilder: null | "legacy" | "sequential" | "parallel"

使用 legacy app-builder 二进制文件安装原生依赖项,或在 sequentialparallel 编译模式下使用 @electron/rebuild

# 默认
sequential

nodeGypRebuild?

readonly optional nodeGypRebuild: boolean

是否在开始打包应用程序之前执行 node-gyp rebuild

不要使用 npm(也不要使用 .npmrc)来配置 electron 标头。请改用 electron-builder node-gyp-rebuild

# 默认
false

npmArgs?

readonly optional npmArgs: null | string | string[]

安装应用程序原生依赖项时要使用的其他命令行参数。


npmRebuild?

readonly optional npmRebuild: boolean

是否在开始打包应用程序之前重建原生依赖项。

# 默认
true

nsis?

readonly optional nsis: null | NsisOptions


nsisWeb?

readonly optional nsisWeb: null | NsisWebOptions


p5p?

readonly optional p5p: null | LinuxTargetSpecificOptions


pacman?

readonly optional pacman: null | LinuxTargetSpecificOptions


pkg?

readonly optional pkg: null | PkgOptions

macOS PKG 选项。


portable?

readonly optional portable: null | PortableOptions


productName?

readonly optional productName: null | string

name 相同,但允许您为可执行文件指定产品名称,其中包含 name 属性中不允许的空格和其他特殊字符。如果未在 build 配置中指定,则使用 package.json 顶层定义的 productName 属性。如果未在 package.json 顶层指定,则使用 name 属性


removePackageKeywords?

readonly optional removePackageKeywords: boolean

是否从 package.json 文件中删除 keywords 字段。

# 默认
true

removePackageScripts?

readonly optional removePackageScripts: boolean

是否从 package.json 文件中删除 scripts 字段。

# 默认
true

rpm?

readonly optional rpm: null | LinuxTargetSpecificOptions


snap?

readonly optional snap: null | SnapOptions

Snap 选项。


squirrelWindows?

readonly optional squirrelWindows: null | SquirrelWindowsOptions


win?

readonly optional win: null | WindowsConfiguration

与如何构建 Windows 目标相关的选项。


可按平台覆盖的选项

以下选项也可以按平台设置(顶层键 maclinuxwin),如果需要。

基本配置

Electron-Builder / app-builder-lib / PlatformSpecificBuildOptions

扩展

扩展自

属性

appId?

readonly optional appId: null | string

应用程序 ID。在 MacOS 中用作 CFBundleIdentifier,在 Windows 中用作 应用程序用户模型 ID(仅限 NSIS 目标,不支持 Squirrel.Windows)。强烈建议设置显式 ID。

默认
com.electron.${name}

artifactName?

readonly optional artifactName: null | string

产物文件名模板。默认为 ${productName}-${version}.${ext}(某些目标可能具有其他默认值,请参阅相应的选项)。

覆盖

TargetSpecificOptions.artifactName


asar?

readonly optional asar: null | boolean | AsarOptions

是否使用 Electron 的存档格式 将应用程序的源代码打包到存档中。

必须解包的 Node 模块将自动检测,您无需显式设置 asarUnpack - 如果这不起作用,请提交问题。

默认
true

asarUnpack?

readonly optional asarUnpack: null | string | string[]

相对于 应用程序目录glob 模式,指定在创建 asar 存档时要解包的文件。


compression?

readonly optional compression: null | CompressionLevel

压缩级别。如果您想快速测试构建,store 可以显着减少构建时间。maximum 不会导致明显的尺寸差异,但会增加构建时间。

默认
normal

cscKeyPassword?

optional cscKeyPassword: null | string


optional cscLink: null | string


defaultArch?

readonly optional defaultArch: string


detectUpdateChannel?

readonly optional detectUpdateChannel: boolean

是否从应用程序版本预发布组件推断更新频道。例如,如果版本为 0.12.1-alpha.1,频道将设置为 alpha。否则设置为 latest。这适用于 github 发布,它将 永远不会自动检测更新频道

默认
true

disableDefaultIgnoredFiles?

optional disableDefaultIgnoredFiles: null | boolean

是否排除所有默认忽略的文件(https://builder.electron.js.cn/contents#files)和选项。默认为 false

默认
false

electronLanguages?

readonly optional electronLanguages: string | string[]

要保留的 electron 语言环境。默认情况下,所有 Electron 语言环境都按原样使用。


electronUpdaterCompatibility?

readonly optional electronUpdaterCompatibility: null | string

electron-updater 兼容性 semver 范围。


executableName?

readonly optional executableName: null | string

可执行文件名。默认为 productName


extraFiles?

optional extraFiles: null | string | FileSet | (string | FileSet)[]

extraResources 相同,但复制到应用程序的内容目录中(MacOS 为 Contents,Linux 和 Windows 为根目录)。

继承自

FilesBuildOptions.extraFiles


extraResources?

optional extraResources: null | string | FileSet | (string | FileSet)[]

相对于项目目录的 glob 模式,指定后,将匹配名称的文件或目录直接复制到应用程序的资源目录中(MacOS 为 Contents/Resources,Linux 和 Windows 为 resources)。

文件模式(以及对 fromto 字段的支持)与 files 相同。

继承自

FilesBuildOptions.extraResources


fileAssociations?

readonly optional fileAssociations: FileAssociation | FileAssociation[]

文件关联。


files?

optional files: null | string | FileSet | (string | FileSet)[]

相对于 应用程序目录glob 模式,指定在复制文件以创建包时要包含的文件。

默认为

[
"**/*",
"!**/node_modules/*/{CHANGELOG.md,README.md,README,readme.md,readme}",
"!**/node_modules/*/{test,__tests__,tests,powered-test,example,examples}",
"!**/node_modules/*.d.ts",
"!**/node_modules/.bin",
"!**/*.{iml,o,hprof,orig,pyc,pyo,rbc,swp,csproj,sln,xproj}",
"!.editorconfig",
"!**/._*",
"!**/{.DS_Store,.git,.hg,.svn,CVS,RCS,SCCS,.gitignore,.gitattributes}",
"!**/{__pycache__,thumbs.db,.flowconfig,.idea,.vs,.nyc_output}",
"!**/{appveyor.yml,.travis.yml,circle.yml}",
"!**/{npm-debug.log,yarn.lock,.yarn-integrity,.yarn-metadata.json}"
]

在任何情况下都不会复制开发依赖项。您无需显式忽略它。默认情况下不忽略隐藏文件,但默认情况下会忽略所有应忽略的文件。

如果您的某些模式不是忽略模式(即不以 ! 开头),则不会将默认模式 **/* 添加到您的自定义模式中。在任何情况下,package.json**/node_modules/**/*(仅复制生产依赖项)都会添加到您的自定义模式中。在任何情况下都会添加所有默认忽略项 — 如果您配置了自己的模式,则无需重复。

可以在平台选项中指定(例如在 mac 中)。

您还可以通过使用 FileSet 对象而不是简单的 glob 模式来指定自定义源目录和目标目录。

[
{
 "from": "path/to/source",
 "to": "path/to/destination",
 "filter": ["**/*", "!foo/*.js"]
}
]

您可以在 fromto 字段中使用 文件宏fromto 可以是文件,您可以使用它在打包时重命名文件。

继承自

FilesBuildOptions.files


forceCodeSigning?

readonly optional forceCodeSigning: boolean

如果应用程序未进行代码签名是否失败。


generateUpdatesFilesForAllChannels?

readonly optional generateUpdatesFilesForAllChannels: boolean

请参阅 使用频道构建和发布

默认
false

icon?

readonly optional icon: null | string


protocols?

readonly optional protocols: Protocol | Protocol[]

URL 协议方案。


publish?

optional publish: Publish

发布者配置。有关更多信息,请参阅 自动更新

覆盖

TargetSpecificOptions.publish


releaseInfo?

readonly optional releaseInfo: ReleaseInfo

发布信息。旨在用于命令行

-c.releaseInfo.releaseNotes="new features"

target?

readonly optional target: null | string | TargetConfiguration | (string | TargetConfiguration)[]

元数据

一些标准字段应在 package.json 中定义。

Electron-Builder / app-builder-lib / Metadata

属性

author?

readonly optional author: null | AuthorMetadata


build?

readonly optional build: Configuration

electron-builder 配置。


description?

readonly optional description: string

应用程序描述。


homepage?

readonly optional homepage: null | string

项目 主页 的 URL(NuGet 包 projectUrl(可选)或 Linux 包 URL(必需))。

如果未指定,并且您的项目存储库在 GitHub 上是公开的,则默认情况下它将是 https://github.com/${user}/${project}


license?

readonly optional license: null | string

仅限 linux。 许可证名称。


name?

readonly optional name: string

应用程序名称。

必需

repository?

readonly optional repository: null | string | RepositoryInfo

存储库

Proton Native

要打包 Proton Native 应用程序,请将 protonNodeVersion 选项设置为 current 或您要打包的特定 NodeJS 版本。目前,仅支持 macOS 和 Linux。

构建版本管理

CFBundleVersion (macOS) 和 FileVersion (Windows) 将在 CI 服务器(支持 Travis、AppVeyor、CircleCI 和 Bamboo)上自动设置为 version.build_number

构建钩子

#### 钩子

Node.js 8

所有示例都假定您使用最新的 Node.js 8.11.x 或更高版本。

Electron-Builder / app-builder-lib / Hooks

扩展自
属性
# afterAllArtifactBuild?

readonly optional afterAllArtifactBuild: null | string | Hook<BuildResult, string[]>

在构建所有产物后要运行的函数(或文件路径或模块 ID)。

(buildResult: BuildResult): Promise<Array<string>> | Array<string>

配置方式与 afterPack 相同(见上文)。

myAfterAllArtifactBuild.js

exports.default = function () {
  // you can return additional files to publish
  return ["/path/to/additional/result/file"]
}

# afterExtract?

readonly optional afterExtract: null | string | Hook<PackContext, void>

预构建的 Electron 二进制文件已解压缩到输出目录后要运行的函数(或文件路径或模块 ID)。设置与 beforePack 相同


# afterPack?

readonly optional afterPack: null | string | Hook<PackContext, void>

打包后(但在打包成可分发格式和签名之前)要运行的函数(或文件路径或模块 ID)。设置与 beforePack 相同


# afterSign?

readonly optional afterSign: null | string | Hook<PackContext, void>

打包和签名后(但在打包成可分发格式之前)要运行的函数(或文件路径或模块 ID)。与 beforePack 相同的设置


# appxManifestCreated?

readonly optional appxManifestCreated: null | string | Hook<string, void>

在磁盘上创建 Appx 清单后要运行的函数(或文件路径或模块 ID) - 尚未打包到 .appx 包中。


# artifactBuildCompleted?

readonly optional artifactBuildCompleted: null | string | Hook<ArtifactCreated, void>

在制品构建完成时要运行的函数(或文件路径或模块 ID)。与 beforePack 相同的设置


# artifactBuildStarted?

readonly optional artifactBuildStarted: null | string | Hook<ArtifactBuildStarted, void>

在制品构建开始时要运行的函数(或文件路径或模块 ID)。与 beforePack 相同的设置


# beforeBuild?

readonly optional beforeBuild: null | string | Hook<BeforeBuildContext, boolean | void>

在安装或重建依赖之前要运行的函数(或文件路径或模块 ID)。当 npmRebuild 设置为 true 时工作。解析为 false 将跳过依赖安装或重建。

如果提供且 node_modules 缺失,则不会调用生产依赖检查。


# beforePack?

readonly optional beforePack: null | string | Hook<PackContext, void>

在打包之前要运行的函数(或文件路径或模块 ID)。

(context: BeforePackContext): Promise<any> | any

作为函数

beforePack: async (context) => {
  // your code
}

因为在配置文件中不能使用 JavaScript,所以可以指定为文件路径或模块 ID。函数必须作为默认导出导出。

"build": {
  "beforePack": "./myBeforePackHook.js"
}

项目根目录下的文件 myBeforePackHook.js

myBeforePackHook.js

exports.default = async function(context) {
  // your custom code
}

# electronDist?

readonly optional electronDist: null | string | Hook<PrepareApplicationStageDirectoryOptions, string>

在暂存 electron 制品环境时要运行的函数(或文件路径或模块 ID)。返回自定义 Electron 构建的路径(例如 ~/electron/out/R)或 Electron zip 文件的文件夹。

Zip 文件必须遵循 electron-v${version}-${platformName}-${arch}.zip 模式,否则将被假定为解压后的 Electron 应用程序目录


# msiProjectCreated?

readonly optional msiProjectCreated: null | string | Hook<string, void>

在磁盘上创建 MSI 项目后要运行的函数(或文件路径或模块 ID) - 尚未打包到 .msi 包中。


# onNodeModuleFile?

readonly optional onNodeModuleFile: null | string | Hook<string, boolean | void>

在每个 node 模块文件上要运行的函数(或文件路径或模块 ID)。返回 true/false 将决定是否强制包含或使用默认复制逻辑