Conditional import of Electron/NodeJS libs - The app can be launch in browser mode

This commit is contained in:
Maxime GRIS
2017-05-06 00:44:59 +02:00
parent 9d43304b67
commit c434f8a8b0
9 changed files with 59 additions and 12 deletions

View File

@@ -1,4 +1,4 @@
import { HomeComponent } from './home/home.component'; import { HomeComponent } from './components/home/home.component';
import { NgModule } from '@angular/core'; import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router'; import { Routes, RouterModule } from '@angular/router';

View File

@@ -1,6 +1,5 @@
import { Component } from '@angular/core'; import { Component } from '@angular/core';
import { ipcRenderer } from 'electron'; import { ElectronService } from './providers/electron.service';
import * as childProcess from 'child_process';
@Component({ @Component({
selector: 'app-root', selector: 'app-root',
@@ -8,10 +7,16 @@ import * as childProcess from 'child_process';
styleUrls: ['./app.component.scss'] styleUrls: ['./app.component.scss']
}) })
export class AppComponent { export class AppComponent {
constructor() { constructor(public electronService: ElectronService) {
// Check if electron is correctly injected (see externals in webpack.config.js)
console.log('c', ipcRenderer); if(electronService.isElectron()) {
// Check if nodeJs childProcess is correctly injected (see externals in webpack.config.js) console.log('Mode electron');
console.log('c', childProcess); // Check if electron is correctly injected (see externals in webpack.config.js)
console.log('c', electronService.ipcRenderer);
// Check if nodeJs childProcess is correctly injected (see externals in webpack.config.js)
console.log('c', electronService.childProcess);
} else {
console.log('Mode web');
}
} }
} }

View File

@@ -1,13 +1,17 @@
import 'zone.js';
import 'reflect-metadata';
import { BrowserModule } from '@angular/platform-browser'; import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core'; import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms'; import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http'; import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component'; import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component'; import { HomeComponent } from './components/home/home.component';
import { AppRoutingModule } from './app-routing.module'; import { AppRoutingModule } from './app-routing.module';
import { ElectronService } from './providers/electron.service';
@NgModule({ @NgModule({
declarations: [ declarations: [
AppComponent, AppComponent,
@@ -19,7 +23,7 @@ import { AppRoutingModule } from './app-routing.module';
HttpModule, HttpModule,
AppRoutingModule AppRoutingModule
], ],
providers: [], providers: [ElectronService],
bootstrap: [AppComponent] bootstrap: [AppComponent]
}) })
export class AppModule { } export class AppModule { }

View File

@@ -2,7 +2,7 @@
color: black; color: black;
margin:0; margin:0;
padding:50px 20px; padding:50px 20px;
background: url(../../assets/background.jpg) no-repeat center fixed; background: url(../../../assets/background.jpg) no-repeat center fixed;
-webkit-background-size: cover; /* pour anciens Chrome et Safari */ -webkit-background-size: cover; /* pour anciens Chrome et Safari */
background-size: cover; /* version standardisée */ background-size: cover; /* version standardisée */
} }

View File

@@ -26,7 +26,7 @@ describe('HomeComponent', () => {
it(`should have as title 'App works !'`, async(() => { it(`should have as title 'App works !'`, async(() => {
const fixture = TestBed.createComponent(HomeComponent); const fixture = TestBed.createComponent(HomeComponent);
const app = fixture.debugElement.componentInstance; const app = fixture.debugElement.componentInstance;
expect(app.title).toEqual('aApp works !'); expect(app.title).toEqual('App works !');
})); }));
it('should render title in a h1 tag', async(() => { it('should render title in a h1 tag', async(() => {

View File

@@ -0,0 +1,15 @@
import { TestBed, inject } from '@angular/core/testing';
import { ElectronService } from './electron.service';
describe('ElectronService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [ElectronService]
});
});
it('should ...', inject([ElectronService], (service: ElectronService) => {
expect(service).toBeTruthy();
}));
});

View File

@@ -0,0 +1,23 @@
import { Injectable } from '@angular/core';
import { ipcRenderer } from 'electron';
import * as childProcess from 'child_process';
@Injectable()
export class ElectronService {
ipcRenderer: typeof ipcRenderer;
childProcess: typeof childProcess;
constructor() {
if (this.isElectron()) {
this.ipcRenderer = window.require('electron').ipcRenderer;
this.childProcess = window.require('child_process');
}
}
isElectron = () => {
return window && window.process && window.process.type;
}
}