Thanks to visit codestin.com
Credit goes to github.com

Skip to content

perf(core): set encapsulation to None for empty component styles #57130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

P4
Copy link
Contributor

@P4 P4 commented Jul 25, 2024

Make it so that encapsulation for empty styles, styles containing only whitespace and comments, etc. is handled the same way as with no styles at all.

Components without styles already have view encapsulation set to None to avoid generating unnecessary attributes for style scoping, like _ngcontent-ng-c1 (#27175)

If the component has an empty external styles file instead, the compiler would generate a component definition without the styles field, but still using the default encapsulation. This can result in runtime overhead if the developer forgets to delete the empty styles file generated automatically for new components by Angular CLI.

Closes #16602

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.io application / infrastructure changes
  • Other... Please describe:

What is the current behavior?

Issue Number: #16602

Example code to see the difference ("standard" metadata omitted for brevity)"

@Component({ template: '', styleUrl: './empty-styles.component.css' }) // css file is empty
export class EmptyStylesComponent {}

@Component({ template: '' }) // no inline or external styles 
export class NoStylesComponent {}

The first component still uses the default encapsulation, even though neither of them have any styles in the final ɵcmp definition

What is the new behavior?

Both components now end up with encapsulation set to None (2) in the final bundle

The same should be true for any component whose styles contain only whitespace or comments, as those get stripped during the build -- at least with source maps turned off.

Does this PR introduce a breaking change?

  • Yes
  • No

If a component has no styles then nothing should be referencing the _nghost-/_ngcontent- attributes, at least from Angular's side. An app could in theory break if it relies in some other way on those attributes being present, but since their names are semi-random, I'd consider that unlikely.

Other information

The case of an empty style file being leftover after generation is fairly common, at least in my experience. This is sometimes intentional, component styles get added and removed quite frequently when developing a new feature, so constantly adding and deleting the file would be pretty annoying, especially with merge conflicts.

@pullapprove pullapprove bot requested a review from AndrewKushnir July 25, 2024 13:11
@angular-robot angular-robot bot added the area: compiler Issues related to `ngc`, Angular's template compiler label Jul 25, 2024
@ngbot ngbot bot added this to the Backlog milestone Jul 25, 2024
@P4 P4 force-pushed the empty-style-encapsulation branch from 0b941f9 to a488571 Compare July 25, 2024 13:34
@angular-robot angular-robot bot added the area: performance Issues related to performance label Jul 25, 2024
@P4
Copy link
Contributor Author

P4 commented Jul 25, 2024

And of course I made a typo in the commit message - should be perf not pref. Sorry about that

@P4 P4 changed the title pref(compiler): set encapsulation to None for empty component styles perf(compiler): set encapsulation to None for empty component styles Jul 25, 2024
Copy link
Member

@JoostK JoostK left a comment

Choose a reason for hiding this comment

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

While this makes sense to me, this might break apps.

@JoostK JoostK added the action: global presubmit The PR is in need of a google3 global presubmit label Jul 25, 2024
@JoostK
Copy link
Member

JoostK commented Jul 25, 2024

Also, the perf effect is targeting core performance, not compiler performance. So from that perspective I think you could make this perf(core) (although I have to admit this is a bit strange, considering which area has actually been changed)

@P4
Copy link
Contributor Author

P4 commented Jul 26, 2024

@JoostK I agree it might break some apps, but as far as I understand it only in the case where someone explicitly assumes the existence of these attributes? They should never be referenced by styles managed by the framework since it only gets triggered if those styles are absent.

I'm perfectly fine with this being considered a breaking change out of caution, let me know if I need to update the PR description or update the commit with BREAKING CHANGES

Re: scope - makes sense, do I need to amend the commit message and do a force-push or is changing the PR title sufficient to correct this?

@JeanMeche
Copy link
Member

We assume this might break some unit tests that rely on those attributes. We'll first test this change against all the test internaly a google. From this point, we'll assess if we make this a breaking change or not.

We'll let you know the process if we require some changes !

@JoostK
Copy link
Member

JoostK commented Jul 26, 2024

Re: scope - makes sense, do I need to amend the commit message and do a force-push or is changing the PR title sufficient to correct this?

Only the actual commit message matters, the PR title is irrelevant.

@JoostK JoostK added the target: minor This PR is targeted for the next minor release label Jul 26, 2024
@JoostK
Copy link
Member

JoostK commented Jul 26, 2024

This came up green across Google, so we can go ahead with this. Thanks for your contribution!

@JoostK JoostK added the action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews label Jul 26, 2024
@JoostK
Copy link
Member

JoostK commented Jul 26, 2024

(the cleanup label is for the commit message change)

@P4 P4 force-pushed the empty-style-encapsulation branch from a488571 to 9cad12d Compare July 27, 2024 18:34
@angular-robot angular-robot bot added the area: core Issues related to the framework runtime label Jul 27, 2024
@P4
Copy link
Contributor Author

P4 commented Jul 27, 2024

Updated the commit message from perf(compiler) to perf(core) as requested. I've changed the PR title too, purely for consistency.

@P4 P4 changed the title perf(compiler): set encapsulation to None for empty component styles perf(core): set encapsulation to None for empty component styles Jul 27, 2024
@P4
Copy link
Contributor Author

P4 commented Jul 27, 2024

Just in case anyone outside Google experiences test failures after updating and finds this PR:

If the failing test expects a component to have _nghost or _ngcontent in its HTML, and those attributes have gone missing, it's likely because the component doesn't define any styles. Those attributes were already omitted for components without styles in their metadata. It now also happens if the styles are empty, styleUrl points at an empty file, etc.

You might need to update the test's expectations or add some dummy styles to the tested component (example)

@JoostK JoostK added action: merge The PR is ready for merge by the caretaker and removed action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews action: global presubmit The PR is in need of a google3 global presubmit labels Jul 28, 2024
@thePunderWoman thePunderWoman added action: review The PR is still awaiting reviews from at least one requested reviewer and removed action: merge The PR is ready for merge by the caretaker labels Jul 30, 2024
@JoostK JoostK force-pushed the empty-style-encapsulation branch from 9cad12d to 9ae8c9b Compare October 6, 2024 19:20
@angular-robot angular-robot bot removed area: compiler Issues related to `ngc`, Angular's template compiler area: core Issues related to the framework runtime labels Oct 6, 2024
@angular-robot angular-robot bot added the area: core Issues related to the framework runtime label Oct 6, 2024
@JoostK JoostK added action: presubmit The PR is in need of a google3 presubmit action: merge The PR is ready for merge by the caretaker and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Oct 6, 2024
Make it so that encapsulation for empty styles, styles containing only whitespace and comments, etc.
is handled the same way as with no styles at all.

Components without styles already have view encapsulation set to `None`
to avoid generating unnecessary attributes for style scoping, like `_ngcontent-ng-c1` (angular#27175)

If the component has an empty external styles file instead, the compiler would generate
a component definition without the `styles` field, but still using the default encapsulation.
This can result in runtime overhead if the developer forgets to delete the empty styles file
generated automatically for new components by Angular CLI.

Closes angular#16602
@JoostK JoostK force-pushed the empty-style-encapsulation branch from 9ae8c9b to be07825 Compare October 7, 2024 15:37
@angular-robot angular-robot bot added area: core Issues related to the framework runtime and removed area: core Issues related to the framework runtime labels Oct 7, 2024
@AndrewKushnir AndrewKushnir added action: global presubmit The PR is in need of a google3 global presubmit and removed action: presubmit The PR is in need of a google3 presubmit labels Oct 8, 2024
@AndrewKushnir
Copy link
Contributor

Global Presubmit.

@devversion
Copy link
Member

I'm removing merge ready label as there seem to be a few failures in the TGP.

@devversion devversion added action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews and removed action: merge The PR is ready for merge by the caretaker action: global presubmit The PR is in need of a google3 global presubmit labels Oct 8, 2024
@AndrewKushnir AndrewKushnir added action: merge The PR is ready for merge by the caretaker merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note and removed action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews labels Oct 8, 2024
@AndrewKushnir
Copy link
Contributor

Caretaker note: TGP is "green" after deflaking some targets.

@AndrewKushnir
Copy link
Contributor

This PR was merged into the repository by commit 97fb86d.

The changes were merged into the following branches: main

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Nov 8, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker area: core Issues related to the framework runtime area: performance Issues related to performance merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note target: minor This PR is targeted for the next minor release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

perf: avoid creating unnecessary DOM attributes
6 participants