angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

Support for Electron and Externals

Open gregvis opened this issue 9 years ago • 22 comments

Please provide us with the following information:

OS?

All OSes

Versions.

1.0.0-beta.26

Repro steps.

clone my electron starter project git clone https://github.com/gregvis/angularcli-electron.git cd into it npm i npm start error in console "Uncaught TypeError: fs.existsSync is not a function"

The log given by the failure.

"Uncaught TypeError: fs.existsSync is not a function"

Mention any other details that might be useful.

In app.component.ts, i have this code: import { Component } from '@angular/core'; const remote = require('electron').remote; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { title = 'app works!'; constructor() { remote.dialog.showOpenDialog({ title: 'Electron Works!' }); } }

The remote.dialog.showOpenDialog does a call to a nodejs fs function. In order to fix this, I need the require('electron') to be ignored by webpack.

There are 2 ways to do this. In "webpack-build-common", 1. target: 'electron-renderer' This basically adds a bunch of 'externals' that webpack ignores for packing. You can see the code here: https://github.com/webpack/webpack/blob/master/lib/WebpackOptionsApply.js#L70-L185

This does fix the issue, however, there are cases where I would like to be able to add other externals, such as the npm package "drivelist" which uses a "child_process" to get a list of drives connected to the system. It doesn't work properly if it is bundled by webpack.

So in order for me to use both electron AND drivelist, i added the following to "webpack-build-common"

externals: { 'electron' : 'commonjs electron', 'drivelist' : 'commonjs ' + nodeModules + '/drivelist' }

So ideally, I need a way to add target and/or externals to the webpack build.

gregvis avatar Jan 25 '17 16:01 gregvis

I added this inside app.component.ts and I can now access fs and electron.

const fs = (<any>window).require("fs");
const electron = (<any>window).require("electron");

Uncaught TypeError: window.require is not a function at Module../src/app/app.component.ts (app.component.ts:9) at webpack_require (bootstrap:78) at Module../src/app/app.module.ts (app.component.ts:21) at webpack_require (bootstrap:78) at Module../src/main.ts (main.ts:1) at webpack_require (bootstrap:78) at Object.0 (main.ts:12) at webpack_require (bootstrap:78) at checkDeferredModules (bootstrap:45) at Array.webpackJsonpCallback [as push] (bootstrap:32)

Angular CLI: 6.0.8 Node: 10.15.0 OS: win32 x64 Angular: 5.0.3 ... animations, common, compiler, compiler-cli, core, forms ... http, language-service, platform-browser ... platform-browser-dynamic, router

Package Version

@angular-devkit/architect 0.6.8 @angular-devkit/build-angular 0.12.2 @angular-devkit/build-optimizer 0.12.2 @angular-devkit/build-webpack 0.12.2 @angular-devkit/core 0.6.8 @angular-devkit/schematics 0.6.8 @angular/cdk 5.0.0-rc.1 @angular/cli 6.0.8 @angular/material 5.0.0-rc.1 @ngtools/webpack 7.2.2 @schematics/angular 0.6.8 @schematics/update 0.6.8 rxjs 5.5.2 typescript 2.4.2 webpack 4.28.4

saurabBajgain avatar Aug 07 '19 02:08 saurabBajgain

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.