Skip to content

Commit e1e3541

Browse files
Coly010FrozenPandaz
authored andcommitted
docs(angular): blog post for using angular with rspack (#30380)
- docs(angular): blog post for using angular with rspack - docs(angular): apply review feedback <!-- Please make sure you have read the submission guidelines before posting an PR --> <!-- https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr --> <!-- Please make sure that your commit message follows our format --> <!-- Example: `fix(nx): must begin with lowercase` --> <!-- If this is a particularly complex change or feature addition, you can request a dedicated Nx release for this pull request branch. Mention someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they will confirm if the PR warrants its own release for testing purposes, and generate it for you if appropriate. --> ## Current Behavior <!-- This is the behavior we have today --> ## Expected Behavior <!-- This is the behavior we should expect with the changes in this PR --> ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes # (cherry picked from commit 851138c)
1 parent b3dd5da commit e1e3541

File tree

4 files changed

+226
-0
lines changed

4 files changed

+226
-0
lines changed
Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
---
2+
title: 'Using Rspack with Angular'
3+
date: 2025-03-19
4+
slug: using-rspack-with-angular
5+
authors: [Colum Ferry]
6+
tags: [angular, webpack, rspack, nx]
7+
cover_image: /blog/images/2025-03-19/rspack.avif
8+
description: Learn about how and why to use Rspack with Angular thanks to Nx's efforts for supporting Rspack for Angular.
9+
---
10+
11+
Configuring your build tooling for [Angular](https://angular.dev) applications has always been a lesser concern for most Angular developers due to the abstractions that Angular created called `builders`. The underlying implementation details were hidden from the developer who only needed to run either `ng build` or `nx build`.
12+
Despite this, most Angular developers knew that it was originally [Webpack](https://webpack.js.org/) that was used to build their applications. This was a great solution at the time and it was even possible to extend their builds by leveraging custom webpack configurations and plugins.
13+
14+
Over time, as applications grew in size and complexity, it became clear that the inherit slowness with Webpack build speeds was becoming more and more of an issue for Angular developers.
15+
16+
The Angular Team decided to address this issue by creating a new build pipeline that leveraged [Esbuild](https://esbuild.github.io/).
17+
18+
> Esbuild brought much-needed performance improvements to Angular builds, but existing Webpack-based applications were left with either a difficult migration path or no clear upgrade strategy.
19+
20+
To this day, many existing Angular applications still rely on Webpack because they cannot readily replace their Webpack configurations and plugins with equivalent Esbuild plugins. Thus, they are continuing to fight with slow builds reducing their productivity.
21+
22+
## What about Rspack?
23+
24+
[Rspack](https://rspack.dev) is a high performance JavaScript bundler written in Rust. It offers strong compatibility with the Webpack ecosystem, allowing for almost seamless replacement of webpack, and provides lightning fast build speeds.
25+
26+
Because it supports the existing Webpack ecosystem, it provides an answer to teams that maintain Angular applications using Webpack and want to migrate to a faster build pipeline.
27+
28+
However, it is crucial to understand that Rspack is not _completely_ compatible with the Webpack ecosystem with some slight nuances and low-level api differences that prevent certain plugins and loaders from working out of the box.
29+
30+
One such example is the `AngularIvyPlugin` which is a Webpack plugin that is used to support Angular's Ivy compiler. Therefore, it was not possible to simply drop in Rpsack and expect it to work with Angular applications.
31+
Many people have tried to get Rspack working with Angular, but it has proven to be a challenge, with partial support and partial success being reported in the past but with a lot of limitations or actual performance degradations over the Webpack approach.
32+
33+
Previous attempts to get Rspack working with Angular focused on porting the Webpack-specific plugins, loaders and configurations to Rspack, either as-is or by reproducing them. This approach was never fully successful.
34+
35+
Instead, a new approach was needed to support Rspack with Angular. A closer examination of how Angular compiles and bundles the application was required.
36+
37+
## Introducing Angular Rspack
38+
39+
![Angular Rspack Logo](/blog/images/2025-03-19/angular-rspack-logo-small.avif)
40+
41+
Angular Rspack started in September 2024 after I spent way too long investigating and researching how exactly Angular compiles and bundles for both their Webpack support and Esbuild support.
42+
43+
Something that I kept coming back to was that any Rspack solution that relied too much on Angular's Webpack support had the chance of being dropped by the Angular team as they continue to build out incredible new features with Esbuild. Instead, I decided that replicating and utilizing the abstractions the Angular team provided for their Esbuild support had a much stronger chance of longevity.
44+
45+
But before we dive into the technical details, I am excited to announce that Angular Rspack is now being maintained by the Nx team and is available for use with Nx. This means that you can now use Rspack with Angular and Nx and enjoy the benefits of both.
46+
The new package is called [@nx/angular-rspack](https://www.npmjs.com/package/@nx/angular-rspack) and it is available on npm while the repository has been moved to [nrwl/angular-rspack](https://github.com/nrwl/angular-rspack).
47+
48+
Nx already supports and integrates with a wide variety of build tooling across the ecosystem - filling in the gaps where developer experience (DX) needs to be improved - which makes Nx the perfect fit for continuing to maintain and build out Angular Rspack.
49+
50+
The support is still currently experimental, however, it is in a state that may be sufficient for your current needs. We invite you to try it out and to let us know if you run into issues by raising issues on the [angular-rspack repo](https://github.com/nrwl/angular-rspack/issues/new).
51+
52+
There are some limitations and missing features that are currently being worked on and on the roadmap to support. They have been listed at the bottom of this article.
53+
54+
## Migrating from Angular Webpack to Angular Rspack
55+
56+
To make the migration process as smooth as possible, a new generator has been added to the `@nx/angular` package called `convert-to-rspack` which will help you migrate your Angular applications from Webpack to Rspack.
57+
58+
The steps are very simple:
59+
60+
1. Run `nx migrate latest` to update your workspace to the latest version of Nx.
61+
2. Run `nx g @nx/angular:convert-to-rspack` to migrate your Angular application to Rspack.
62+
63+
There is also a [guide in our documentation](/recipes/angular/rspack/migrate-from-webpack) that walks you through the process step-by-step.
64+
Even if you're currently using the Angular CLI, it's as simple as first running `npx nx init` in your workspace and then running `npx nx g convert-to-rspack`.
65+
66+
## Using Angular Rspack
67+
68+
You'll notice that after migrating to Angular Rspack your `build` and `serve` targets have been removed from your project, and are instead [inferred](/concepts/inferred-tasks) by the `@nx/rspack/plugin`.
69+
In addition, a new `rspack.config.ts` file has been created in your project which looks something like this:
70+
71+
```ts
72+
import { createConfig } from '@nx/angular-rspack';
73+
export default createConfig(
74+
{
75+
options: {
76+
root: __dirname,
77+
outputPath: {
78+
base: '../../dist/apps/app',
79+
},
80+
index: './src/index.html',
81+
browser: './src/main.ts',
82+
polyfills: ['zone.js'],
83+
tsConfig: './tsconfig.app.json',
84+
assets: [
85+
'./src/favicon.ico',
86+
'./src/assets',
87+
{
88+
input: './public',
89+
glob: '**/*',
90+
},
91+
],
92+
styles: ['./src/styles.scss'],
93+
scripts: [],
94+
},
95+
},
96+
{
97+
production: {
98+
options: {
99+
outputPath: {
100+
base: '../../dist/apps/app-prod',
101+
},
102+
index: './src/index.prod.html',
103+
browser: './src/main.prod.ts',
104+
tsConfig: './tsconfig.prod.json',
105+
},
106+
},
107+
}
108+
);
109+
```
110+
111+
{% callout type="deepdive" title="createConfig Information" %}
112+
The `createConfig` function is used to create an Rspack configuration object setup for Angular applications.
113+
You can read more about it [here](/nx-api/angular-rspack/documents/create-config).
114+
{% /callout %}
115+
116+
### Building and Serving your Application
117+
118+
You can now run `nx build app` and `nx serve app` to build and serve your application via Rspack.
119+
120+
```{% command="nx build app" %}
121+
> nx run app:build
122+
123+
> rspack build --node-env=production
124+
125+
● ━━━━━━━━━━━━━━━━━━━━━━━━━ (100%) emitting after emit browser:
126+
127+
128+
browser compiled successfully in 2.32 s
129+
```
130+
131+
```{% command="nx serve app" %}
132+
> nx run app:serve
133+
134+
> rspack serve --node-env=development
135+
136+
<i> [webpack-dev-server] [HPM] Proxy created: /api -> http://localhost:3000
137+
<i> [webpack-dev-server] Project is running at:
138+
<i> [webpack-dev-server] Loopback: http://127.0.0.1:8080/
139+
<i> [webpack-dev-server] Content not from webpack is served from '/Users/columferry/dev/nrwl/issues/rspack-angular/ng-rspack/e2e/fixtures/rspack-csr-css/public' directory
140+
<i> [webpack-dev-server] 404s will fallback to '/index.html'
141+
Listening on port: 8080
142+
● ━━━━━━━━━━━━━━━━━━━━━━━━━ (100%) emitting after emit browser:
143+
browser compiled successfully in 2.33 s
144+
```
145+
146+
### Using a configuration
147+
148+
To run the build with the `production` configuration:
149+
150+
```bash
151+
NGRS_CONFIG=production nx build app
152+
```
153+
154+
`NGRS_CONFIG` is an environment variable that you can use to specify which configuration to use. If the environment variable is not set, the `production` configuration is used by default.
155+
156+
## Creating a new Angular Rspack application
157+
158+
We currently do not have a generator for creating a new Angular Rspack application, but we will soon and it will be available under a `--bundler=rspack` option on the `@nx/angular:application` generator.
159+
160+
However, you can still create a new Angular Rspack application by running the following commands:
161+
162+
```shell
163+
nx g @nx/angular:application myapp --bundler=webpack
164+
nx g @nx/angular:convert-to-rspack myapp
165+
```
166+
167+
## Benchmarks
168+
169+
![Benchmarks](/blog/images/2025-03-19/bundler-build-times.avif)
170+
171+
Below is a table of benchmarks for the different bundlers available, run against an application consisting of ~800 lazy loaded routes with ~10 components each - totaling ~8000 components.
172+
173+
**System Info**
174+
175+
- MacBook Pro (macOS 15.3.1)
176+
- Processor: M2 Max
177+
- Memory: 96 GB
178+
- `@nx/angular-rspack` version: 20.6.2
179+
- Angular version: ~19.2.0
180+
181+
| Build/Bundler | Prod SSR (s) | Prod (s) | Dev (s) |
182+
| ------------- | ------------ | -------- | ------- |
183+
| Webpack | 198.614 | 154.339 | 159.436 |
184+
| esbuild | 23.701 | 19.569 | 15.358 |
185+
| Rsbuild | 23.949 | 20.490 | 18.209 |
186+
| Rspack | 30.589 | 19.269 | 19.940 |
187+
188+
> You can find the benchmarks and run them yourself: [https://github.com/nrwl/ng-bundler-benchmark](https://github.com/nrwl/ng-bundler-benchmark)
189+
190+
As can be seen by the benchmarks above, Rspack is significantly faster than Webpack and very close to Esbuild.
191+
192+
Given that the primary goal for Angular Rspack is to provide a faster build system for Angular Webpack applications while supporting their existing Webpack configurations and plugins, we are confident that Angular Rspack will be a great choice for teams that want to migrate to a faster build system.
193+
194+
## Known Limitations and Missing Features
195+
196+
The following are known limitations and missing features of Angular Rspack:
197+
198+
- Static Site Generation (SSG) is not supported.
199+
- Angular's built-in support for Internationalization (i18n) is not supported.
200+
- Server Routing is not supported - still experimental in Angular currently.
201+
- App Engine APIs are not supported - still experimental in Angular currently.
202+
- Optimization is not currently 1:1 with Angular's optimization - however, there are still great optimizations that are made.
203+
- Styles optimization for `inline-critical` and `remove-special-comments` are not yet implemented.
204+
- Inlining of fonts is not yet implemented.
205+
- Web Workers are not fully supported.
206+
- Hot Module Replacement (HMR) is partially supported.
207+
208+
If you have any other missing features or limitations, please [let us know](https://github.com/nrwl/angular-rspack/issues/new).
209+
210+
## What's Next?
211+
212+
We are actively working on improving the experience, stability and performance of Angular Rspack. Our next steps will revolve around getting to feature parity with Angular's build system - addressing the items listed above to achieve this.
213+
214+
The `@nx/angular` plugin will also be updated to support generating new Angular Rspack applications as well as supporting Rspack Module Federation with Angular.
215+
216+
Exciting times ahead! You can follow our progress by starring the [Angular Rspack repository](https://github.com/nrwl/angular-rspack) and following us on [X](https://X.com/nxdevtools).
217+
218+
## Further Reading
219+
220+
- [Nx Angular Rspack](/recipes/angular/rspack/introduction)
221+
- [Angular](https://angular.dev)
222+
- [Rspack](https://rspack.dev)
223+
- 🧠 [Nx Docs](/getting-started/intro)
224+
- 👩‍💻 [Nx GitHub](https://github.com/nrwl/nx)
225+
- 💬 [Nx Official Discord Server](https://go.nx.dev/community)
226+
- 📹 [Nx Youtube Channel](https://www.youtube.com/@nxdevtools)
Binary file not shown.
Binary file not shown.
17.3 KB
Binary file not shown.

0 commit comments

Comments
 (0)