Skip to content

refactor(compiler): move injector factory - #41022

Closed
petebacondarwin wants to merge 2 commits into
angular:masterfrom
petebacondarwin:compiler-injector-factory-refactor
Closed

petebacondarwin wants to merge 2 commits into
angular:masterfrom
petebacondarwin:compiler-injector-factory-refactor

Conversation

@petebacondarwin

@petebacondarwin petebacondarwin commented Feb 26, 2021 •

Copy link
Copy Markdown
Contributor

This work is a precursor to creating the ngDeclareNgModule() code.

This sits on top of #41040 - so you only need to look at the commits starting from refactor(compiler): separate compileFactoryFunction() from compileInjector() onwards.

@google-cla google-cla Bot added the cla: yes label Feb 26, 2021
@pullapprove
pullapprove Bot requested a review from AndrewKushnir February 26, 2021 21:40
@petebacondarwin
petebacondarwin requested review from alxhub and removed request for AndrewKushnir February 26, 2021 21:40
@petebacondarwin petebacondarwin added action: review The PR is still awaiting reviews from at least one requested reviewer area: compiler Issues related to `ngc`, Angular's template compiler refactoring Issue that involves refactoring or code-cleanup target: patch This PR is targeted for the next patch release labels Feb 26, 2021
@ngbot ngbot Bot modified the milestone: Backlog Feb 26, 2021
@petebacondarwin
petebacondarwin marked this pull request as draft February 26, 2021 21:56
@petebacondarwin petebacondarwin added state: WIP and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Feb 26, 2021

@JoostK JoostK left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Overall LGTM. I suspect that InjectorDef.factory was simply never moved to the fac property when the separate fac was introduced, but I'm not sure what the effect of this change exactly is. Are NgModules still injectable like they used to... do we have tests for that?

Comment thread goldens/public-api/core/core.d.ts Resolved
Comment thread packages/core/src/di/interface/defs.ts Resolved
Comment thread packages/core/src/metadata/ng_module.ts Resolved
Comment thread packages/core/src/render3/di.ts Resolved
@petebacondarwin
petebacondarwin force-pushed the compiler-injector-factory-refactor branch 5 times, most recently from 23cf683 to ee857b4 Compare March 3, 2021 07:31
@petebacondarwin
petebacondarwin marked this pull request as ready for review March 3, 2021 08:51
Comment on lines 42 to 44

@petebacondarwin petebacondarwin Mar 3, 2021 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

As of writing the actual values on master for these files are different to the golden file:

  main-es2015 polyfills-es2015 5-es2015
master 241450 36975 745
this PR 242415 36975 753
diff -35 0 +8

As noted in the commit message, the increase to 5-es2015 is because this PR introduces 4 extra chars for each NgModule declaration.

The initial main-es2015 decreases due to the simplification of the code in @anguar/core.

@petebacondarwin petebacondarwin added action: review The PR is still awaiting reviews from at least one requested reviewer state: blocked and removed state: WIP labels Mar 3, 2021
@petebacondarwin
petebacondarwin requested a review from JoostK March 3, 2021 09:27
@petebacondarwin
petebacondarwin force-pushed the compiler-injector-factory-refactor branch 3 times, most recently from 6f12f6a to 6dbb6f0 Compare March 4, 2021 21:01
@pullapprove
pullapprove Bot requested a review from alxhub March 8, 2021 18:28
…Injector()`

This commit moves the creation of the injector's factory function
out so that it can be more easily refactored further.
Previously, injector definitions contained a `factory` property that
was used to create a new instance of the associated NgModule class.

Now this factory has been moved to its own `ɵfac` static property on the
NgModule class itself. This is inline with how directives, components and
pipes are created.

There is a small size increase to bundle sizes for each NgModule class,
because the `ɵfac` takes up a bit more space:

Before:

```js
let a = (() => {
  class n {}
  return n.\u0275mod = c.Cb({type: n}),
  n.\u0275inj = c.Bb({factory: function(t) { return new (t || n) }, imports: [[e.a.forChild(s)], e.a]}),
  n
})(),
```

After:

```js
let a = (() => {
  class n {}
  return n.\u0275fac = function(t) { return new (t || n) },
  n.\u0275mod = c.Cb({type: n}),
  n.\u0275inj = c.Bb({imports: [[r.a.forChild(s)], r.a]}),
  n
})(),
```

In other words `n.\u0275fac = ` is longer than `factory: ` (by 5 characters)
and only because the tooling insists on encoding `ɵ` as `\u0275`.

This can be mitigated in a future PR by only generating the `ɵfac` property
if it is actually needed.
@petebacondarwin
petebacondarwin force-pushed the compiler-injector-factory-refactor branch from 2957620 to 2960026 Compare March 8, 2021 19:22
@AndrewKushnir

Copy link
Copy Markdown
Contributor

Presubmit #2.

@petebacondarwin petebacondarwin 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 Mar 8, 2021
@AndrewKushnir AndrewKushnir added target: major This PR is targeted for the next major release and removed action: presubmit The PR is in need of a google3 presubmit target: patch This PR is targeted for the next patch release labels Mar 8, 2021
AndrewKushnir pushed a commit that referenced this pull request Mar 8, 2021
Previously, injector definitions contained a `factory` property that
was used to create a new instance of the associated NgModule class.

Now this factory has been moved to its own `ɵfac` static property on the
NgModule class itself. This is inline with how directives, components and
pipes are created.

There is a small size increase to bundle sizes for each NgModule class,
because the `ɵfac` takes up a bit more space:

Before:

```js
let a = (() => {
  class n {}
  return n.\u0275mod = c.Cb({type: n}),
  n.\u0275inj = c.Bb({factory: function(t) { return new (t || n) }, imports: [[e.a.forChild(s)], e.a]}),
  n
})(),
```

After:

```js
let a = (() => {
  class n {}
  return n.\u0275fac = function(t) { return new (t || n) },
  n.\u0275mod = c.Cb({type: n}),
  n.\u0275inj = c.Bb({imports: [[r.a.forChild(s)], r.a]}),
  n
})(),
```

In other words `n.\u0275fac = ` is longer than `factory: ` (by 5 characters)
and only because the tooling insists on encoding `ɵ` as `\u0275`.

This can be mitigated in a future PR by only generating the `ɵfac` property
if it is actually needed.

PR Close #41022
@AndrewKushnir

Copy link
Copy Markdown
Contributor

@petebacondarwin FYI there was a conflict while merging this PR to the patch branch (11.2.x), so I merged the change to the master branch only. Could you please create a patch-only version of this PR if needed? Thank you.

@petebacondarwin

Copy link
Copy Markdown
Contributor Author

Thanks @AndrewKushnir

@petebacondarwin
petebacondarwin deleted the compiler-injector-factory-refactor branch March 9, 2021 07:40
petebacondarwin added a commit to petebacondarwin/angular that referenced this pull request Mar 9, 2021
…Injector()` (angular#41022)

This commit moves the creation of the injector's factory function
out so that it can be more easily refactored further.

PR Close angular#41022
petebacondarwin added a commit to petebacondarwin/angular that referenced this pull request Mar 9, 2021
…41022)

Previously, injector definitions contained a `factory` property that
was used to create a new instance of the associated NgModule class.

Now this factory has been moved to its own `ɵfac` static property on the
NgModule class itself. This is inline with how directives, components and
pipes are created.

There is a small size increase to bundle sizes for each NgModule class,
because the `ɵfac` takes up a bit more space:

Before:

```js
let a = (() => {
  class n {}
  return n.\u0275mod = c.Cb({type: n}),
  n.\u0275inj = c.Bb({factory: function(t) { return new (t || n) }, imports: [[e.a.forChild(s)], e.a]}),
  n
})(),
```

After:

```js
let a = (() => {
  class n {}
  return n.\u0275fac = function(t) { return new (t || n) },
  n.\u0275mod = c.Cb({type: n}),
  n.\u0275inj = c.Bb({imports: [[r.a.forChild(s)], r.a]}),
  n
})(),
```

In other words `n.\u0275fac = ` is longer than `factory: ` (by 5 characters)
and only because the tooling insists on encoding `ɵ` as `\u0275`.

This can be mitigated in a future PR by only generating the `ɵfac` property
if it is actually needed.

PR Close angular#41022
petebacondarwin added a commit to petebacondarwin/angular that referenced this pull request Mar 9, 2021
…Injector()` (angular#41022)

This commit moves the creation of the injector's factory function
out so that it can be more easily refactored further.

PR Close angular#41022
petebacondarwin added a commit to petebacondarwin/angular that referenced this pull request Mar 9, 2021
…41022)

Previously, injector definitions contained a `factory` property that
was used to create a new instance of the associated NgModule class.

Now this factory has been moved to its own `ɵfac` static property on the
NgModule class itself. This is inline with how directives, components and
pipes are created.

There is a small size increase to bundle sizes for each NgModule class,
because the `ɵfac` takes up a bit more space:

Before:

```js
let a = (() => {
  class n {}
  return n.\u0275mod = c.Cb({type: n}),
  n.\u0275inj = c.Bb({factory: function(t) { return new (t || n) }, imports: [[e.a.forChild(s)], e.a]}),
  n
})(),
```

After:

```js
let a = (() => {
  class n {}
  return n.\u0275fac = function(t) { return new (t || n) },
  n.\u0275mod = c.Cb({type: n}),
  n.\u0275inj = c.Bb({imports: [[r.a.forChild(s)], r.a]}),
  n
})(),
```

In other words `n.\u0275fac = ` is longer than `factory: ` (by 5 characters)
and only because the tooling insists on encoding `ɵ` as `\u0275`.

This can be mitigated in a future PR by only generating the `ɵfac` property
if it is actually needed.

PR Close angular#41022
AndrewKushnir pushed a commit that referenced this pull request Mar 9, 2021
…Injector()` (#41022) (#41133)

This commit moves the creation of the injector's factory function
out so that it can be more easily refactored further.

PR Close #41022

PR Close #41133
AndrewKushnir pushed a commit that referenced this pull request Mar 9, 2021
…41133)

Previously, injector definitions contained a `factory` property that
was used to create a new instance of the associated NgModule class.

Now this factory has been moved to its own `ɵfac` static property on the
NgModule class itself. This is inline with how directives, components and
pipes are created.

There is a small size increase to bundle sizes for each NgModule class,
because the `ɵfac` takes up a bit more space:

Before:

```js
let a = (() => {
  class n {}
  return n.\u0275mod = c.Cb({type: n}),
  n.\u0275inj = c.Bb({factory: function(t) { return new (t || n) }, imports: [[e.a.forChild(s)], e.a]}),
  n
})(),
```

After:

```js
let a = (() => {
  class n {}
  return n.\u0275fac = function(t) { return new (t || n) },
  n.\u0275mod = c.Cb({type: n}),
  n.\u0275inj = c.Bb({imports: [[r.a.forChild(s)], r.a]}),
  n
})(),
```

In other words `n.\u0275fac = ` is longer than `factory: ` (by 5 characters)
and only because the tooling insists on encoding `ɵ` as `\u0275`.

This can be mitigated in a future PR by only generating the `ɵfac` property
if it is actually needed.

PR Close #41022

PR Close #41133
@angular-automatic-lock-bot

Copy link
Copy Markdown

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 Apr 9, 2021
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 cla: yes refactoring Issue that involves refactoring or code-cleanup target: major This PR is targeted for the next major release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants