Skip to content

Conversation

SkyZeroZx
Copy link
Contributor

@SkyZeroZx SkyZeroZx commented Sep 29, 2025

Common to Standalone Migration

This migration replaces CommonModule imports with specific standalone imports from @angular/common.

Usage

ng g @angular/core:common-to-standalone --path src/app/feature

Example

Before:

import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';

@Component({
  selector: 'app-example',
  standalone: true,
  imports: [CommonModule],
  template: `
    <div *ngIf="show">
      {{ data | async | json }}
    </div>
  `
})
export class ExampleComponent {
  show = true;
  data = Promise.resolve({ message: 'Hello' });
}

After:

import { Component } from '@angular/core';
import { AsyncPipe, JsonPipe, NgIf } from '@angular/common';

@Component({
  selector: 'app-example',
  standalone: true,
  imports: [AsyncPipe, JsonPipe, NgIf],
  template: `
    <div *ngIf="show">
      {{ data | async | json }}
    </div>
  `
})
export class ExampleComponent {
  show = true;
  data = Promise.resolve({ message: 'Hello' });
}

Benefits

  • Smaller bundle size: Only imports needed directives and pipes instead of the entire CommonModule
  • Better tree-shaking: Unused parts of @angular/common are eliminated

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • angular.dev application / infrastructure changes
  • Other... Please describe:

What is the current behavior?

Issue Number: #60808

What is the new behavior?

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

@pullapprove pullapprove bot requested a review from JeanMeche September 29, 2025 18:19
@angular-robot angular-robot bot added detected: feature PR contains a feature commit area: migrations Issues related to `ng update`/`ng generate` migrations labels Sep 29, 2025
@ngbot ngbot bot added this to the Backlog milestone Sep 29, 2025
Copy link
Member

@JeanMeche JeanMeche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this.
Can you make sure to include the corresponding docs in the change, see adev/src/content/reference/migrations/overview.md

@SkyZeroZx
Copy link
Contributor Author

Thanks for this. Can you make sure to include the corresponding docs in the change, see adev/src/content/reference/migrations/overview.md

If I do it, I would first like to have your approval to be able to add it. I will do it on this same PR.

@JeanMeche
Copy link
Member

Yes this is an optional migration that we'd like to have !

@SkyZeroZx SkyZeroZx force-pushed the feature/add-common-module-migration branch from 1827465 to ae56ad6 Compare September 30, 2025 02:09
@SkyZeroZx
Copy link
Contributor Author

Updated addressing the feedback, the only remaining question is whether to support NgModules or only standalone components. If it’s possible to consult this in some way, that would be great.

@SkyZeroZx SkyZeroZx force-pushed the feature/add-common-module-migration branch 2 times, most recently from 5596c9b to 2f1954c Compare October 1, 2025 04:10
@SkyZeroZx SkyZeroZx force-pushed the feature/add-common-module-migration branch from 2f1954c to 4531859 Compare October 2, 2025 17:24
@SkyZeroZx SkyZeroZx force-pushed the feature/add-common-module-migration branch from 4531859 to 477aac7 Compare October 3, 2025 21:40
Copy link
Member

@JeanMeche JeanMeche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Next round of comments. PTAL.

private hasCommonModuleInImports(decorator: ts.Decorator): boolean {
if (!ts.isCallExpression(decorator.expression)) return false;

const config = decorator.expression.arguments[0];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is me thinking out loud:

I think this is unsafe if we hit a @Directive(), ie with 0 arguments. iirc ts.isObjectLiteralExpression(config) will throw if config is undefined.

But since we don't care about Directive decorator, we should only check component decorators.

Either way, we need to make the check more robust.

Copy link
Contributor Author

@SkyZeroZx SkyZeroZx Oct 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we only process the Component decorator, this shouldn't happen. However, it could be "possible" that a user might have something strange with the decorator. I'm going to add a condition.

private analyzeClass(
    node: ts.ClassDeclaration,
    typeChecker: ts.TypeChecker,
  ): CommonModuleReference | null {
    const nodeDecorators = ts.getDecorators(node);
    if (!nodeDecorators) return null;

    const decorators = getAngularDecorators(typeChecker, nodeDecorators);

    // Only process Component decorators, not Directive or other Angular decorators
    for (const decorator of decorators) {
      if (decorator.name === 'Component') {
        return this.analyzeComponentDecorator(node, decorator, typeChecker);
      }
    }

    return null;
  }

@SkyZeroZx SkyZeroZx force-pushed the feature/add-common-module-migration branch from 477aac7 to 12e59a5 Compare October 4, 2025 21:19
@SkyZeroZx SkyZeroZx force-pushed the feature/add-common-module-migration branch from 12e59a5 to 7db8f18 Compare October 10, 2025 01:07
@SkyZeroZx SkyZeroZx force-pushed the feature/add-common-module-migration branch from 7db8f18 to fc8b738 Compare October 10, 2025 01:21
@SkyZeroZx SkyZeroZx force-pushed the feature/add-common-module-migration branch from fc8b738 to c2e88ca Compare October 10, 2025 01:28
@JeanMeche JeanMeche added the target: major This PR is targeted for the next major release label Oct 10, 2025
@SkyZeroZx SkyZeroZx force-pushed the feature/add-common-module-migration branch from c2e88ca to 9abafde Compare October 10, 2025 01:36
@SkyZeroZx SkyZeroZx force-pushed the feature/add-common-module-migration branch from 9abafde to 93d7749 Compare October 10, 2025 01:51
@SkyZeroZx SkyZeroZx force-pushed the feature/add-common-module-migration branch from 93d7749 to 8e76c83 Compare October 10, 2025 02:21
@SkyZeroZx SkyZeroZx force-pushed the feature/add-common-module-migration branch from 8e76c83 to b0df11b Compare October 10, 2025 04:22
@JeanMeche
Copy link
Member

Can you please rebase.

Introduces a migration that replaces CommonModule usage with individual imports from @angular/common, aligning with Angular's standalone component approach and improving module import clarity
@SkyZeroZx SkyZeroZx force-pushed the feature/add-common-module-migration branch from b0df11b to 106c2d0 Compare October 15, 2025 01:31
@SkyZeroZx
Copy link
Contributor Author

Updated

@JeanMeche JeanMeche added the action: merge The PR is ready for merge by the caretaker label Oct 15, 2025
@AndrewKushnir
Copy link
Contributor

This PR was merged into the repository. The changes were merged into the following branches:

@melroy89
Copy link
Contributor

melroy89 commented Oct 16, 2025

Wait..? This is something else then ng generate @angular/core:standalone?

EDIT: Ah yes so this will get rid of the old CommonModule. But should ng generate @angular/core:standalone also not use CommonModule anymore..?

@JeanMeche
Copy link
Member

ng generate @angular/core:standalone will transform your NgModules into standalone components (and delete some of your NgModules). This migration here will replace direct imports of the CommonModule into individual imports.

Those migrations are distinct and this one was introduced to help project follow the recommendations closely.

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

Labels

action: merge The PR is ready for merge by the caretaker area: migrations Issues related to `ng update`/`ng generate` migrations detected: feature PR contains a feature commit target: major This PR is targeted for the next major release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants