简体   繁体   中英

electron-forge: how to specify hooks?

Goal

I want to get rid of some folders before the electron-forge's packaging step, because the option ignore.roge.config in package.json does not get rid of all the intermediate folders that I specify to ignore for some packages. Those intermediate folders are usually generated during a native build process during packaging.

Problem

But adding hooks field with the documented events don't seem to work, eg,

having a package.json field like this seems to add nothing to the equation, ie, I don't see the expected console log.

  "config": {
    "forge": {
      "packagerConfig": {
        "icon": "src/images/myapp",
        "ignore": [
          "/.gitignore",
          "/.vscode",
          "/yarn.lock",
          "/node_modules/mydep/build/",
          "/node_modules/mydep/prebuilds/linux*"
        ]
      },
      "hooks": {
          "prePackage": "async () => {\"console.log("this is prepackage step.");\"} "
      },
      "makers": [
        {
          "name": "@electron-forge/maker-zip",
          "platforms": [
            "darwin",
            "win32"
          ]
        }
      ]
    }
  },

Referring to a related elctron-forge github issue , I've also tried to feed a JS source file to hooks


"hooks": "require:./hooks.js",

where the hooks script looks like

{
    prePackage: async () => {
        console.log('this is prepackage step.');
    }
}

This didn't work either.

Worse, I can't even specify multiple hooks this way:

{
    generateAssets: async () => {
        console.log('We should generate some assets here');
    },
    prePackage: async (forgeConfig, options) => {
        console.error('lbn: prePackage');
    }
}

The above code gives me the following error when running yarn make :

An unhandled error has occurred inside Forge:
Unexpected token ':'
/path/to/myapp/hooks.js:5
    prePackage: async (forgeConfig, options) => {
              ^

SyntaxError: Unexpected token ':'
    at wrapSafe (internal/modules/cjs/loader.js:1116:16)
    at Module._compile (internal/modules/cjs/loader.js:1164:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
    at Module.load (internal/modules/cjs/loader.js:1049:32)
    at Function.Module._load (internal/modules/cjs/loader.js:937:14)
    at Module.require (internal/modules/cjs/loader.js:1089:19)
    at require (internal/modules/cjs/helpers.js:73:18)
    at renderConfigTemplate (/path/to/myapp/node_modules/@electron-forge/core/src/util/forge-config.ts:100:20)
    at _default (/path/to/myapp/node_modules/@electron-forge/core/src/util/forge-config.ts:145:3)
    at /path/to/myapp/node_modules/@electron-forge/core/src/api/make.ts:96:19
error Command failed with exit code 1.

Question

What is the right way to specify hooks?

Solved it myself.

We should place the hooks as a normal global module


// ./hooks.js

const fs = require('fs');
const path = require('path');

module.exports = {

postPackage: async (forgeConfig, options) => {
    console.warn('\n\npostPackage: exclude files ...\n\n');
}

}; // module.exports = {


Then refer to it in package.json

"hooks": "require:./hooks.js",

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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