I am investigating the use of Cordova Electron so I can move away from Windows UWP (which sees me stuck at Ionic 4)
So, I need to be able to call native code from the "web app", and the way appears to be via the ipcRenderer.
However, I just cannot seem to get a reference to this.
First following another post, I have the nodeIntegration
included in the Election Browser window options.
{
"browserWindow": {
"autoHideMenuBar": true,
"plugins": true,
"webPreferences": {
"nodeIntegration": true
}
}
}
After this, I have gone through many other posts (for example this one, or this one, but none work at all.
Any attempt to get to require
either by itself or on the window object, I always get require is not defined
.
Or, another example, if I have the following:
import { Component } from '@angular/core';
declare function require(name: string);
....
onClick(): void {
try {
const ipc = require('electron').ipcRenderer;
console.log('sending');
ipc.send('test', 'google');
console.log('sent');
} catch (error) {
console.error(error);
}
}
I get errors such as
Warning: D:\dev\ionic\electron-ionic\src\app\tab1\tab1.page.ts depends on 'electron'. CommonJS or AMD dependencies can cause optimization bailouts.
For more info see: https://angular.io/guide/build#configuring-commonjs-dependencies
./node_modules/electron/index.js:1:11-24 - Error: Module not found: Error: Can't resolve 'fs' in 'D:\dev\ionic\electron-ionic\node_modules\electron'
./node_modules/electron/index.js:2:13-28 - Error: Module not found: Error: Can't resolve 'path' in 'D:\dev\ionic\electron-ionic\node_modules\electron'
I really have tried every combination I can find, but nothing works.
What is the correct syntax to get to the ipcRenderer
in this context (ie an Ionic Angular Cordova Electron application)?
Update 1
I tried the "more natural" importing of ipcRenderer as follows:
import { ipcRenderer } from 'electron';
...
ipcRenderer.send('test', 'google')
But same errors as above (fs and path modules) when doing the ionic build
I then did an:
npm i fs --save-dev
npm i path --save-dev
and now down to one error when doing the ionic build
:
./node_modules/electron/index.js:1:11-24 - Error: Module not found: Error: Can't resolve 'fs' in 'D:\dev\ionic\electron-ionic\node_modules\electron'
Strangely I don't see either fs
or path
folder in node_modules
.
Update 2
Putting a breakpoint in the Ionic code, so I can examine then Global
and window
object, I don't see require
, electron
, nor cordova
anywhere.