Electron 打包準則 - Arch Linux 中文维基

跳至內容

# Electron 打包準則

出自 Arch Linux 中文维基
Arch 打包準則

32 位 – 安全 – CLR – CMake – DKMS – Eclipse – Electron – Free Pascal – GNOME – Go – Haskell – Java – 交叉編譯工具 – KDE – Lisp – Meson – MinGW – 內核模塊 – Node.js – Nonfree – OCaml – Perl – PHP – Python – R – Ruby – Rust – Shell – VCS – Web – Wine – 字體

翻譯狀態：

- 本文（或部分內容）譯自 Electron package guidelines，最近一次同步於 2025-06-29，若英文版本有所更改，則您可以幫助同步與翻譯更改的內容。
- 您可以在 ArchWiki 的對應頁面找到本文翻譯的原始修訂歷史。
- 本文可能與英文原文存在出入。

本文或本節需要翻譯。要貢獻翻譯，請訪問簡體中文翻譯團隊。

附註： 全文需要翻譯（在 Talk:Electron 打包準則# 中討論）

This document covers standards and guidelines on writing PKGBUILDs for Electron.

## Using the system electron

Arch Linux provides versioned electron* packages and an electron包 metapackage for the latest version. They can be used to run an electron application via a shell script wrapper:

    #!/bin/sh

    exec /usr/bin/electron34 /path/to/appname/ "$@"

The `appname/` directory, or alternatively a file bundle called `appname.asar`, can be found in a prebuilt electron application as the `resources/app/` folder (or `resources/app.asar`). Everything else is just a copy of the electron runtime and can be removed from the final package.

注意：Applications that require `ELECTRON_RUN_AS_NODE=1`, e.g. Visual Studio Code, cannot use `/usr/bin/electron*`. See code.sh.

### Editing version on package.json

It is dangerous to edit version of Electron in `package.json` or `package-lock.json` using `sed`. You can use `npm pkg set devDependencies.electron=$(cat /usr/lib/electron*/version)` instead.

本文或本章節的事實準確性存在爭議。

原因： Editing package.json may not needed and unrelated to building modules correctly.（在 Talk:Electron 打包準則 中討論）

### Building compiled extensions against the system electron

Some electron applications have compiled native extensions which link to the electron runtime, and must be built using the correct electron version. Since npm/yarn will always build against a private prebuilt copy of electron, patch the electron dependency from `package.json` to reference the same version as the system electron dependency. The build system will download the prebuilt copy it requires, compile the native extensions, and package everything into a final distribution, but this can be pruned during the `package()` step as usual.

Alternatively, you can remove the electron dependency from `package.json` and set the correct environment variables before running npm:

    export npm_config_target=$(tail /usr/lib/electron/version)
    export npm_config_arch=x64
    export npm_config_target_arch=x64
    export npm_config_disturl=https://electronjs.org/headers[失效連結 2023-10-29 ⓘ]
    export npm_config_runtime=electron
    export npm_config_build_from_source=true
    HOME="$srcdir/.electron-gyp" npm install

Set `HOME` to a path inside the `$srcdir` so the build process does not place any files in your real `HOME` directory. Make sure to adjust the path for all further commands that make use of the `.electron-gyp` cache.

(more details in Electron docs).

### Using electron-builder with system electron

Many projects use electron-builder to build and package the Javascript file and Electron binaries. By default electron-builder downloads the entire electron version that is defined in the package management file (e.g. `package.json`). This might not be desired if you want to use the system electron and save the bandwidth since you are going to throw away the electron binaries anyway. The electron-builder provides the configurations `electronDist` and `electronVersion`, to specify a custom path of Electron and the version the application is packaged for respectively.

Find the electron-builder configuration file (e.g. `electron-builder.json`) and add the following settings:

- `electronDist` to`/usr/lib/electron34` for electron34包
- `electronVersion` to the contents of `/usr/lib/electron34/version` (without the leading `v` if exists)

Packages that apply this: rocketchat-desktopAUR ubports-installer-gitAUR

electron-builder configuration[失效連結 2024-10-12 ⓘ]

Alternatively you can use the CLI to change/add these settings like this:

    ./node_modules/.bin/electron-builder --linux --x64 --dir $dist -c.electronDist=$electronDist -c.electronVersion=$electronVer

Note that you have to specify all these options or it will not work.

本文或本章節的事實準確性存在爭議。

原因： Current setting still copies Electron from $electronDist . It is expired to add the method to stop it. （在 Talk:Electron 打包準則 中討論）

## Architecture

See PKGBUILD#arch.

An Electron package that contains compiled native extensions is architecture-dependent. Otherwise it is most likely architecture-independent.

If the package contains a prebuilt copy of electron, it is always architecture-dependent.

## Directory structure

If the package is architecture-dependent, install the `resources/app/` directory to `/usr/lib/appname/`. Otherwise use `/usr/share/appname/`.

If the package contains a prebuilt copy of electron, copy the final distribution in its entirety to `/opt/appname`.

## Getting version of Electron

Version of Electron could be given by `npm pkg get devDependencies.electron` in the directory containing `package.json` or `package-lock.json`.

Prebuild or Nonfree application hides version of electron from `package.json`, `package-lock.json`, and `version` files. In such case, you may get version by replacing `app` or `app.asar` with `/usr/lib/electron/resources/default_app.asar` and running `pathto/electron-binary --version`.

注意：Avoid doing it at PKGBUILD. This should be last resort to make packages with system-wide electron.
分類：​

- Arch 打包準則
- 包含失效連結的頁面

隱藏分類：​

- 使用「模板:Translateme」標記的頁面或章節
- 使用「模板:準確性」標記的頁面或章節

Electron 打包準則
新增話題