简体   繁体   English

如何使用电子生成器 package 预编译二进制文件

[英]How to package precompiled binary with electron-builder

I am building an electron application that makes use of ffmpeg-static and I have spent hours searching Stack Overflow to no avail.我正在构建一个使用ffmpeg-static的 electron 应用程序,我花了几个小时搜索 Stack Overflow 无济于事。 I started the application using electron-react-boilerplate .我使用electron-react-boilerplate启动了应用程序。 Here is what the file setup looks like:这是文件设置的样子:

my-app/
├─ .erb/
│  ├─ configs/
│  ├─ . . .
├─ node_modules/
│  ├─ ffmpeg-static/
│  │  ├─ ffmpeg
├─ assets/
│  ├─ icon.png
│  ├─ . . .
├─ src/
│  ├─ RecordingPage.jsx
│  ├─ App.jsx
│  ├─ main.dev.ts
│  ├─ . . .
├─ release/
│  ├─ Application.AppImage
│  ├─ . . .
│ package.json

Here is a sample of the package.json :这是package.json的示例:

{
  . . .
  "scripts": {
    "build": "concurrently \"yarn build:main\" \"yarn build:renderer\"",
    "build:main": "cross-env NODE_ENV=production webpack --config ./.erb/configs/webpack.config.main.prod.babel.js",
    "build:renderer": "cross-env NODE_ENV=production webpack --config ./.erb/configs/webpack.config.renderer.prod.babel.js",
    "rebuild": "electron-rebuild --parallel --types prod,dev,optional --module-dir src",
    "lint": "cross-env NODE_ENV=development eslint . --cache --ext .js,.jsx,.ts,.tsx",
    "package": "yarn build && electron-builder build --publish never",
    "package-win": "yarn build && electron-builder build --win --x64",
    "package-mac": "yarn build && electron-builder build --mac",
    "postinstall": "node -r @babel/register .erb/scripts/CheckNativeDep.js && electron-builder install-app-deps && yarn cross-env NODE_ENV=development webpack --config ./.erb/configs/webpack.config.renderer.dev.dll.babel.js && opencollective-postinstall && yarn-deduplicate yarn.lock",
    "start": "node -r @babel/register ./.erb/scripts/CheckPortInUse.js && cross-env yarn start:renderer",
    "start:main": "cross-env NODE_ENV=development electron -r ./.erb/scripts/BabelRegister ./src/main.dev.ts",
    "start:renderer": "cross-env NODE_ENV=development webpack serve --config ./.erb/configs/webpack.config.renderer.dev.babel.js",
    "test": "jest"
  },
  "lint-staged": {
    "*.{js,jsx,ts,tsx}": [
      "cross-env NODE_ENV=development eslint --cache"
    ],
    "{*.json,.{babelrc,eslintrc,prettierrc}}": [
      "prettier --ignore-path .eslintignore --parser json --write"
    ],
    "*.{css,scss}": [
      "prettier --ignore-path .eslintignore --single-quote --write"
    ],
    "*.{html,md,yml}": [
      "prettier --ignore-path .eslintignore --single-quote --write"
    ]
  },
  "build": {
    "productName": "VirtualEnsemble",
    "appId": "nonexistent",
    "asarUnpack": [
      "**/node_modules/ffmpeg-static/*"
    ],
    "files": [
      "dist/",
      "node_modules/",
      "index.html",
      "main.prod.js",
      "main.prod.js.map",
      "package.json"
    ],
    "afterSign": ".erb/scripts/Notarize.js",
    "mac": {
      "target": [
        "pkg"
      ],
      "type": "distribution",
      "hardenedRuntime": true,
      "entitlements": "assets/entitlements.mac.plist",
      "entitlementsInherit": "assets/entitlements.mac.plist",
      "gatekeeperAssess": false
    },
    "dmg": {
      "contents": [
        {
          "x": 130,
          "y": 220
        },
        {
          "x": 410,
          "y": 220,
          "type": "link",
          "path": "/Applications"
        }
      ]
    },
    "win": {
      "target": [
        "nsis"
      ]
    },
    "linux": {
      "target": [
        "AppImage"
      ],
      "category": "Development"
    },
    "directories": {
      "app": "src",
      "buildResources": "assets",
      "output": "release"
    },
    "extraResources": [
      "./assets/**"
    ]
  },
. . .
}

And from my main.dev.ts :从我的main.dev.ts

const pathToFFMPEG = require('ffmpeg-static').replace(
  'app.asar',
  'app.asar.unpacked'
);
const ffmpeg = require('fluent-ffmpeg');

ffmpeg.setFfmpegPath(pathToFFMPEG);

As you can see, I tried using the asarUnpack from here , but that did not work for me.如您所见,我尝试从此处使用asarUnpack ,但这对我不起作用。 There is no app.asar.unpacked after packaging the application.打包应用后没有app.asar.unpacked I am thoroughly confused and would appreciate any help.我非常困惑,希望能提供任何帮助。

I found something that seems to work.我发现了一些似乎有效的东西。 I have only tested it on Linux though.我只在 Linux 上测试过它。

I put the following in my package.json under the build section:我将以下内容放在build部分下的package.json中:

"extraResources": [                                                                                                                                                                    
       "./assets/**",
       "./node_modules/ffmpeg-static/ffmpeg"
     ]

which copies the ffmpeg binary from the ffmpeg-static module to a resources folder inside the packaged app.它将 ffmpeg 二进制文件从ffmpeg-static模块复制到打包应用程序内的资源文件夹中。

Then, to use it in the main process, I used然后,为了在主进程中使用它,我使用了

const ffmpegpath=path.join(__dirname, '../node_modules/ffmpeg-static/ffmpeg');
const ffmpeg = require('fluent-ffmpeg');
ffmpeg.setFfmpegPath(ffmpegpath);

I used ../ because the __dirname in electron-react-boilerplate points to the app.asar in the resources folder, but we just want the resources folder.我使用../因为electron-react-boilerplate中的__dirname指向resources文件夹中的app.asar ,但我们只需要resources文件夹。

I am unsure how this will translate to Windows and Mac, I will update when I figure it out.我不确定这将如何转化为 Windows 和 Mac,当我弄清楚时我会更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM