3

I've rigged create-react-app with the electron-forge app and now I need to somehow specify the build folder produced from the CRA for the packaging. That folder should also be served.

Would such a thing be possible with electron-forge?

Nemanja Milosavljevic
  • 1,251
  • 18
  • 33

3 Answers3

4

I understand are you asking how to tell electron-forge which directory to find your source files in for packaging the app.

If so, see: https://github.com/electron-userland/electron-packager/blob/master/docs/api.md

where it describes the options of the

"config": { "forge": {

object in your package.json file

inside they there is this package config object:

  "electronPackagerConfig": {
    "dir": "./src",

where you can specify your source folder.

Also, BTW: there you can specify files/file-regexs to be ignored in packaging:

    "ignore": [".idea", ".gitignore"]
Andrew Mackenzie
  • 5,477
  • 5
  • 48
  • 70
  • 9
    This does not seem to work with electron-forge version 6.0.0-beta.47. This webpage mentions that you cannot overwrite the "dir" property of electron-packager: https://www.electronforge.io/configuration#packager-config –  Jan 17 '20 at 22:23
2

electron-forge has no option to specify input folder (project's root folder will be used):

  1. Specify ignore option to skip folders/files;
  2. Use main key in package.json to specify correct start script.

For example, package.json for vue project:

{
    "name": "project",
    "version": "1.0.0",
    "main": "index.js",
    ...
    "config": {
        "forge": {
            "packagerConfig": {
                "ignore": [
                    "^/[.]vs$",
                    "^/public$",
                    "^/src$",
                    "^/[.]browserslistrc$",
                    "^/[.]editorconfig$",
                    "^/tsconfig[.]json$",
                    "[.](cmd|user|DotSettings|njsproj|sln)$"
                ]
            },
            ...
        }
    },
    ...
}
Yaroslav Sivakov
  • 470
  • 3
  • 14
  • 1
    If you specify your electron-forge configuration in a Javascript file ([see docs](https://www.electronforge.io/configuration)), you can also provide a function for the ignore filter ([see docs](https://electron.github.io/electron-packager/main/interfaces/electronpackager.options.html#ignore)). This is a lot more flexible than a list of regex patterns; you can implement the ignore function to act as a whitelist of folders to include rather than a blacklist of folders to exclude – mark.monteiro Sep 08 '22 at 21:50
0

Actually you could specify source folder in script's arg only (assume /src is the folder with files to package): electron-forge package ./src

That source folder must contain package.json with data electron-forge need to package your project. If no package.json will be found or it will not meet the requirements, electron-forge will go up to folder with proper package.json and assume it's the actual project folder. As I found in electron-forge sources, it needs at least this data:

{ 
  name: cfg.name,
  version: cfg.version,
  author: cfg.author,
  main: "main.js",
  devDependencies: {
    electron: cfg.devDependencies.electron,
    "@electron-forge/cli": cfg.devDependencies["@electron-forge/cli"]
  },
  config: {
    forge: "../forge.config.js"
  }
}

In my project I create this package.json by script and use my main package.json as cfg in sample above to provide data. Also you could provide forge config as a path to your forge.config.js to avoid of duplicate it.

Also keep in mind electron-forge would package files/dirs in that folder only, so neither node_modules nor other external assets will be included. Therefore you need either make bundle of your project with webpack, esbundle etc. or be careful with dependencies and provide it for packaged app.