Skip to content

Commit c434f8a

Browse files
committed
Conditional import of Electron/NodeJS libs - The app can be launch in browser mode
1 parent 9d43304 commit c434f8a

9 files changed

+59
-12
lines changed

Diff for: src/app/app-routing.module.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HomeComponent } from './home/home.component';
1+
import { HomeComponent } from './components/home/home.component';
22
import { NgModule } from '@angular/core';
33
import { Routes, RouterModule } from '@angular/router';
44

Diff for: src/app/app.component.ts

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
import { Component } from '@angular/core';
2-
import { ipcRenderer } from 'electron';
3-
import * as childProcess from 'child_process';
2+
import { ElectronService } from './providers/electron.service';
43

54
@Component({
65
selector: 'app-root',
76
templateUrl: './app.component.html',
87
styleUrls: ['./app.component.scss']
98
})
109
export class AppComponent {
11-
constructor() {
12-
// Check if electron is correctly injected (see externals in webpack.config.js)
13-
console.log('c', ipcRenderer);
14-
// Check if nodeJs childProcess is correctly injected (see externals in webpack.config.js)
15-
console.log('c', childProcess);
10+
constructor(public electronService: ElectronService) {
11+
12+
if(electronService.isElectron()) {
13+
console.log('Mode electron');
14+
// Check if electron is correctly injected (see externals in webpack.config.js)
15+
console.log('c', electronService.ipcRenderer);
16+
// Check if nodeJs childProcess is correctly injected (see externals in webpack.config.js)
17+
console.log('c', electronService.childProcess);
18+
} else {
19+
console.log('Mode web');
20+
}
1621
}
1722
}

Diff for: src/app/app.module.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1+
import 'zone.js';
2+
import 'reflect-metadata';
13
import { BrowserModule } from '@angular/platform-browser';
24
import { NgModule } from '@angular/core';
35
import { FormsModule } from '@angular/forms';
46
import { HttpModule } from '@angular/http';
57

68
import { AppComponent } from './app.component';
7-
import { HomeComponent } from './home/home.component';
9+
import { HomeComponent } from './components/home/home.component';
810

911
import { AppRoutingModule } from './app-routing.module';
1012

13+
import { ElectronService } from './providers/electron.service';
14+
1115
@NgModule({
1216
declarations: [
1317
AppComponent,
@@ -19,7 +23,7 @@ import { AppRoutingModule } from './app-routing.module';
1923
HttpModule,
2024
AppRoutingModule
2125
],
22-
providers: [],
26+
providers: [ElectronService],
2327
bootstrap: [AppComponent]
2428
})
2529
export class AppModule { }
File renamed without changes.

Diff for: src/app/home/home.component.scss renamed to src/app/components/home/home.component.scss

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
color: black;
33
margin:0;
44
padding:50px 20px;
5-
background: url(../../assets/background.jpg) no-repeat center fixed;
5+
background: url(../../../assets/background.jpg) no-repeat center fixed;
66
-webkit-background-size: cover; /* pour anciens Chrome et Safari */
77
background-size: cover; /* version standardisée */
88
}

Diff for: src/app/home/home.component.spec.ts renamed to src/app/components/home/home.component.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe('HomeComponent', () => {
2626
it(`should have as title 'App works !'`, async(() => {
2727
const fixture = TestBed.createComponent(HomeComponent);
2828
const app = fixture.debugElement.componentInstance;
29-
expect(app.title).toEqual('aApp works !');
29+
expect(app.title).toEqual('App works !');
3030
}));
3131

3232
it('should render title in a h1 tag', async(() => {
File renamed without changes.

Diff for: src/app/providers/electron.service.spec.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { TestBed, inject } from '@angular/core/testing';
2+
3+
import { ElectronService } from './electron.service';
4+
5+
describe('ElectronService', () => {
6+
beforeEach(() => {
7+
TestBed.configureTestingModule({
8+
providers: [ElectronService]
9+
});
10+
});
11+
12+
it('should ...', inject([ElectronService], (service: ElectronService) => {
13+
expect(service).toBeTruthy();
14+
}));
15+
});

Diff for: src/app/providers/electron.service.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Injectable } from '@angular/core';
2+
3+
import { ipcRenderer } from 'electron';
4+
import * as childProcess from 'child_process';
5+
6+
@Injectable()
7+
export class ElectronService {
8+
9+
ipcRenderer: typeof ipcRenderer;
10+
childProcess: typeof childProcess;
11+
12+
constructor() {
13+
if (this.isElectron()) {
14+
this.ipcRenderer = window.require('electron').ipcRenderer;
15+
this.childProcess = window.require('child_process');
16+
}
17+
}
18+
19+
isElectron = () => {
20+
return window && window.process && window.process.type;
21+
}
22+
23+
}

0 commit comments

Comments
 (0)