从Electron Forge的应用程序中删除package.json或其中的一部分。

huangapple go评论57阅读模式
英文:

Removing package.json or part of it from app in electron-forge

问题

I have started using electron-forge. My package.json is begin added to the final output after making and packaging.

The package.json contains info which I dont want to to be in production. Such as devDependencies or scripts. Putting package.json in forge.config.js ignore list breaks the build.

Is there a way to remove the file or hide ceratin parts of it in the final packaged app?

英文:

I have started using electron-forge. My package.json is begin added to the final output after making and packaging.

The package.json contains info which I dont want to to be in production. Such as devDependencies or scripts. Putting package.json in forge.config.js ignore list breaks the build.

Is there a way to remove the file or hide ceratin parts of it in the final packaged app?

答案1

得分: 2

使用forge.config.js文件中的postPackage钩子,示例代码如下:

{
	hooks: {
		postPackage: async (forgeConfig, options) => {
			console.info('postPackage() Packages built at:', options.outputPaths);
			if (!(options.outputPaths instanceof Array)) {
				return;
			}
			const packageDotJson = path.join(options.outputPaths[0], 'resources', 'app', 'package.json');
			const content = fs.readFileSync(packageDotJson);
			const json = JSON.parse(content.toString());
			Object.keys(json).forEach((key) => {
				switch (key) {
					case 'name': {
						break;
					}
					case 'version': {
						break;
					}
					case 'main': {
						break;
					}
					case 'author': {
						break;
					}
					case 'description': {
						break;
					}
					default: {
						delete json[key];
						break;
					}
				}
			});
			fs.writeFileSync(packageDotJson, JSON.stringify(json, null, '\t'));
		},
	}
}
英文:

Use the postPackage hook within your forge.config.js file, like so for example:

{
	hooks: {
		postPackage: async (forgeConfig, options) => {
			console.info('postPackage() Packages built at:', options.outputPaths);
			if (!(options.outputPaths instanceof Array)) {
				return;
			}
			const packageDotJson = path.join(options.outputPaths[0], 'resources', 'app', 'package.json');
			const content = fs.readFileSync(packageDotJson);
			const json = JSON.parse(content.toString());
			Object.keys(json).forEach((key) => {
				switch (key) {
					case 'name': {
						break;
					}
					case 'version': {
						break;
					}
					case 'main': {
						break;
					}
					case 'author': {
						break;
					}
					case 'description': {
						break;
					}
					default: {
						delete json[key];
						break;
					}
				}
			});
			fs.writeFileSync(packageDotJson, JSON.stringify(json, null, '\t'));
		},
	}
}

huangapple
  • 本文由 发表于 2023年5月7日 03:52:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76190841.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定