Table of Contents
1 Reasons and solutions for Angular 13 development mode being too slow
aot, pug compiles and reports an error
project construction There is a requirement for custom
First you need to configure build support for custom webpack configuration. Execute the following command to add dependencies:
New
3 小结
Home Web Front-end JS Tutorial What should I do if Angular13+ development mode is too slow? Causes and solutions

What should I do if Angular13+ development mode is too slow? Causes and solutions

Dec 22, 2022 pm 09:15 PM
javascript angular

Angular13 What should I do if the development mode is too slow? The following article will introduce to you the reasons why Angular 13 development mode is too slow and how to optimize build performance. I hope it will be helpful to you!

What should I do if Angular13+ development mode is too slow? Causes and solutions

1 Reasons and solutions for Angular 13 development mode being too slow

Recently worked on a high-frequency Angular project for seven years After upgrading to Angular 13, its development mode has slow construction speed, high resource usage, and poor development experience. When launching a build on a Macbook air that I only use occasionally for meetings (and has recently become my primary productivity tool during the work-from-home period), its fans whir, the CPU is maxed out, and After the build is completed, a hot update takes more than one minute. [Related tutorial recommendations: "angular Tutorial"]

After analyzing and troubleshooting various reasons, we finally found the schema(. /node_modules/@angular/cli/lib/config/schema.json), and combined with Angular 12 release document, the specific cause was located: A major change in Angular 12 is to aot, buildOptimizer, optimization and other parameters have been changed from the default value false to true.

A number of browser and server builder options have had their default values ​​changed. The aim of these changes is to reduce the configuration complexity and support the new "production builds by default" initiative.

You can see that the default production mode after Angular 12 is quite tricky for cross-version upgrades. We can learn the details of the changes from this submission:
656f8d7

1.1 Solve the problem of slow development mode of Angular 12

The solution is in

development

Disable configuration items related to production mode in the configuration. Example: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>{ &quot;$schema&quot;: &quot;./node_modules/@angular/cli/lib/config/schema.json&quot;, &quot;projects&quot;: { &quot;front&quot;: { &quot;architect&quot;: { &quot;build&quot;: { &quot;configurations&quot;: { &quot;development&quot;: { &quot;tsConfig&quot;: &quot;./tsconfig.dev.json&quot;, &quot;aot&quot;: false, &quot;buildOptimizer&quot;: false, &quot;optimization&quot;: false, &quot;extractLicenses&quot;: false, &quot;sourceMap&quot;: true, &quot;vendorChunk&quot;: true, &quot;namedChunks&quot;: true } } }, } }, &quot;defaultProject&quot;: &quot;front&quot; }</pre><div class="contentsignin">Copy after login</div></div>Note

aot

When it is turned on and off, there may be some differences in the performance of the build results, which need to be analyzed based on the specific problem.

1.2 Problem: After opening

aot, pug compiles and reports an error

pug

is used in this project Develop html content. The build is normal when aot is turned off, but an error will be reported when turned on. Perform debugger debugging based on the error content and location. You can see that the compilation result is an esModule object. This is due to the use of

raw-loader

, whose compilation result defaults to esModule mode, just disable the esModule configuration item. Example (for custom webpack configuration, please refer to the dll configuration related examples below): <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>{ test: /\.pug$/, use: [ { loader: &amp;#39;raw-loader&amp;#39;, options: { esModule: false, }, }, { loader: &amp;#39;pug-html-loader&amp;#39;, options: { doctype: &amp;#39;html&amp;#39;, }, }, ], },</pre><div class="contentsignin">Copy after login</div></div>

2 Further optimization: Angular custom webpack configuration dll supports

project construction There is a requirement for custom

webpack

configuration, which is implemented using the @angular-builders/custom-webpack library, but no dll is configured.

Angular

provides the vendorChunk parameter. Turning it on will extract public resources such as dependencies in package.json into independent chunks. It can well solve the problem of hot update bundles being too large, causing hot update to be too slow, etc., but there is still a high memory usage, and in the actual comparison test, in the presence of webpack5 cache, it is better than the dll mode build The compilation speed and hot update speed are slightly slower. Therefore, when the performance of the development machine is average, configuring the dll in the development mode will bring certain benefits.

2.1 Angular supports custom webpack configuration

First you need to configure build support for custom webpack configuration. Execute the following command to add dependencies:

npm i -D @angular-builders/custom-webpack
Copy after login

Modify

angluar.json

configuration. Content format reference: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>{ &quot;$schema&quot;: &quot;./node_modules/@angular/cli/lib/config/schema.json&quot;, &quot;cli&quot;: { &quot;analytics&quot;: false, &quot;cache&quot;: { &quot;path&quot;: &quot;node_modules/.cache/ng&quot; } }, &quot;version&quot;: 1, &quot;newProjectRoot&quot;: &quot;projects&quot;, &quot;projects&quot;: { &quot;front&quot;: { &quot;root&quot;: &quot;&quot;, &quot;sourceRoot&quot;: &quot;src&quot;, &quot;projectType&quot;: &quot;application&quot;, &quot;prefix&quot;: &quot;app&quot;, &quot;schematics&quot;: { &quot;@schematics/angular:component&quot;: { &quot;style&quot;: &quot;less&quot; } }, &quot;architect&quot;: { &quot;build&quot;: { &quot;builder&quot;: &quot;@angular-builders/custom-webpack:browser&quot;, &quot;options&quot;: { &quot;customWebpackConfig&quot;: { &quot;path&quot;: &quot;./webpack.config.js&quot; }, &quot;indexTransform&quot;: &quot;scripts/index-html-transform.js&quot;, &quot;outputHashing&quot;: &quot;media&quot;, &quot;deleteOutputPath&quot;: true, &quot;watch&quot;: true, &quot;sourceMap&quot;: false, &quot;outputPath&quot;: &quot;dist/dev&quot;, &quot;index&quot;: &quot;src/index.html&quot;, &quot;main&quot;: &quot;src/app-main.ts&quot;, &quot;polyfills&quot;: &quot;src/polyfills.ts&quot;, &quot;tsConfig&quot;: &quot;./tsconfig.app.json&quot;, &quot;baseHref&quot;: &quot;./&quot;, &quot;assets&quot;: [ &quot;src/assets/&quot;, { &quot;glob&quot;: &quot;**/*&quot;, &quot;input&quot;: &quot;./node_modules/@ant-design/icons-angular/src/inline-svg/&quot;, &quot;output&quot;: &quot;/assets/&quot; } ], &quot;styles&quot;: [ &quot;node_modules/angular-tree-component/dist/angular-tree-component.css&quot;, &quot;src/css/index.less&quot; ], &quot;scripts&quot;: [] }, &quot;configurations&quot;: { &quot;development&quot;: { &quot;tsConfig&quot;: &quot;./tsconfig.dev.json&quot;, &quot;buildOptimizer&quot;: false, &quot;optimization&quot;: false, &quot;aot&quot;: false, &quot;extractLicenses&quot;: false, &quot;sourceMap&quot;: true, &quot;vendorChunk&quot;: true, &quot;namedChunks&quot;: true, &quot;scripts&quot;: [ { &quot;inject&quot;: true, &quot;input&quot;: &quot;./dist/dll/dll.js&quot;, &quot;bundleName&quot;: &quot;dll_library&quot; } ] }, &quot;production&quot;: { &quot;outputPath&quot;: &quot;dist/prod&quot;, &quot;baseHref&quot;: &quot;./&quot;, &quot;watch&quot;: false, &quot;fileReplacements&quot;: [ { &quot;replace&quot;: &quot;src/environments/environment.ts&quot;, &quot;with&quot;: &quot;src/environments/environment.prod.ts&quot; } ], &quot;optimization&quot;: { &quot;scripts&quot;: true, &quot;styles&quot;: { &quot;minify&quot;: true, &quot;inlineCritical&quot;: false }, &quot;fonts&quot;: true }, &quot;outputHashing&quot;: &quot;all&quot;, &quot;sourceMap&quot;: false, &quot;namedChunks&quot;: false, &quot;aot&quot;: true, &quot;extractLicenses&quot;: false, &quot;vendorChunk&quot;: false, &quot;buildOptimizer&quot;: true } }, &quot;defaultConfiguration&quot;: &quot;production&quot; }, &quot;serve&quot;: { &quot;builder&quot;: &quot;@angular-builders/custom-webpack:dev-server&quot;, &quot;options&quot;: { &quot;browserTarget&quot;: &quot;front:build&quot;, &quot;liveReload&quot;: false, &quot;open&quot;: false, &quot;host&quot;: &quot;0.0.0.0&quot;, &quot;port&quot;: 3002, &quot;servePath&quot;: &quot;/&quot;, &quot;publicHost&quot;: &quot;localhost.gf.com.cn&quot;, &quot;proxyConfig&quot;: &quot;config/ngcli-proxy-config.js&quot;, &quot;disableHostCheck&quot;: true }, &quot;configurations&quot;: { &quot;production&quot;: { &quot;browserTarget&quot;: &quot;front:build:production&quot; }, &quot;development&quot;: { &quot;browserTarget&quot;: &quot;front:build:development&quot; } }, &quot;defaultConfiguration&quot;: &quot;development&quot; }, &quot;test&quot;: { &quot;builder&quot;: &quot;@angular-builders/custom-webpack:karma&quot;, &quot;options&quot;: { &quot;customWebpackConfig&quot;: { &quot;path&quot;: &quot;./webpack.test.config.js&quot; }, &quot;indexTransform&quot;: &quot;scripts/index-html-transform.js&quot;, &quot;main&quot;: &quot;src/ngtest.ts&quot;, &quot;polyfills&quot;: &quot;src/polyfills.ts&quot;, &quot;tsConfig&quot;: &quot;./tsconfig.spec.json&quot;, &quot;karmaConfig&quot;: &quot;./karma.conf.js&quot;, &quot;assets&quot;: [ &quot;src/assets/&quot;, { &quot;glob&quot;: &quot;**/*&quot;, &quot;input&quot;: &quot;./node_modules/@ant-design/icons-angular/src/inline-svg/&quot;, &quot;output&quot;: &quot;/assets/&quot; } ], &quot;styles&quot;: [ &quot;node_modules/angular-tree-component/dist/angular-tree-component.css&quot;, &quot;src/css/index.less&quot; ], &quot;scripts&quot;: [] } } } } }, &quot;defaultProject&quot;: &quot;front&quot;, &quot;schematics&quot;: { &quot;@schematics/angular:module&quot;: { &quot;routing&quot;: true, &quot;spec&quot;: false }, &quot;@schematics/angular:component&quot;: { &quot;flat&quot;: false, &quot;inlineStyle&quot;: true, &quot;inlineTemplate&quot;: false } } }</pre><div class="contentsignin">Copy after login</div></div> This example involves many custom configuration contents. You should mainly pay attention to the webpack-related parts. Other contents can be compared and referenced based on the specific situation of your own project. For some details, you can also refer to the practical introduction in this previous article:

lzw.me/a/update-to…

2.2 Configuring webpack dll support for Angular

New

webpack.config.js

file. Content reference: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>const { existsSync } = require(&amp;#39;node:fs&amp;#39;); const { resolve } = require(&amp;#39;node:path&amp;#39;); const webpack = require(&amp;#39;webpack&amp;#39;); // require(&amp;#39;events&amp;#39;).EventEmitter.defaultMaxListeners = 0; /** * @param {import(&amp;#39;webpack&amp;#39;).Configuration} config * @param {import(&amp;#39;@angular-builders/custom-webpack&amp;#39;).CustomWebpackBrowserSchema} options * @param {import(&amp;#39;@angular-builders/custom-webpack&amp;#39;).TargetOptions} targetOptions */ module.exports = (config, options, targetOptions) =&gt; { if (!config.devServer) config.devServer = {}; config.plugins.push( new webpack.DefinePlugin({ LZWME_DEV: config.mode === &amp;#39;development&amp;#39; }), ); const dllDir = resolve(__dirname, &amp;#39;./dist/dll&amp;#39;); if ( existsSync(dllDir) &amp;&amp; config.mode === &amp;#39;development&amp;#39; &amp;&amp; options.scripts?.some((d) =&gt; d.bundleName === &amp;#39;dll_library&amp;#39;) ) { console.log(&amp;#39;use dll:&amp;#39;, dllDir); config.plugins.unshift( new webpack.DllReferencePlugin({ manifest: require(resolve(dllDir, &amp;#39;dll-manifest.json&amp;#39;)), context: __dirname, }) ); } config.module.rules = config.module.rules.filter((d) =&gt; { if (d.test instanceof RegExp) { // 使用 less,移除 sass/stylus loader return !(d.test.test(&amp;#39;x.sass&amp;#39;) || d.test.test(&amp;#39;x.scss&amp;#39;) || d.test.test(&amp;#39;x.styl&amp;#39;)); } return true; }); config.module.rules.unshift( { test: /\.pug$/, use: [ { loader: &amp;#39;raw-loader&amp;#39;, options: { esModule: false, }, }, { loader: &amp;#39;pug-html-loader&amp;#39;, options: { doctype: &amp;#39;html&amp;#39;, }, }, ], }, { test: /\.html$/, loader: &amp;#39;raw-loader&amp;#39;, exclude: [helpers.root(&amp;#39;src/index.html&amp;#39;)], }, { test: /\.svg$/, loader: &amp;#39;raw-loader&amp;#39;, }, { test: /\.(t|les)s/, loader: require.resolve(&amp;#39;@lzwme/strip-loader&amp;#39;), exclude: /node_modules/, options: { disabled: config.mode !== &amp;#39;production&amp;#39;, }, } ); // AngularWebpackPlugin,用于自定义 index.html 处理插件 const awPlugin = config.plugins.find((p) =&gt; p.options?.hasOwnProperty(&amp;#39;directTemplateLoading&amp;#39;)); if (awPlugin) awPlugin.pluginOptions.directTemplateLoading = false; // 兼容上古遗传逻辑,禁用部分插件 config.plugins = config.plugins.filter((plugin) =&gt; { const pluginName = plugin.constructor.name; if (/CircularDependency|CommonJsUsageWarnPlugin/.test(pluginName)) { console.log(&amp;#39;[webpack][plugin] disabled: &amp;#39;, pluginName); return false; } return true; }); // console.log(&amp;#39;[webpack][config]&amp;#39;, config.mode, config, options, targetOptions); return config; };</pre><div class="contentsignin">Copy after login</div></div>Create a new

webpack.dll.mjs

file for dll building. Content example: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>import { join } from &amp;#39;node:path&amp;#39;; import webpack from &amp;#39;webpack&amp;#39;; const rootDir = process.cwd(); const isDev = process.argv.slice(2).includes(&amp;#39;--dev&amp;#39;) || process.env.NODE_ENV === &amp;#39;development&amp;#39;; /** @type {import(&amp;#39;webpack&amp;#39;).Configuration} */ const config = { context: rootDir, mode: isDev ? &amp;#39;development&amp;#39; : &amp;#39;production&amp;#39;, entry: { dll: [ &amp;#39;@angular/common&amp;#39;, &amp;#39;@angular/core&amp;#39;, &amp;#39;@angular/forms&amp;#39;, &amp;#39;@angular/platform-browser&amp;#39;, &amp;#39;@angular/platform-browser-dynamic&amp;#39;, &amp;#39;@angular/router&amp;#39;, &amp;#39;@lzwme/asmd-calc&amp;#39;, // more... ], }, output: { path: join(rootDir, &amp;#39;dist/dll&amp;#39;), filename: &amp;#39;dll.js&amp;#39;, library: &amp;#39;[name]_library&amp;#39;, }, plugins: [ new webpack.DllPlugin({ path: join(rootDir, &amp;#39;dist/dll/[name]-manifest.json&amp;#39;), name: &amp;#39;[name]_library&amp;#39;, }), new webpack.IgnorePlugin({ resourceRegExp: /^\.\/locale$/, contextRegExp: /moment$/, }), ], cache: { type: &amp;#39;filesystem&amp;#39; }, }; webpack(config).run((err, result) =&gt; { console.log(err ? `Failed!` : `Success!`, err || `${result.endTime - result.startTime}ms`); });</pre><div class="contentsignin">Copy after login</div></div><p>在 <code>angular.json 中添加 dll.js 文件的注入配置,可参考前文示例中 development.scripts 中的配置内容格式。

package.json 中增加启动脚本配置。示例:

{
    "scripts": {
        "ng:serve": "node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng serve",
        "dll": "node config/webpack.dll.mjs",
        "dev": "npm run dll -- --dev && npm run ng:serve -- -c development",
    }
}
Copy after login

最后,可执行 npm run dev 测试效果是否符合预期。

3 小结

angular-cli 在升级至 webpack 5 以后,基于 webpack 5 的缓存能力做了许多编译优化,一般情况下开发模式二次构建速度相比之前会有大幅的提升。但是相比 snowpackvite 一类的 esm no bundles 方案仍有较大的差距。其从 Angular 13 开始已经在尝试引入 esbuild,但由于其高度定制化的构建逻辑适配等问题,对一些配置参数的兼容支持相对较为复杂。在 Angular 15 中已经可以进行生产级配置尝试了,有兴趣也可作升级配置与尝试。

更多编程相关知识,请访问:编程教学!!

The above is the detailed content of What should I do if Angular13+ development mode is too slow? Causes and solutions. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

How to install Angular on Ubuntu 24.04 How to install Angular on Ubuntu 24.04 Mar 23, 2024 pm 12:20 PM

Angular.js is a freely accessible JavaScript platform for creating dynamic applications. It allows you to express various aspects of your application quickly and clearly by extending the syntax of HTML as a template language. Angular.js provides a range of tools to help you write, update and test your code. Additionally, it provides many features such as routing and form management. This guide will discuss how to install Angular on Ubuntu24. First, you need to install Node.js. Node.js is a JavaScript running environment based on the ChromeV8 engine that allows you to run JavaScript code on the server side. To be in Ub

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

Angular components and their display properties: understanding non-block default values Angular components and their display properties: understanding non-block default values Mar 15, 2024 pm 04:51 PM

The default display behavior for components in the Angular framework is not for block-level elements. This design choice promotes encapsulation of component styles and encourages developers to consciously define how each component is displayed. By explicitly setting the CSS property display, the display of Angular components can be fully controlled to achieve the desired layout and responsiveness.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

See all articles