Skip to content

Commit c61df39

Browse files
alxhubAndrewKushnir
authored andcommittedApr 25, 2019
feat(router): deprecate loadChildren:string (#30073)
The proposed ES dynamic import() is now supported by the Angular CLI and the larger toolchain. This renders the `loadChildren: string` API largely redundant, as import() is far more natural, is less error-prone, and is standards compliant. This commit deprecates the `string` form of `loadChildren` in favor of dynamic import(). DEPRECATION: When defining lazy-loaded route, Angular previously offered two options for configuring the module to be loaded, both via the `loadChildren` parameter of the route. Most Angular developers are familiar withthe `string` form of this API. For example, the following route definition configures Angular to load a `LazyModule` NgModule from `lazy-route/lazy.module.ts`: ``` [{ path: 'lazy', loadChildren: 'lazy-route/lazy.module#LazyModule', }] ``` This "magic string" configuration was previously necessary as there was no dynamic module loading standard on the web. This has changed with the pending standardization of dynamic `import()` expressions, which are now supported in the Angular CLI and in web tooling in general. `import()` offers a more natural and robust solution to dynamic module loading. The above example can be rewritten to use dynamic `import()`: ``` [{ path: 'lazy', loadChildren: () => import('./lazy-route/lazy.module').then(mod => mod.LazyModule), }] ``` This form of lazy loading offers significant advantages in terms of: * type checking via TypeScript * simplicity of generated code * future potential to run natively in supporting browsers (see: [caniuse: dynamic import()](https://caniuse.com/#feat=es6-module-dynamic-import)) As a result, Angular is deprecating the `loadChildren: string` syntax in favor of ES dynamic `import()`. An automatic migration will run during `ng upgrade` to convert your existing Angular code to the new syntax. PR Close #30073
1 parent abcb2cf commit c61df39

File tree

6 files changed

+41
-3
lines changed

6 files changed

+41
-3
lines changed
 

‎packages/core/src/linker/ng_module_factory_loader.ts

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import {NgModuleFactory} from './ng_module_factory';
1717
* Used to load ng module factories.
1818
*
1919
* @publicApi
20+
* @deprecated the `string` form of `loadChildren` is deprecated, and `NgModuleFactoryLoader` is
21+
* part of its implementation. See `LoadChildren` for more details.
2022
*/
2123
export abstract class NgModuleFactoryLoader {
2224
abstract load(path: string): Promise<NgModuleFactory<any>>;

‎packages/core/src/linker/system_js_ng_module_factory_loader.ts

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ declare var System: any;
2424
* token.
2525
*
2626
* @publicApi
27+
* @deprecated the `string` form of `loadChildren` is deprecated, and `SystemJsNgModuleLoaderConfig`
28+
* is part of its implementation. See `LoadChildren` for more details.
2729
*/
2830
export abstract class SystemJsNgModuleLoaderConfig {
2931
/**
@@ -47,6 +49,8 @@ const DEFAULT_CONFIG: SystemJsNgModuleLoaderConfig = {
4749
/**
4850
* NgModuleFactoryLoader that uses SystemJS to load NgModuleFactory
4951
* @publicApi
52+
* @deprecated the `string` form of `loadChildren` is deprecated, and `SystemJsNgModuleLoader` is
53+
* part of its implementation. See `LoadChildren` for more details.
5054
*/
5155
@Injectable()
5256
export class SystemJsNgModuleLoader implements NgModuleFactoryLoader {

‎packages/router/src/config.ts

+27-1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,18 @@ export type ResolveData = {
9393
/**
9494
*
9595
* A function that is called to resolve a collection of lazy-loaded routes.
96+
*
97+
* Often this function will be implemented using an ES dynamic `import()` expression. For example:
98+
*
99+
* ```
100+
* [{
101+
* path: 'lazy',
102+
* loadChildren: () => import('./lazy-route/lazy.module').then(mod => mod.LazyModule),
103+
* }];
104+
* ```
105+
*
106+
* This function _must_ match the form above: an arrow function of the form
107+
* `() => import('...').then(mod => mod.MODULE)`.
96108
*
97109
* @see `Route#loadChildren`.
98110
* @publicApi
@@ -105,10 +117,24 @@ export type LoadChildrenCallback = () => Type<any>| NgModuleFactory<any>| Observ
105117
* A string of the form `path/to/file#exportName` that acts as a URL for a set of routes to load,
106118
* or a function that returns such a set.
107119
*
120+
* The string form of `LoadChildren` is deprecated (see `DeprecatedLoadChildren`). The function
121+
* form (`LoadChildrenCallback`) should be used instead.
122+
*
108123
* @see `Route#loadChildren`.
109124
* @publicApi
110125
*/
111-
export type LoadChildren = string | LoadChildrenCallback;
126+
export type LoadChildren = LoadChildrenCallback | DeprecatedLoadChildren;
127+
128+
/**
129+
* A string of the form `path/to/file#exportName` that acts as a URL for a set of routes to load.
130+
*
131+
* @see `Route#loadChildren`
132+
* @publicApi
133+
* @deprecated the `string` form of `loadChildren` is deprecated in favor of the proposed ES dynamic
134+
* `import()` expression, which offers a more natural and standards-based mechanism to dynamically
135+
* load an ES module at runtime.
136+
*/
137+
export type DeprecatedLoadChildren = string;
112138

113139
/**
114140
*

‎packages/router/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99

10-
export {Data, LoadChildren, LoadChildrenCallback, ResolveData, Route, Routes, RunGuardsAndResolvers, UrlMatchResult, UrlMatcher} from './config';
10+
export {Data, DeprecatedLoadChildren, LoadChildren, LoadChildrenCallback, ResolveData, Route, Routes, RunGuardsAndResolvers, UrlMatchResult, UrlMatcher} from './config';
1111
export {RouterLink, RouterLinkWithHref} from './directives/router_link';
1212
export {RouterLinkActive} from './directives/router_link_active';
1313
export {RouterOutlet} from './directives/router_outlet';

‎tools/public_api_guard/core/core.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,7 @@ export declare abstract class NgModuleFactory<T> {
586586
abstract create(parentInjector: Injector | null): NgModuleRef<T>;
587587
}
588588

589+
/** @deprecated */
589590
export declare abstract class NgModuleFactoryLoader {
590591
abstract load(path: string): Promise<NgModuleFactory<any>>;
591592
}
@@ -1297,11 +1298,13 @@ export interface SkipSelfDecorator {
12971298

12981299
export declare type StaticProvider = ValueProvider | ExistingProvider | StaticClassProvider | ConstructorProvider | FactoryProvider | any[];
12991300

1301+
/** @deprecated */
13001302
export declare class SystemJsNgModuleLoader implements NgModuleFactoryLoader {
13011303
constructor(_compiler: Compiler, config?: SystemJsNgModuleLoaderConfig);
13021304
load(path: string): Promise<NgModuleFactory<any>>;
13031305
}
13041306

1307+
/** @deprecated */
13051308
export declare abstract class SystemJsNgModuleLoaderConfig {
13061309
factoryPathPrefix: string;
13071310
factoryPathSuffix: string;

‎tools/public_api_guard/router/router.d.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ export declare class DefaultUrlSerializer implements UrlSerializer {
101101
serialize(tree: UrlTree): string;
102102
}
103103

104+
/** @deprecated */
105+
export declare type DeprecatedLoadChildren = string;
106+
104107
export declare type DetachedRouteHandle = {};
105108

106109
export declare type Event = RouterEvent | RouteConfigLoadStart | RouteConfigLoadEnd | ChildActivationStart | ChildActivationEnd | ActivationStart | ActivationEnd | Scroll;
@@ -145,7 +148,7 @@ export declare class GuardsCheckStart extends RouterEvent {
145148
toString(): string;
146149
}
147150

148-
export declare type LoadChildren = string | LoadChildrenCallback;
151+
export declare type LoadChildren = LoadChildrenCallback | DeprecatedLoadChildren;
149152

150153
export declare type LoadChildrenCallback = () => Type<any> | NgModuleFactory<any> | Observable<Type<any>> | Promise<NgModuleFactory<any> | Type<any> | any>;
151154

0 commit comments

Comments
 (0)
Please sign in to comment.