twolfson / karma-electron

Karma launcher and preprocessor for Electron
The Unlicense
59 stars 21 forks source link

Add support to enableRemoteModule for electron >= 14 #59

Closed jeffcunat closed 2 years ago

jeffcunat commented 2 years ago

This PR gives the possibility to use karma-electron with electron >= 14 when using remote in renderer process. This adds @electron/remote@2 dependency and the appropriate calls to enable remote in browserWindow webContents when creating them.

See https://github.com/electron/remote/blob/main/docs/migration-2.md

twolfson commented 2 years ago

Hey @jeffcunat, thanks for the PR!

Unfortunately, I think your change will actually break for electron < 14 users who are still using the electron built-in remote module.

i.e. They'd still be using browserWindowOptions.webPreferences.enableRemoteModule so we'd try to load/call require('@electron/remote/main').initialize() which doesn't exist.

After reading through the documentation, it doesn't seem like any changes are needed to karma-electron at all to use the new package

i.e. We provide a require parameter which can invoke the necessary lines (which are in your PR)

https://github.com/twolfson/karma-electron/tree/7.3.0#launcher-configuration

// karma.conf.js
module.exports = function (config) {
  config.set({
    // Specify usage of our custom launcher
    browsers: ['CustomElectron'],

    // Define a custom launcher which inherits from `Electron`
    customLaunchers: {
      CustomElectron: {
        base: 'Electron',
        require: __dirname + '/test/karma-electron-require.js',
    }
  });
};
// test/karma-electron-require.js
require('@electron/remote/main').initialize();
app.on('browser-window-created', function (_, window) {
  require('@electron/remote/main').enable(window.webContents);
});

If you feel that we should have stronger documentation for this, or a convenience flag, then that's totally understandable and we should discuss that in a separate issue =)

jeffcunat commented 2 years ago

It works great as you suggest. Thanks a lot. So my PR is not useful. Maybe you should mention it in the docs...