Skip to content

Transition Angular from the whole-program compilation to localized compilationΒ #43165

Closed

Description

@IgorMinar

Note 1: This proposal has its origin in Out-of-band type-checking for TypeScript and Template compilation which mentions an "extreme" implementation option β€” this issue provides more details about this option.

Note 2: πŸ“Ί Check out the video recording of a community discussion about this proposal @ angularnation.net

The problem

As described in the Out-of-band type-checking for TypeScript and Template compilation proposal, making Angular's compilation model highly parallelizable would result in major benefits for Angular developers by significantly lowering the interactive workflow latency (making the "edit & refresh" cycle faster).

This change would additionally improve the efficiency (in time, and CPU cost) of building Angular at scale in monorepos and on CI systems.

The current cost of compilation is significant, and becoming a hurdle affecting DX, and our ability to scale massive code bases.

Background info

The unit of parallelization of Angular's compilation is the "program" as defined by the TypeScript compiler β€” the program is the smallest unit of code (a group of .ts, .d.ts, and .js files declared and configured by a single tsconfig.json file) that the TypeScript compiler can process independently of other code.

A typical Angular compilation unit is a program that is composed of several (often many) components, directives, pipes, services, NgModules, and generic TypeScript files.

A small application is commonly built as a single program, that often depends on 3rd party Angular libraries (that themselves were compiled as individual programs). Larger code bases, split up their app into multiple programs using Angular CLI workspaces, Bazel's rules_nodejs (or the ng_module rule), or Nx.

These approaches to scaling help, but don't solve the ultimate problem, which is that it is currently not possible to process a single Angular Component and transform it to runnable JS without processing a bigger chunk of the code base as well (the whole program). This prevents us from taking advantage of massive parallelization that modern hardware in combination with highly parallelizable transpilation engines like esbuild, (and Vite built on top of it) and Bazel offer. This results in wasted human and CPU time and ultimately in loss of focus, creativity, and high CI bills.

Proposed solution

Let's transition the processing model of Angular code from the "whole world" compilation model (global optimization), to localized compilation (and as close to transpilation as possible). In an extreme example, this would mean that a single .ts file with an Angular component could be converted to .json its own without having to read or process any other files.

In practice, it's more likely that we'll need to read and process several files around this single .ts file because of references to external templates and style sheets, and we'll need the imported NgModules and (upcoming "standalone components, directives, and pipes" β€” check out the RFC) to be at least read, if not compiled as well, because they represent transitive dependencies of the component being processed.

Aiming for purely syntactic transpilation of template and metadata to JavaScript code would most likely be a "bridge too far" (see "Alternatives considered" below), but getting as close as possible to that ideal is at the core of this proposal.

We don't need purely syntactic transpilation to still reap most of the benefits of parallelization, localized compilation would result in a huge improvement already.

Implementation details

  • Out-of-band type-checking for TypeScript and Template compilation is a most likely a prerequisite for this project, because we need to decouple type-checking from code generation.
  • Moving away from NgModules, to module-less, a.k.a. "standalone" components, directives, and pipes would provide us with focused understand the context within which a component or a directive are being compiled β€” fortunately we want to execute on this project regardless of this proposal in order to reduce verbosity and complexity of Angular, so this prerequisite is well aligned with our ongoing work.
  • We'd need to rethink our compiler architecture, and investigate if building it on top of the TypeScript AST is the right approach or if we should consider more lightweight alternatives. A lot of research in this area needs to be done to make the right call, but considering that we absolutely want to keep TypeScript for type-checking, IDE/Language Service support, etc, it might make a lot of sense to stick with it and find ways to make it fast enough.
  • Once the compiler supports localized compilation, we'd need to build plugins for esbuild or its alternative like swc.
  • Angular CLI's build pipeline would have to be revamped to support this processing model. We could even consider adopting Vite since they've done a lot of awesome work already πŸ‘πŸ».
  • The impact on this change on other parts of Angular, like the language-service, google3 integration, and others is less clear at the moment, and will need to be researched.

Trade offs

  • Switching to transpilation represents a radical shift for the design of both the Angular compiler and runtime, and has an impact on how type-checking should work. All of this represents a significant effort, which would take time.

Web ecosystem compatibility & Prior art

All of the following use transpilation or localized compilation approach:

What's the goal of this issue?

The goal is to write down some of the ideas that have been made possible thanks to Ivy and start collecting early feedback that could be used as an input for future RFC.

Alternatives considered

  • We could switch over to an existing templating solution that is transpilable (JSX, lit), but these systems have fundamentally different component composition & change detection models, which would result in a major breaking change and regressions in runtime performance in some scenarios (due to our inability to perform certain optimizations). This change would not be worth the trouble.

  • We could modify the template syntax to make it more suitable for purely syntactic transpilation (for example by removing selector matching)β€” while this might be worth doing in the future (pending more exploration of costs vs benefits), we can achieve most of the goals of this proposal by changing how Angular works internally while preserving backwards compatibility for most of the code out there and the investment into these code bases.

Activity

  1. added
    needs: discussion
    feature
    on Aug 15, 2021
  2. changed the title [-]Transition Angular from the whole-world compilation to localized compilation[/-] [+]Transition Angular from the program-based compilation to localized compilation[/+] on Aug 15, 2021
  3. SanderElias commented on Aug 16, 2021

    @SanderElias
    Contributor

    @IgorMinar To bad, I can only give a single thumbs-up πŸ˜‰

    Thinking along those lines, I think we can have a solution that will make the transition to the full "extreme performance" model easier.
    What if we create a tool (or more precisely extract this from the existing tools), that builds an in-memory (and persisting on disk) repository of all the meta-data the angular compiler needs? This thing can be updated when a unit gets compiled. In our current version, we need to do a "full-cycle" to populate this, but going forward this can become more and more granular.
    Having this lib in the memory means we can compile and typecheck a component/pipe/directive/module/whatever in relative isolation.

    I know this sounds simpler as it is, but it gives Angular a way to move slowly into the "transpile 1 file at a time" while keeping the backward compatibility.

  4. IgorMinar commented on Aug 16, 2021

    @IgorMinar
    ContributorAuthor

    @SanderElias my concern with cutting corners here is that it would come at a high resource usage and high complexity (just the cache invalidation alone would be tricky to get right). But it is an idea with exploring more. Thanks for sharing.

  5. added
    area: compiler
    on Aug 16, 2021
  6. added this to the Backlog milestone on Aug 16, 2021
  7. alxhub commented on Aug 16, 2021

    @alxhub
    Member

    What if we create a tool (or more precisely extract this from the existing tools), that builds an in-memory (and persisting on disk) repository of all the meta-data the angular compiler needs? This thing can be updated when a unit gets compiled. In our current version, we need to do a "full-cycle" to populate this, but going forward this can become more and more granular.
    Having this lib in the memory means we can compile and typecheck a component/pipe/directive/module/whatever in relative isolation.

    This is pretty much how the compiler's incremental build system (aka "watch mode") which powers ng serve currently functions. The main source of complexity here is that changes in Angular code are frequently non-local - editing an @NgModule for example can affect the compilation of components in many other files, depending on how that @NgModule is imported/re-exported across the program. So to accurately and efficiently react to a change in its input, the compiler has not only an in-memory "database" of all of the metadata for each class, but also understands the dependencies between these artifacts and how a change to one will propagate through the others. This is extremely nontrivial, but is necessary for reasonable incremental performance.

  8. changed the title [-]Transition Angular from the program-based compilation to localized compilation[/-] [+]Transition Angular from the whole-program compilation to localized compilation[/+] on Aug 16, 2021
  9. IgorMinar commented on Aug 16, 2021

    @IgorMinar
    ContributorAuthor

    @alxhub my hope is that standalone components/directives/pipes will make this problem much more attainable. Would you agree?

  10. alxhub commented on Aug 16, 2021

    @alxhub
    Member

    @IgorMinar the main benefit of standalone components is that the dependency graph can be extrapolated from the import graph, and no longer requires whole-program analysis to connect a component with its NgModule.

    In theory, an application composed entirely of standalone entities (no NgModules with declarations) could be compiled in parallel, with each compilation task only needing to explore a subset of the whole program to identify dependencies. This exploration would still be necessary due to selector matching and other similar requirements (like the need to distinguish directives from pipes). In any complex program there would be a good bit of overlap, with shared components needing to be explored by multiple compilation tasks. Some kind of queueing and coordination scheme could be used to eliminate this duplicated effort, with a sophisticated enough compiler architecture.

  11. IgorMinar commented on Aug 16, 2021

    @IgorMinar
    ContributorAuthor

    @alxhub yup. Exactly! I think we are on the same page. I have no expectations that this would be a simple solution, but a lot of it could be reused from the incremental complication we already support.

  12. LanderBeeuwsaert commented on Aug 19, 2021

    @LanderBeeuwsaert

    Not knowing much about the internals or details of compilation, even from the start that I learned about IVY, where it was presented that IVY would make components more "independent" (I don't remember the correct terminology anymore) from each other; I always had the idea/hope this would eventually result in the parallelization of component compilation.

    So I don't have anything useful to add except for the fact that it seems quite obviously (for an outsider with only a naive understanding of the complexity involved) the ultimate direction for the build process to evolve to.

    So two thumbs up indeed :)

  13. ssougnez commented on Aug 27, 2021

    @ssougnez

    Hello,

    I was about to create a new issue for a specific question then I found this one.
    I think that it might answer the question I have, so I'll ask it in here just for a confirmation :-)

    Basically, I'm working with a CMS that does not support Angular nor SPA so my current solution is to use angularjs as I'm able to drop components here and there in the CMS without having any issue.

    This is obviously not possible with Angular as you need a root component and that this one cannot use to project the content inserted inside it. However, from what I understand, the current issue relates to the fact to abandon the use of NgModule, so the question is: does that mean that I'll be able to drop component here in there without the need of a root one? If I understood correctly the whole discussion about "making components standalone and ngModules optional", I'll be able to reference the js files in the home page of the web site then use the components of the Angular app as I'm doing right now with angularjs, am I right?

    Thanks

  14. yharaskrik commented on Sep 1, 2021

    @yharaskrik
    Contributor

    Wow. This is amazing! I love it. πŸ‘πŸ‘ Two thumbs up indeed! Compilers are not my forte so I don't have a lot to contribute but I wholeheartedly support this improvement in the framework.

  15. IgorMinar commented on Oct 12, 2021

    @IgorMinar
    ContributorAuthor

    I've updated the description with references to the standalone components, directives, and pipes RFC now that the RFC is out.

  16. removed
    feature
    on Nov 5, 2021
  17. pavankjadda commented on Sep 8, 2022

    @pavankjadda
    Contributor

    I came across this post after NgConf-2022. Minko explained this at Q&A. Hopefully when Standalone components are the default, Angular can switch to local compilation model.

  18. nitedani commented on Jan 29, 2024

    @nitedani

    I would like to have an official Vite plugin for Angular, making it possible for users to leverage the large ecosystem of Vite plugins. It would also be greatly beneficial for framework authors to have an extensible foundation to build upon.

  19. csvn commented on Aug 7, 2025

    @csvn
    Contributor

    With Vite soon using Rolldown, and with the new Environments API, it seems like making Angular just a vite plugin + schematics would make a lot of sense. The dev-server is already using vite, so it does not feel like a huge gap to go in that direction.

  20. olfek commented on Aug 18, 2025

    @olfek

    I would like a plugin (like - https://github.com/angular/angular-cli/tree/6c64560f6eae41e6ab39d6868973f1c8e0c74ee9/packages/ngtools/webpack) for at minimum 3 of the current most popular build tools so developers have a choice.

  21. Klaster1 commented on Mar 9, 2026

    @Klaster1

    Any updates on this?

  22. DavidANeil commented on Mar 21, 2026

    @DavidANeil

    I would love to know what the current state of the art is for getting fast AoT output from Angular for a Bazel-orchestrated (not Angular-cli-orchestrated) system.
    I see that the codebase has an experimental-local compilation mode, but that seems to have stalled, or maybe it is done? @pmvald might know more.
    I recently learned about @ngtools/webpack and that seems promising, except that we don't use webpack, so I believe I would need to do a non-trivial amount of rewiring to get that working for our own.
    NgTscPlugin seems to be the wrong way to do things these days, but that is what our build system currently uses, and it couples typechecking and emit into one action, which elongates our dependency graph to untenable levels. I see @JoostK's name on some of this code, so maybe he can contribute to this discussion.

    For a reference of scale, we currently have ~4300 "programs" that depend on the Angular compiler in our monorepo. We would really like to be able to use AoT, but we don't want to maintain our own compiler plugin.
    We currently avoid most of the pain by just using JiT for dev mode, and then letting CI deal with waiting for the AoT output to build before we pass it to the closure compiler for final optimizations. We would like to avoid the complexity of maintaining two build paths.

    I also understand that this is a hard space to make decisions in, due to the looming advent of the TypeScript-go compiler API, requiring potentially large changes in how Angular integrates with the TypeScript compiler.

    I appreciate any information people have on this topic.

  23. JeanMeche commented on Nov 19, 2026

    @JeanMeche
    Member

    Closing this discussion as this direction is being addressed by the new Angular Preprocessor (ngp) compiler architecture.

    Building on standalone components and the esbuild/Vite-based build pipeline, ngp decouples Angular compilation from TypeScript's monolithic ts.Program / ts.Transformer pipeline by using a parallel, Rust/oxc-based analyzer that preprocesses Angular sources into .ng.ts (consumable by downstream transpilers and bundlers) and separate .ngtypecheck.ts files for TypeScript / TypeScript 7 type-checking. It also supports non-optimized local compilation without cross-file scope resolution or type-checking, while retaining demand-driven cross-file analysis where needed for production optimizations (such as tree-shaking, @defer, and non-standalone NgModule compatibility).

    For more details, see our blog post: https://blog.angular.dev/an-update-on-angulars-typescript-7-powered-compiler-9619a35e2b0a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    action: discussarea: compilerneeds: discussion

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions