javascript Electron - 如何添加外部文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46022443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 06:23:26  来源:igfitidea点击:

Electron - How to add external files?

javascriptnode.jselectron

提问by user2298995

I have an Electron app. I try to make the app open an .exe file. I created a directory in the root folder named liband placed the .exe file there. In development, I have no problem opening the file by using __dirname + '/lib/file.exe, but when I package the app (using yarn dist), it does not open the exe file and there is no libfolder anymore on the distfolder.

我有一个电子应用程序。我尝试让应用程序打开一个 .exe 文件。我在名为的根文件夹中创建了一个目录,lib并将 .exe 文件放在那里。在开发中,我通过使用打开文件没有问题__dirname + '/lib/file.exe,但是当我打包应用程序(使用yarn dist)时,它不会打开 exe 文件并且lib文件夹上不再有dist文件夹。

I tried writing to console the default location using console.log(__dirname)and it outputs \dist\win-unpacked\resources\app.asa(which is a file).

我尝试使用console.log(__dirname)和它输出\dist\win-unpacked\resources\app.asa(这是一个文件)来写入控制台默认位置。

How can I add an external file that can be accessed when the app is packaged?

如何添加应用打包时可以访问的外部文件?

回答by dimaqw

Add the following code to package.json:

将以下代码添加到 package.json 中:

 "build": {
    "extraResources": [
      {
        "from": "./src/extraResources/",
        "to": "extraResources",
        "filter": [
          "**/*"
        ]
      }
    ]
  }

Then, you can access the files using

然后,您可以使用访问文件

const configFile = path.join(path.dirname(__dirname), 'extraResources','config.json');

I use the following folders structure which allows me to run the app any way.

我使用以下文件夹结构,它允许我以任何方式运行应用程序。

from project folder: node_modules\.bin\electron.cmd src\main\index.js

从项目文件夹: node_modules\.bin\electron.cmd src\main\index.js

from unpacked source dist\win-unpacked\app.exe check-for-update

来自未打包的源 dist\win-unpacked\app.exe check-for-update

from installed folder C:\Users\user\AppData\Local\Programs\app\app.exe

从安装的文件夹 C:\Users\user\AppData\Local\Programs\app\app.exe

+-- dist
|   +-- win-unpacked
|      +-- resources
|         +-- extraResources
|            config.json
+-- node_modules
+-- src 
|   +-- extraResources
|      config.json
|      someFile.js
|   +-- main
|      index.js
|   +-- render
|      index.js

回答by user2298995

Managed to solve it by using extraResources. Should be declared under build in your package.jsonfile.

设法通过使用 extraResources 来解决它。应该在您的package.json文件中的build 下声明。

For example:

例如:

  1. Create a new folder named extraResources adjacent to pacakge.json
  2. Add the following code to your package.jsonfile:

    "build": { "extraResources": ["./extraResources/**"] }

  3. Then, you can access the files inside this folder by using __dirname + '/../extraResources/'from your main app.
  1. 在 pacakge.json 旁边创建一个名为 extraResources 的新文件夹
  2. 将以下代码添加到您的package.json文件中:

    "build": { "extraResources": ["./extraResources/**"] }

  3. 然后,您可以使用__dirname + '/../extraResources/'主应用程序访问此文件夹中的文件。

回答by Romy

there I found a new solution, using electron-packager on Windows do not add the files into the resources folder at the end of the process.

在那里我找到了一个新的解决方案,在 Windows 上使用电子打包器不要在过程结束时将文件添加到资源文件夹中。

So I added this command into the package.json

所以我将此命令添加到 package.json

"build-win": "electron-packager . --platform=win32 --asar --prune --arch=ia32 --extra-resource=./extraResources/documents/QuickStartGuideWN-H1.pdf --extra-resource=./extraResources/MAC_drivers/MacOS10.14/ --icon=assets/alfa_a.ico --out ./dist --overwrite",

And now the files are insied the resource fodlder just add

现在文件被插入资源文件夹中,只需添加

--extra-resource=./extraResources/file