简体   繁体   English

电子锻造:如何指定钩子?

[英]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.我想在电子伪造的打包步骤之前删除一些文件夹,因为package.json中的选项ignore.roge.config并没有删除我指定要忽略的所有中间文件夹。 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,但是添加带有记录事件的hooks字段似乎不起作用,例如,

having a package.json field like this seems to add nothing to the equation, ie, I don't see the expected console log.像这样的package.json字段似乎没有添加任何内容,即,我没有看到预期的控制台日志。

  "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参考 相关的 elctron-forge github 问题,我还尝试将 JS 源文件提供给钩子


"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 :上面的代码在运行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然后在package.json引用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 电子锻造制造不会建造 - electron-forge make won't build 使用VSCode调试电子锻造应用程序 - Debug electron-forge app with VSCode ElectronJS npm 启动/bash:电子伪造:找不到命令 - ElectronJS npm start / bash: electron-forge: command not found 使用电子伪造打包应用程序时出现问题“无法解析'./→'” - Problem while packaging application with electron-forge “Can't resolve './→' ” 在电子锻造项目中安装“node-pty”后,缺少名为 pty.node.js 的文件。 如何在 Linux 中安装 node-pty - A file called pty.node.js is missing after installing "node-pty" in a electron-forge project. How can I install node-pty in Linux Electron-forge npm run make“错误:找不到您的应用程序的主要入口点。” - Electron-forge npm run make "Error: The main entry point to your app was not found." “未捕获的 ReferenceError:require is not defined” 在 index.html 的电子锻造应用程序与反应模板 - “Uncaught ReferenceError: require is not defined” in index.html of electron-forge app with react template @electron-forge/plugin-webpack 中的 configuration.module.rules[2] 问题 - configuration.module.rules[2] Issue in @electron-forge/plugin-webpack Electron 用反应锻造? - Electron Forge with react? Electron Forge 卡在松鼠上 - Electron Forge stuck on squirrel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM