-
Notifications
You must be signed in to change notification settings - Fork 40
So close, but cannot get it to work (elaborate snippets inside)! #8
Description
I looked very closely at how Atom implements electron-link, but I for the world cannot get it to work... It also seems like almost none is actually using this, which is a shame, because it's a pretty cool feature. Hence I'm going to document what I have done, so others can learn from it and save themselves a day of work. Anyway, some help would be very much appreciated to get it running. I feel like I'm close, but I'm just not seeing what I'm missing...
I made a snapshot.js which contains the following:
if(false) {
require("./native");
require("./main");
}
The two files from above are the only two I need a snapshot from. I guarded them with an if(false) so I have complete control when to load them.
I use the following compile.js, roughly based on Atom, but very simplied:
const electronLink = require("electron-link");
const fs = require("fs");
const vm = require("vm");
const path = require("path");
const childProcess = require("child_process");
let processedFiles = 0;
const snapshotScriptPath = "./cache/snapshot_generated.js";
electronLink({
baseDirPath: "./app",
mainPath: "./app/snapshot.js",
cachePath: "./cache",
shouldExcludeModule: (modulePath) => {
console.log(modulePath);
return !modulePath.endsWith("native.js") && !modulePath.endsWith("main.js");
}
}).then(function (snapshotScript) {
fs.writeFileSync(snapshotScriptPath, snapshotScript);
vm.runInNewContext(snapshotScript, undefined, { filename: snapshotScriptPath, displayErrors: true });
var mksnapshotPath = path.join('.', 'node_modules', 'electron-mksnapshot', 'bin', 'mksnapshot');
childProcess.execFileSync(mksnapshotPath, [snapshotScriptPath, "--startup_blob", "./app/snapshot_blob.bin"]);
}).catch((error) => {
console.log(error);
});
The two files from the snapshot.js are of the following format:
module.exports = function() {
//Do stuff
}
My startup.js, which is set in package.json, does the following:
var main;
if (typeof snapshotResult !== 'undefined') {
snapshotResult.setGlobals(global, process, global, {}, console, require);
main = snapshotResult.customRequire("./main");
}
else {
main = require("./main");
}
main();
main() effectively runs the module.exports from before, creates a BrowserWindow with a preload-script, which in turn requires native.js in a similar fashion.
Everything works fine when trying out using electron app from the CLI. But after building, and replacing the default snapshot_blob.bin, it simply does not work, at all.
Can someone please point out what I'm missing?
PS. I also notice, when I open the snapshot_blob.bin with notepad, the whole generated snapshot.js is fully readable, kind of defeating the point of compiling it (other than speed). What's up with that?