Open danikaze opened 1 month ago
So... I think this is a problem of tree-shaking
In previous project I made on electron (from different setup: i.e. erb) I never had problems even exporting both ipcRenderer
and ipcMain
in the same file (because in the end, renderer is going to import only ipcRenderer
and the rest of the code is not going to be included in the build.
But I was guessing that this problem happens because in the electron
module there are other exports for the main process that require things like fs
or path
which is the error that the renderer shows in the end...
To prove it, I just tried creating the classic example to show that tree-shaking works in webpack:
Created a util.ts
like:
export function sum(a: number, b: number): number {
return a + b;
}
export function mul(a: number, b: number): number {
return a * b;
}
And added this in renderer.ts
:
import { sum } from './util';
console.log(sum(4,5))
It works fine but, when I check the source code in the dev tools... we can see that there's no tree shaking being performed:
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.sum = sum;
exports.mul = mul;
function sum(a, b) {
return a + b;
}
function mul(a, b) {
return a * b;
}
//# sourceURL=[module]
//# sourceMappingURL=data:application/json;charset=utf-8;base64,[shortened]
//# sourceURL=webpack-internal:///./src/util.ts
Technically, tree shaking in webpack only removes dead code on production move... but in development at least it should mark it with /* unused harmony */
.
Also, tried adding mode: 'production'
to the webpack.renderer.config.ts
and code is still not removed...
Might be following the wrong hint but... idk what to try tbh
Also reproducible in Mac:
Installed with:
npm init electron-app@latest electron-react-typescript -- --template=webpack-typescript
npm i -D electron # → issue #3273
ERROR in ./node_modules/electron/index.js 1:11-24
Module not found: Error: Can't resolve 'fs' in 'PROJECT_ROOT/node_modules/electron'
Pre-flight checklist
Electron Forge version
7.4.0
Electron version
31.3.1
Operating system
Windows 10 / 22H2 (OS Build 19045.4651)
Last known working Electron Forge version
?
Expected behavior
Compilation works.
Actual behavior
On running
npm run start
, compilation fails (see additional info)Steps to reproduce
From a fresh install via
just add the following to
renderer.ts
:then run
npm start
Additional information
Compilation output: