-
Notifications
You must be signed in to change notification settings - Fork 12k
[router] Recommend Dynamic Import Expressions instead of string for loadChildren #7147
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
Comments
Unfortunately, this won't work with AOT without additional logic. |
Should be doable. I don't imagine we would deprecate the current way though |
Simply using dynamic import will not respect |
@clydin Yes, I can't get it to work with AoT. Such a pity! |
Hi. Currently I found some workaround which works fine in universal application (the standard lazyload do not work on ng universal apps) . let server = false;
if (typeof window === "undefined") {
server = true;
}
export async function preloadPageModule(): Promise<any> {
return (await import(/* webpackChunkName: "page" */ './pages/page.module')).PageModule;
}
@NgModule({
imports: [
// ...
RouterModule.forRoot([
// ...
{ path: 'page', loadChildren: server ? preloadPageModule : './pages/page.module#PageModule'}
])
],
declarations: [AppComponent],
bootstrap: [AppComponent]
})
export class AppModule { } Setup:
Still there is one problem, application on server side is prerendering the PageModule (component from it) , then after application start this component is removed from dom, chunk for the PageModule component is loading, after load is rendered again. What can be frustrating for the user but can works eg for SEO. BTW const promises: Array<Promise<any>> = [];
if (typeof Object.assign === 'undefined') {
// Object.assign is available in most of browsers - standard lazy load if needed
promises.push(import(/* webpackChunkName: "objectAssignPolyfill" */ 'object.assign'));
}
if (typeof (window as any).Zone === 'undefined') {
// Zone is not in any browsers - eager (bundled in same bundle as this file, but lazy evaluate)
promises.push(import(/* webpackMode: "eager", webpackChunkName: "zonePolyfill" */ 'zone.js/dist/zone'));
}
Promise.all(promises).then(() => {
// And after preload polyfills evaluate and execute the app code.
return import(/* webpackMode: "eager", webpackChunkName: "bootstrap" */ './app/app.bootstrap');
}); |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Bug Report or Feature Request (mark with an
x
)Desired functionality.
Currently, it's recommended to use
loadChildren
with the module location as a string and the class name after a#
like following:Since Typescript 2.4 we can use the feature "Dynamic Import Expressions", which allows to dynamically import modules without using external module loaders like SystemJS.
I would suggest to deprecate the string loading functionality and switch to something like the following, not only because this is a nifty feature Typescript now supports, but we can get rid of additional logic to ensure webpack builds the lazy loaded chunks (
/packages/@ngtools/webpack/src/lazy_routes.ts
):I've tried the code after switching modules in tsconfig from
es2015
tocommonjs
and with@angular/[email protected]
Here is a plunkr example
The text was updated successfully, but these errors were encountered: