<< return Webpack note about Angular Electron
Using Webpack with Electron-Angular project is the recommended approach, especially as your application grows in complexity. It provides benefits like proper module resolution, code optimization, and handling various asset types. Its the ideal way for larger Electron projects (or any project using more advanced module features) as this will make module resolution automatic, reduce bundle sizes for distributable applications, handle all web asset types including images, and in general make the application a lot more robust. While this has a small initial setup cost, the gains are substantial for long-term development, maintainability, and optimized production builds, especially in complex Electron projects. Follow the steps I provided earlier for Webpack setup. So:
(1) Install Webpack:
# npm install --save-dev webpack webpack-cli webpack-merge ts-loader html-webpack-plugin mini-css-extract-plugin css-loader copy-webpack-plugin
(2) Change TSC config, remove "outFile", see more about TSV options in TscCompilerOptions
(3) Create webpack.config.js
1: content_copyaddcompare_arrowsopen_in_full
2: const path = require('path');
3:
4: module.exports = {
5: mode: 'development', // Or 'production' when building for prod
6: target: 'electron-main', // Target Electron's main process
7: entry: './src/electron-main.ts', // Electron main process entry
8: devtool: 'source-map',
9: output: {
10: path: path.resolve(__dirname, 'dist'),
11: filename: 'main.js', // Output filename for main process
12: },
13: module: {
14: rules: [
15: {
16: test: /\.ts$/,
17: use: 'ts-loader',
18: exclude: /node_modules/,
19: },
20: ],
21: },
22: resolve: {
23: extensions: ['.ts', '.js'],
24: },
25: };
(4) Add webpack.config.prod.js in the project's root
1: const { merge } = require('webpack-merge');
2: const baseConfig = require('./webpack.config.js');
3:
4: module.exports = merge(baseConfig, {
5: mode: 'production',
6:
7: });
8:
9: Use code with care. Learn more
10: And add webpack.config.dev.js in the project's root. This will be used for dev builds.
11: content_copyaddcompare_arrowsopen_in_full
12: const { merge } = require('webpack-merge');
13: const baseConfig = require('./webpack.config.js');
14:
15: module.exports = merge(baseConfig, {
16: mode: 'development',
17: devtool: 'inline-source-map',
18: });
(5) Modify package.json scripts to use Webpack
1: "scripts": {
2: // ...other scripts
3:
4: "electron:start": "npx cross-env NODE_ENV=development webpack --config webpack.config.dev.js && electron .",
5: "electron:build": "webpack --config webpack.config.prod.js && electron ."
6: },
7: "main": "dist/main.js", // update the main entry point for electron to point to the dist folder
(6) Build Angular separately: You'll still build your Angular app separately using ng build. Electron loads the built Angular app. You can add a script in package.json that builds both at the same time to improve workflow.
Electron context:
- (2024) Electron video player with cashing and CSP policy #Electron #Video
- (2024) My workable project template for Angular Electron #Angular #Electron
- (2024) Node PostProcessor step for Angular and Electron. #Angular #Electron #NodeBackend
- (2022) Electron learning #FrontLearning #Electron
- ...
- (2024) Browser Window Options in Electron application.
- (2024) Important Parameters in Electron application.
- (2024) Core Features and Concepts in Electron application.
- (2024) Crucial Programming Techniques in Electron application.
- (2024) Main Process vs. Renderer Process in Electron application.
- (2024) Inter-Process Communication in Electron application.
- (2024) Asynchronous Operations in Electron application.
- (2024) Databases in Electron application.
- (2024) MySQL and PostgreSQL integration in Electron application.
- (2024) LocalStorage in Electron application.
- (2024) Native Modules in Electron application.
- (2024) Electron APIs in Electron application.
- (2024) Multi-window Applications in Electron application.
- (2024) Packaging and Distribution in Electron application.
- (2024) Node Integration in Electron application.
- (2024) Memory Leaks in Electron application.
- (2024) Code Security in Electron application.
- (2024) Browser Automation in Electron application.
- (2024) Testing in Electron application.
- (2024) Monetization in Electron application.
AngularElectron context:
Front context:
- (2025) My new project with WebRTC, what is WebRTC? #Android #Front
- (2024) Encapsulate HTML, CSS, and JS to WebComponent. Shadow DOM. Example of my WebComponent in this blog. #Front
- (2024) My lecture about Javascript (Ukrainian language). #Front
- (2019) Inject OnBegin/OnSuccess/OnFailure custom Javascript code into ASP.NET Ajax tag by Bundle of jquery.unobtrusive-ajax.min.js #Front #JavascriptProjects
- (2017) My site with peer-to-peer communication based on Mizu-Voip library. #Front #Css #Voip
- (2017) My solution with Bootstrap modal window and Classic ASP.NET. #AspNetClassic #Front #Css
- (2016) SPA-page на Classic ASP.NET та jQuery. #Front #AspNetClassic
- (2015) Cropper світлин сайту. #Front #AspNetMvc #Css
- (2015) Перемикач мови для сайту. #Front #AspNetMvc
- (2012) Календарики (datapicker), применяемые мною на jQuery / MS AJAX / JavaScript / Flex / MONO. #Front
- (2012) Заполнение связанных списков на MS AJAX и jQuery. #Front #AspNetClassic
- (2012) Применение jQuery-UI-dialog в ASP.NET #Front #AspNetClassic
- (2011) Как с помощью jQuery сделать флеш-ролик резиновым #Front #Flex
- (2011) AJAX подсказка/автозаполнение на jQuery #Front
- (2010) Flex, Java, Angular, Android. #SectionFront
- (2009) Язва-скрипт в ASP.NET. #Front
- (2009) Счетчики на Web-страничках. #AspNetClassic #Front

|