-
Notifications
You must be signed in to change notification settings - Fork 12k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for Electron and Externals #4227
Comments
I know this isn't the answer you want to hear, but this isn't a usecase we want to support right now. I know webpack itself supports many targets but on the CLI, at the moment, we don't. For your usecase I would say using a custom webpack config would be better. You can still use the CLI as a generator, and use https://github.com/angular/angular-cli/tree/master/packages/%40ngtools/webpack to get AoT builds. |
I maked a repository with a temporaly solucion and example in https://github.com/vilarone/ng2-electron-builder 2 month ago. Its use a folked angular-cli with target parameter in angular-cli.json |
I recently stumbled upon this problem myself, electron renderers written using angular-cli does not work with native Node.js modules such as A temporary hack that works for me is to have another JS file responsible to attaching your external modules to the renderer's global window.fs = require('fs');
window.electron = require('electron'); Once you have included that JS file in the |
After using the @morcerf solution, i also needed, to pass compilation, to put in my typings.d.ts : /// <reference path="../node_modules/electron/electron.d.ts"/>
declare var electron: Electron.AllElectron; |
@morcerf can you be more clear with your solution? Like where to add it with syntax to angular-cli.json? |
@premiumwd For me, creating a modules.js file with morcerf's code, and adding it at the top of my scripts of my index.html, worked. |
@premiumwd yep of course, hope this can be more helpful. For example, your project needs the You will need to create a file (for example it is named
Once you that file, you can add this part into your
alternatively you can also do a Once you have done 1 of these approaches, Note that I haven't migrated my electron code to Typescript. So if you use Typescript, you might have to do some type declaration to make it happy as @Litarvan also suggested above. |
I have created a plugin/extention for |
Adding these to your polyfills.ts will stop the typescript compiler from complaining
|
I've found some ugly but workable solution here
Then in TS you can declare electron and access any method/property:
After that you can do such things:
|
The only workable solution I've found here: Angular 2 + Electron + Angular-CLI I managed to access electron API by adding following to index.html:
Then in TS you can declare electron and access any method/property:
This is very hackish solution and doesn't solve problem of os.platform() being browser. |
@Eshva it works. But still some electron features are not working like |
@Eshva it works!!!! Now i cant use plugin sql.js, its writes the same sqlite file after modify local db to sync (export) with sqlite file, that prevents the app to refresh infinitily. |
@Eshva you saved my day!!! |
I added this inside
|
I'm also getting this with mammoth .docx HTML converter (https://github.com/mwilliamson/python-mammoth):
|
@ghuser it worked! thanks! |
All, I spent a couple of days reviewing the cli codebase. The build system is very extensible. I ended up creating a new npm package named ng-electron-devkit that extends angular 6 build system to support electron-renderer. My goal is to add support for electron main process and packaging electron assets in the build pipeline in future updates. This is not a hack to the CLI. You can target both electron-renderer and the browser and have completely different build settings! |
I am currently using a slightly different approach and it worked for me. In your typings.d.ts :
To me this is clean enough: no stange scripts in your html, no imported JavaScript, no third-party libraries. And the modules listed in your typings.d.ts get typed, too. I'm attaching a screenshot of the result/usage. PS: I tested it with typescript 2.9 and angular 6+ only. |
Uncaught TypeError: window.require is not a function Angular CLI: 6.0.8 Package Version@angular-devkit/architect 0.6.8 |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
OS?
Versions.
Repro steps.
The log given by the failure.
Mention any other details that might be useful.
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.
The text was updated successfully, but these errors were encountered: