Skip to content

Commit 1fb08a7

Browse files
committed
fix/ use node 3rd party libraries in renderer process
1 parent 2e29232 commit 1fb08a7

File tree

11 files changed

+1174
-766
lines changed

11 files changed

+1174
-766
lines changed

Diff for: README.md

+9-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Bootstrap and package your project with Angular 12 and Electron 13 (Typescript +
1818

1919
Currently runs with:
2020

21-
- Angular v12.0.2
21+
- Angular v12.0.5
2222
- Electron v13.0.1
2323
- Electron Builder v22.10.5
2424

@@ -81,16 +81,20 @@ You can disable "Developer Tools" by commenting `win.webContents.openDevTools();
8181
| app | Electron main process folder (NodeJS) |
8282
| src | Electron renderer process folder (Web / Angular) |
8383

84-
## Use Electron / NodeJS libraries
84+
## How to import 3rd party libraries
8585

86-
3rd party libraries used in electron's main process (like an ORM) have to be added in `dependencies` of `app/package.json`.
8786
This sample project runs in both modes (web and electron). To make this work, **you have to import your dependencies the right way**. \
8887

89-
## Use "web" 3rd party libraries (like angular, material, bootstrap, ...)
88+
There are two kind of 3rd party libraries :
89+
- NodeJS's one (like an ORM, Database...)
90+
- Used in electron's Main process (app folder) have to be added in `dependencies` of `app/package.json`
91+
- Used in electron's Renderer process (src folder) have to be added in `dependencies` of both `app/package.json` and `src/package.json`
9092

91-
3rd party libraries used in electron's renderer process (like angular) have to be added in `dependencies` of `package.json`. \
9293
Please check `providers/electron.service.ts` to watch how conditional import of libraries has to be done when using NodeJS / 3rd party libraries in renderer context (i.e. Angular).
9394

95+
- Web's one (like bootstrap, material, tailwind...)
96+
- It have to be added in `dependencies` of `src/package.json`
97+
9498
## Add a dependency with ng-add
9599

96100
You may encounter some difficulties with `ng-add` because this project doesn't use the defaults `@angular-builders`. \

Diff for: angular.json

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
{
22
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
3+
"cli": {
4+
"defaultCollection": "@angular-eslint/schematics"
5+
},
36
"version": 1,
47
"newProjectRoot": "projects",
58
"projects": {
69
"angular-electron": {
710
"root": "",
811
"sourceRoot": "src",
912
"projectType": "application",
13+
"schematics": {
14+
"@schematics/angular:application": {
15+
"strict": true
16+
}
17+
},
1018
"architect": {
1119
"build": {
1220
"builder": "@angular-builders/custom-webpack:browser",
@@ -17,6 +25,7 @@
1725
"tsConfig": "src/tsconfig.app.json",
1826
"polyfills": "src/polyfills.ts",
1927
"assets": [
28+
"src/favicon.ico",
2029
"src/assets"
2130
],
2231
"styles": [
@@ -126,7 +135,7 @@
126135
"eslintConfig": ".eslintrc.json",
127136
"lintFilePatterns": [
128137
"src/**/*.ts",
129-
"app/**.ts"
138+
"src/**/*.component.html"
130139
]
131140
}
132141
}

Diff for: app/main.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { app, BrowserWindow, screen } from 'electron';
22
import * as path from 'path';
3+
import * as fs from 'fs';
34
import * as url from 'url';
45

56
// Initialize remote module
@@ -27,19 +28,25 @@ function createWindow(): BrowserWindow {
2728
enableRemoteModule : true // true if you want to run 2e2 test with Spectron or use remote module in renderer context (ie. Angular)
2829
},
2930
});
31+
3032

3133
if (serve) {
32-
3334
win.webContents.openDevTools();
34-
3535
require('electron-reload')(__dirname, {
36-
electron: require(`${__dirname}/../node_modules/electron`)
36+
electron: require(path.join(__dirname, '/../node_modules/electron'))
3737
});
3838
win.loadURL('http://localhost:4200');
39-
4039
} else {
40+
// Path when running electron executable
41+
let pathIndex = './index.html';
42+
43+
if (fs.existsSync(path.join(__dirname, '../dist/index.html'))) {
44+
// Path when running electron in local folder
45+
pathIndex = '../dist/index.html';
46+
}
47+
4148
win.loadURL(url.format({
42-
pathname: path.join(__dirname, '../dist/index.html'),
49+
pathname: path.join(__dirname, pathIndex),
4350
protocol: 'file:',
4451
slashes: true
4552
}));

Diff for: electron-builder.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"asar": false,
23
"directories": {
34
"output": "release/"
45
},
@@ -12,7 +13,7 @@
1213
"extraResources": [
1314
{
1415
"from": "dist",
15-
"to": "dist",
16+
"to": "app",
1617
"filter": [
1718
"**/*"
1819
]

0 commit comments

Comments
 (0)