Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(router): auto-unwrap default exports when lazy loading (#47586)
When using `loadChildren` or `loadComponent`, a common pattern is to pass a function that returns a `Promise` from a dynamic import: ```typescript { path: 'lazy', loadComponent: () => import('./lazy-file').then(m => m.LazyCmp), } ``` The `.then` part of the expression selects the particular exported component symbol from the dynamically imported ES module. ES modules can have a "default export", created with the `export default` modifier: ```typescript @component({...}) export default class LazyCmp { ... } ``` This default export is made available to dynamic imports under the well- known key of `'default'`, per the ES module spec: https://tc39.es/ecma262/#table-export-forms-mapping-to-exportentry-records This commit adds a feature to the router to automatically dereference such default exports. With this logic, when `export default` is used, a `.then` operation to select the particular exported symbol is no longer required: ```typescript { path: 'lazy', loadComponent: () => import('./lazy-file'), } ``` The above `loadComponent` operation will automatically use the `default` export of the `lazy-file` ES module. This functionality works for `loadChildren` as well. PR Close #47586
- Loading branch information
1 parent
a2a066d
commit da58801
Showing
9 changed files
with
154 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {Component} from '@angular/core'; | ||
|
||
@Component({ | ||
standalone: true, | ||
template: 'default exported', | ||
selector: 'test-route', | ||
}) | ||
export default class TestRoute { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {Component} from '@angular/core'; | ||
import {Routes} from '@angular/router'; | ||
|
||
@Component({ | ||
standalone: true, | ||
template: 'default exported', | ||
selector: 'test-route', | ||
}) | ||
export class TestRoute { | ||
} | ||
|
||
|
||
export default [ | ||
{path: '', pathMatch: 'full', component: TestRoute}, | ||
] as Routes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters