Electron - How to add external files?

前端 未结 4 1722
感动是毒
感动是毒 2020-12-05 05:42

I have an Electron app. I try to make the app open an .exe file. I created a directory in the root folder named lib and placed the .exe file there. In developme

相关标签:
4条回答
  • 2020-12-05 06:06

    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.

    So I added this command into the 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
    
    0 讨论(0)
  • 2020-12-05 06:16

    Add the following code to 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

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

    from installed folder 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
    
    0 讨论(0)
  • 2020-12-05 06:24

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

    For example:

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

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

    3. Then, you can access the files inside this folder by using __dirname + '/../extraResources/' from your main app.
    0 讨论(0)
  • 2020-12-05 06:26

    Same as answer by @user2298995 , but got error using __dirname + '/../extraResources/filename' in windows x64.

    But path.join(process.resourcesPath,'extraResources',fileName) worked fine.

    0 讨论(0)
提交回复
热议问题