Electron Forge

repository·main·Indexed 27 days ago

https://github.com/electron/forge

A complete toolset for building, packaging, and distributing modern Electron applications. It provides a unified workflow including the @electron-forge/core API for Node.js execution, the create-electron-app bootstrapper, and various makers for platform-specific distribution such as AppX, MSIX, Squirrel, and Wix for Windows; DMG and PKG for macOS; and DEB, RPM, Snap, and Flatpak for Linux.

Tokens
28.6K
Snippets
63
Records
172
Agent score
88%

What's inside electron-forge

  1. Use the Electron Forge Core API

    main

    The @electron-forge/core module provides the base Electron Forge API as a set of asynchronous JavaScript functions. This allows you to execute Forge commands directly in Node.js without using the CLI.

    Note that all user-side configuration is still managed via your Forge configuration file or the config.forge section in your package.json.

    import { api } from '@electron-forge/core';
    
    // Package the current directory as an Electron app
    api.package(__dirname);
  2. Publish artifacts to Nucleus Update Server

    main

    Use @electron-forge/publisher-nucleus to publish your Electron Forge artifacts to a Nucleus Update Server instance. This allows users to download updates across all three supported platforms.

    To use this publisher, add it to the publishers array in your forge.config.js file. It is highly recommended to provide the token via an environment variable rather than hard-coding it into your configuration file for security.

    module.exports = {
      // ...
      publishers: [
        {
          name: '@electron-forge/publisher-nucleus',
          config: {
            host: 'https://my-nucleus.mysite.com',
            appId: 1,
            channelId: 'abcdefg',
            token: 'my-token'
          }
        }
      ]
    };
  3. Publish artifacts to GitHub Releases with @electron-forge/publisher-github

    main

    Use @electron-forge/publisher-github to automatically publish your build artifacts to GitHub Releases. This enables users to download files directly from your repository and allows open-source projects to use the update.electronjs.org hosted update service.

    To use this publisher, add it to the publishers array in your forge.config.js file. You must provide the repository object containing the owner and name of your GitHub repository.

    module.exports = {
      // ...
      publishers: [
        {
          name: '@electron-forge/publisher-github',
          config: {
            repository: {
              owner: 'me',
              name: 'awesome-thing'
            },
            prerelease: true
          }
        }
      ]
    };
  4. Use @electron-forge/publisher-s3 to publish artifacts to Amazon S3

    main

    The @electron-forge/publisher-s3 publisher uploads your build artifacts to an Amazon S3 bucket.

    By default, artifacts are stored using the following key pattern: ${config.folder || appVersion}/${artifactName}

    Warning: If you run the publish command multiple times with the same version on the same platform, existing artifacts in S3 may be overwritten. Ensure you manage your release versions carefully to avoid overwriting previous releases.

    module.exports = {
      // ...
      publishers: [
        {
          name: '@electron-forge/publisher-s3',
          config: {
            bucket: 'my-bucket',
            public: true
          }
        }
      ]
    };
  5. Use @electron-forge/maker-rpm to build RPM packages

    main

    The @electron-forge/maker-rpm maker builds .rpm files, which are the standard package format for RedHat-based Linux distributions like Fedora.

    Prerequisites: You must build the RPM target on a Linux machine that has the rpm or rpm-build packages installed.

  6. Configure Bitbucket authentication via environment variables

    main

    For security, it is recommended to use environment variables instead of hardcoding credentials in your forge.config.js. When using environment variables, you can omit the auth object from the publisher configuration.

    Set the following environment variables:

    • BITBUCKET_USERNAME
    • BITBUCKET_APP_PASSWORD
    # env.sh
    BITBUCKET_USERNAME="myusername"
    BITBUCKET_APP_PASSWORD="mysecretapppassword"
    
    source env.sh
  7. Use @electron-forge/maker-deb to build .deb packages

    main

    The @electron-forge/maker-deb maker allows you to build .deb packages for Debian-based Linux distributions (like Ubuntu).

    Prerequisites: To build the .deb target, you must use a Linux or macOS machine with the fakeroot and dpkg packages installed.

    {
      name: '@electron-forge/maker-deb',
      config: {
        options: {
          maintainer: 'The Forgers',
          homepage: 'https://example.com',
          icon: 'path/to/icon.svg'
        }
      }
    }
  8. Use @electron-forge/plugin-webpack to compile main and renderer processes

    main

    The @electron-forge/plugin-webpack plugin automates the setup of webpack tooling for Electron applications. It handles compilation for both the main process and renderer processes, provides built-in support for Hot Module Replacement (HMR) in the renderer process, and supports multiple renderers.

    To use it, add the plugin to your forge.config.js and provide configuration for mainConfig and one or more renderer entry points.

    // forge.config.js
    
    module.exports = {
      plugins: [
        {
          name: '@electron-forge/plugin-webpack',
          config: {
            mainConfig: './webpack.main.config.js',
            renderer: {
              config: './webpack.renderer.config.js',
              entryPoints: [{
                name: 'main_window',
                html: './src/renderer/index.html',
                js: './src/renderer/index.js',
                preload: {
                  js: './src/preload.js'
                }
              }]
            }
          }
        }
      ]
    };
  9. Initialize a new Electron Forge project

    main

    You can quickly bootstrap a new Electron application using the create-electron-app CLI tool. This sets up a project with Electron Forge pre-configured.

    Pre-requisites:

    • Node.js 16.4.0 or higher
    • Git

    Steps:

    1. Run the initialization command.
    2. Navigate into the project directory.
    3. Start the development server.
    npx create-electron-app@latest my-new-app
    
    # then
    cd my-new-app
    npm start