Wednesday, 25 September 2019

Electron: How to execute bundled file?

TL;DR: I have a bundled JS file bundle.js created with Browserify. I want to load this file and attach the contents to a BrowserView in Electron using executeJavaScript. How can I do this?

The package I need to include is Web3.js. I bundled Web3 with Browserify by running:

browserify packages/web3/src/index.js -o web3-bundle.js

Here are the two most promising approaches to include the package into the BrowserView, which both unfortunately didn't work.

  1. Using require.

I tried using require to import the file.

const Web3 = require('web3-bundle.js');

view.webContents.executeJavaScript(`
  ${Web3};
  console.log(Web3);
`);
  1. Using fs:
fs.readFile(path.resolve(__dirname, 'web3-bundle.js'), 'utf8', function (err, data) {
  if (err) {
    console.error(err);
    return;
  }
  console.log('data', data)
view.webContents.executeJavaScript(`
  ${data};
  console.log(Web3);
`)
});

How can you attach a bundled file into a BrowserView in Electron?



from Electron: How to execute bundled file?

No comments:

Post a Comment