Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

So close, but cannot get it to work (elaborate snippets inside)! #8

Open
LennardF1989 opened this issue Sep 24, 2017 · 3 comments
Open

Comments

@LennardF1989
Copy link

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?

@astoilkov
Copy link

Hi @LennardF1989,

Did you figure out how to make electron-link work? I have successfully made the snapshot_blob.bin but there seems to no difference in performance. In both cases, the app load time is around 450ms.

@adaml881
Copy link

Hi, have either of you had any progress with this?

@HZSamir
Copy link

HZSamir commented May 8, 2021

3 years later. Does anyone have any idea how to use this thing?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants