To use asarmor in an Electron Forge project, you must use the hooks configuration to handle native dependency copying and the encryption/patching process.
packageAfterCopy: Use this hook to copy the native asarmor binaries (main.node and renderer.node) from your node_modules into the build directory. This ensures the native modules are available in the packaged application.postPackage: Use this hook to perform the security operations. This involves calling asarmor.encrypt to encrypt JavaScript files within the .asar archive and using asarmor.open followed by .patch() and .write() to apply security patches to the archive.
const config: ForgeConfig = {
// ... other config
hooks: {
packageAfterCopy: async (forgeConfig, buildPath) => {
const release = join(__dirname, 'node_modules', 'asarmor', 'build', 'Release');
// Copy main.node
await copyFile(
join(release, 'main.node'),
join(buildPath, '.vite', 'build', 'main.node')
);
// Copy renderer.node
await copyFile(
join(release, 'renderer.node'),
join(buildPath, '.vite', 'renderer', 'main_window', 'renderer.node')
);
},
postPackage: async (forgeConfig, buildPath) => {
const asarPath = `${buildPath.outputPaths[0]}/resources/app.asar`;
// Encrypt JS files in the ASAR
await asarmor.encrypt({
src: asarPath,
dst: asarPath,
});
// Apply patches to the ASAR
const archive = await asarmor.open(asarPath);
archive.patch();
await archive.write(asarPath);
},
},
};