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

Skip to content

Add HMR update function #58205

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 5 commits into from
Closed

Conversation

crisbeto
Copy link
Member

Includes the following changes that are necessary to generate the HMR update function:

refactor(core): account for new replaceMetadata signature

Updates the runtime code to account for the upcoming changes to ɵɵreplaceMetadata.

I also had to reorganize how the angularCoreEnv was set up, because ɵɵreplaceMetadata needs access to it without triggering a circular dependency.

refactor(compiler): output AST visitor not visiting implicit arrow function returns

Fixes that the output AST's RecursiveVisitor wasn't visiting all the nodes when an arrow function has an implicit return. The problem was that we were calling the visitExpression method directly, instead of .visitExpression. This doesn't affect existing code since the RecursiveVisitor isn't used anywhere, but it will affect future HMR code.

refactor(compiler-cli): allow namespace import identifiers to be rewritten

Currently only the prefix of namespace imports is configurable, but for HMR we need to know ahead of time what the name of @angular/core will be. These changes allow us to rewrite it.

refactor(compiler): generate HMR update function

Adds some code to the compiler that will generate the HMR update callback function definition.

feat(compiler-cli): generate the HMR replacement module

Adds the ability to generate the function that replaces the component's metadata during HMR. The HMR update module is a function that is loaded dynamically and as such it has some special considerations:

  • It isn't bundled, because doing so will result in multiple versions of core.
  • Since it isn't bundled, all dependencies have to be passed in as parameters. These changes include some special logic to determine and output those dependencies.
  • While HMR is enabled, we have to disable the functionality that generates dynamic imports and drop the dependencies inside @defer blocks, because we need to retain the ability to refer to them in case they're needed inside the HMR update function.
  • The function is returned by the NgCompiler as a string for the CLI's sake.

@angular-robot angular-robot bot added detected: feature PR contains a feature commit area: core Issues related to the framework runtime area: compiler Issues related to `ngc`, Angular's template compiler labels Oct 15, 2024
@ngbot ngbot bot added this to the Backlog milestone Oct 15, 2024
@crisbeto crisbeto added the target: major This PR is targeted for the next major release label Oct 15, 2024
@crisbeto crisbeto marked this pull request as ready for review October 15, 2024 13:41
@crisbeto crisbeto added the action: review The PR is still awaiting reviews from at least one requested reviewer label Oct 15, 2024
@angular-robot angular-robot bot added area: core Issues related to the framework runtime area: compiler Issues related to `ngc`, Angular's template compiler and removed area: compiler Issues related to `ngc`, Angular's template compiler area: core Issues related to the framework runtime labels Oct 15, 2024
@crisbeto crisbeto changed the title Add HMR update functions Add HMR update function Oct 15, 2024
Copy link
Contributor

@AndrewKushnir AndrewKushnir left a comment

Choose a reason for hiding this comment

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

LGTM (left a couple minor, non-blocking comments) 👍

}

// Import declarations.
if (ts.isImportDeclaration(node) && node.importClause) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: it looks like we have similar logic in packages/compiler-cli/src/ngtsc/imports/src/imported_symbols_tracker.ts, we my consider refactoring it (later, in a separate PR) and reuse in multiple places, so we don't duplicate imported symbols extraction logic.

* Traverses a TypeScript AST and tracks all the top-level reads.
* @param node Node from which to start the traversal.
*/
private addAllTopLevelIdentifiers = (node: ts.Node) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: can we declare this function as a class method?

Suggested change
private addAllTopLevelIdentifiers = (node: ts.Node) => {
private addAllTopLevelIdentifiers(node: ts.Node) {

Copy link
Member Author

Choose a reason for hiding this comment

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

I had to define it as an arrow function, because it uses this and is being passed in directly in the forEachChild call below.

@crisbeto crisbeto removed the request for review from clydin October 16, 2024 05:35
Updates the runtime code to account for the upcoming changes to `ɵɵreplaceMetadata`.

I also had to reorganize how the `angularCoreEnv` was set up, because `ɵɵreplaceMetadata` needs access to it without triggering a circular dependency.
…nction returns

Fixes that the output AST's `RecursiveVisitor` wasn't visiting all the nodes when an arrow function has an implicit return. The problem was that we were calling the `visitExpression` method directly, instead of `.visitExpression`. This doesn't affect existing code since the `RecursiveVisitor` isn't used anywhere, but it will affect future HMR code.
…itten

Currently only the prefix of namespace imports is configurable, but for HMR we need to know ahead of time what the name of `@angular/core` will be. These changes allow us to rewrite it.
Adds some code to the compiler that will generate the HMR update callback function definition.
Adds the ability to generate the function that replaces the component's metadata during HMR. The HMR update module is a function that is loaded dynamically and as such it has some special considerations:
* It isn't bundled, because doing so will result in multiple version of core.
* Since it isn't bundled, all dependencies have to be passed in as parameters. These changes include some special logic to determine and output those dependencies.
* While HMR is enabled, we have to disable the functionality that generates dynamic imports and drop the dependencies inside `@defer` blocks, because we need to retain the ability to refer to them in case they're needed inside the HMR update function.
* The function is returned by the `NgCompiler` as a string for the CLI's sake.
@angular-robot angular-robot bot removed the area: compiler Issues related to `ngc`, Angular's template compiler label Oct 16, 2024
@angular-robot angular-robot bot added area: core Issues related to the framework runtime area: compiler Issues related to `ngc`, Angular's template compiler and removed area: core Issues related to the framework runtime labels Oct 16, 2024
@ngbot ngbot bot modified the milestone: Backlog Oct 16, 2024
@crisbeto crisbeto added 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 16, 2024
@devversion
Copy link
Member

This PR was merged into the repository by commit 231e6ff.

The changes were merged into the following branches: main

devversion pushed a commit that referenced this pull request Oct 16, 2024
…nction returns (#58205)

Fixes that the output AST's `RecursiveVisitor` wasn't visiting all the nodes when an arrow function has an implicit return. The problem was that we were calling the `visitExpression` method directly, instead of `.visitExpression`. This doesn't affect existing code since the `RecursiveVisitor` isn't used anywhere, but it will affect future HMR code.

PR Close #58205
devversion pushed a commit that referenced this pull request Oct 16, 2024
…itten (#58205)

Currently only the prefix of namespace imports is configurable, but for HMR we need to know ahead of time what the name of `@angular/core` will be. These changes allow us to rewrite it.

PR Close #58205
devversion pushed a commit that referenced this pull request Oct 16, 2024
Adds some code to the compiler that will generate the HMR update callback function definition.

PR Close #58205
devversion pushed a commit that referenced this pull request Oct 16, 2024
Adds the ability to generate the function that replaces the component's metadata during HMR. The HMR update module is a function that is loaded dynamically and as such it has some special considerations:
* It isn't bundled, because doing so will result in multiple version of core.
* Since it isn't bundled, all dependencies have to be passed in as parameters. These changes include some special logic to determine and output those dependencies.
* While HMR is enabled, we have to disable the functionality that generates dynamic imports and drop the dependencies inside `@defer` blocks, because we need to retain the ability to refer to them in case they're needed inside the HMR update function.
* The function is returned by the `NgCompiler` as a string for the CLI's sake.

PR Close #58205
@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 16, 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: compiler Issues related to `ngc`, Angular's template compiler area: core Issues related to the framework runtime 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.

3 participants