[英]electron-forge: how to specify hooks?
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.这些中间文件夹通常是在打包期间的本机构建过程中生成的。
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.
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.