angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

Lazy loading is not working together with 'import *' syntax

Open areijngoudt opened this issue 10 years ago • 85 comments

Please provide us with the following information:

OS?

Mac OSX El Capitan

Versions.

angular-cli: 1.0.0-beta.16 node: 5.7.1 os: darwin x64

Repro steps.

Was this an app that wasn't created using the CLI? What change did you do on your code? etc. The app was not created using the CLI. When upgrading to Angular 2 I choose to use the CLI as serving and building tool

I have this route in my app.routing.ts:

const ROUTES: Routes = [
    { path: '', redirectTo: 'analyse', pathMatch: 'full' },
    { path: 'annotate', component: AnnotateComponent },
    { path: 'fraud', component: FraudComponent },
    { path: 'info',  loadChildren: 'app/info/info.module#InfoModule'  },
];```

the InfoModule should be loaded lazily when navigating to it's path. But even in de --prod build there's no seperate bundle. The InfoModule is still part of the main.bundle.js.

Note that I'm still importing the InfoModule in the app.module.ts otherwise the info route does not work at all.

areijngoudt avatar Oct 04 '16 09:10 areijngoudt

Thanks a lot @aaronleeucla (may you get struck with good fortune today). I couldn't figure out why Webpack was no longer generating separate chunk.js files for my lazy routes. Your solution worked for me. Moving import * as moment ... components into lazy loaded modules/routes (thus removing them from the eager ones) fixed this.

wheredoesyourmindgo avatar Oct 30 '16 21:10 wheredoesyourmindgo

Any progress with this issue @filipesilva? It has label "priority: 1 (urgent)" almost one month. It is blocking me to update my project to newest angular-cli version.

JiriBalcar avatar Nov 01 '16 12:11 JiriBalcar

I have the same problem with angular-cli beta18.

With beta16, with an application using momentjs (and import * as moment from ''), I did not have any issues.

@aaronleeucla Which version of angular-cli are you using ?

Manu

EmmanuelDemey avatar Nov 02 '16 09:11 EmmanuelDemey

For what's worth, I've the same problem too using angular-cli beta18

daco avatar Nov 02 '16 10:11 daco

Is this related only to importing moment.js ? Because I am using angular-cli 18 and everything seems to work fine regarding lazy loading

Mihai-B avatar Nov 02 '16 11:11 Mihai-B

@Mihai-B Sorry, yes it is related to importing moment.js.

daco avatar Nov 02 '16 11:11 daco

This is really breaking everything.. I'm facing the exact same issue. A Fix for this is much appreciated.

I even tried with angular-cli: 1.0.0-beta.19-3 .. but the issue still exists.

Looks like this issue was introduced in beta.18

a5hik avatar Nov 05 '16 04:11 a5hik

@a5hik

Looks like this issue was introduced in beta.18

I also tried with beta.17 and even 16 and have the same issue =)

serhiisol avatar Nov 09 '16 12:11 serhiisol

Help plz, trying to lazy load one module getting same issue

Error: Cannot match any routes. URL Segment: 'admin'

i am using:

angular-cli: 1.0.0-beta.19-3 node: 6.7.0 os: win32 x64

"ng2-bootstrap": "^1.1.16",

package.json

"dependencies": { "@angular/common": "2.1.2", "@angular/compiler": "2.1.2", "@angular/core": "2.1.2", "@angular/forms": "2.1.2", "@angular/http": "2.1.2", "@angular/platform-browser": "2.1.2", "@angular/platform-browser-dynamic": "2.1.2", "@angular/router": "~3.1.0", "bootstrap": "^3.3.7", "core-js": "^2.4.1", "ng2-bootstrap": "^1.1.16", "ng2-sidebar": "^1.6.2", "rxjs": "5.0.0-beta.12", "ts-helpers": "^1.1.1", "zone.js": "^0.6.23" }, "devDependencies": { "@types/jasmine": "^2.2.30", "@types/node": "^6.0.42", "angular-cli": "1.0.0-beta.19-3", "codelyzer": "1.0.0-beta.1", "jasmine-core": "2.4.1", "jasmine-spec-reporter": "2.5.0", "karma": "1.2.0", "karma-chrome-launcher": "^2.0.0", "karma-cli": "^1.0.1", "karma-jasmine": "^1.0.2", "karma-remap-istanbul": "^0.2.1", "protractor": "4.0.9", "ts-node": "1.2.1", "tslint": "3.13.0", "typescript": "~2.0.3", "webdriver-manager": "10.2.5" }

VinayLeaf avatar Nov 10 '16 11:11 VinayLeaf

You will have to wait for beta 20, lazy loading isn't really working yet.

achimha avatar Nov 10 '16 11:11 achimha

@achimha What do you mean? Of course it's working, even with 3rd party libs using the workaround mentioned above.

@VinayLeaf post your question with code on stackoverflow and I can help you there

alexciesielski avatar Nov 10 '16 11:11 alexciesielski

The workaround means modifying the library and if the library depends on a library which depends on a library etc. the workaround isn't really workable.

However, beta 20 is probably out very soon and then things should be much better.

achimha avatar Nov 10 '16 11:11 achimha

agree with @ciesielskico, it's really working with workaround. Tried with latest cli

serhiisol avatar Nov 11 '16 10:11 serhiisol

Can anyone tell me that how to use that workaround???

On 11-Nov-2016 3:45 pm, "Serhii Sol" [email protected] wrote:

agree with @ciesielskico https://github.com/ciesielskico, it's really working with workaround. Tried with latest cli

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/angular/angular-cli/issues/2496#issuecomment-259926017, or mute the thread https://github.com/notifications/unsubscribe-auth/AQo1nUyqcETorUxa08j3-wesrSvDsn4fks5q9EBWgaJpZM4KNcjN .

VinayLeaf avatar Nov 11 '16 10:11 VinayLeaf

@VinayLeaf All you need to do is to avoid wildcard imports e.g.:

import * as moment from 'moment'

instead you need to include import into angular-cli.json e.g.:

"scripts": [
"../node_modules/moment/moment.js"
]

and then add typing for moment e.g.:

declare var moment: any;

And that's pretty it. I hope that will help you :)

PS. also you can try to import it as polyfill, but declaration of variable still should be added somewhere in types

serhiisol avatar Nov 11 '16 10:11 serhiisol

Need to declare var instead of import * as momment from 'moment'?? Every where??

On 11-Nov-2016 3:51 pm, "Serhii Sol" [email protected] wrote:

@VinayLeaf https://github.com/VinayLeaf All you need to do is to avoid wildcard imports e.g.:

import * as moment from 'moment'

instead you need to include import into angular-cli.json e.g.:

"scripts": ["../node_modules/moment/moment.js" ]

and then add typing for moment e.g.:

declare var moment: any;

And that's pretty it. I hope that will help you :)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/angular/angular-cli/issues/2496#issuecomment-259926983, or mute the thread https://github.com/notifications/unsubscribe-auth/AQo1nYYjRRUqXfsSGuhD3q8jyjpjcwmTks5q9EGfgaJpZM4KNcjN .

VinayLeaf avatar Nov 11 '16 10:11 VinayLeaf

@VinayLeaf only once in typings somewhere (where's your types) and then use moment as global.

serhiisol avatar Nov 11 '16 10:11 serhiisol

@serhiisol i found 2 occurrences of import * as moment from 'moment' in following location

node_modules\awesome-typescript-loader\issues\product-designer\src\components\products node_modules\awesome-typescript-loader\issues\product-designer\src\constants

than i have replaced import * as moment from 'moment' to import * from 'moment'

i have included following code in angular-cli.json

"scripts": [ "../node_modules/moment/moment.js" ]

than i have declared declare var moment: any; in typings.d.ts

still i am getting following error

error_handler.js:47 EXCEPTION: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'admin'

am i doing something wrong?

if you want to look at my file how they looks: than got this link http://stackoverflow.com/questions/40531095/lazy-loading-not-working-with-angular-cli-webpack

VinayLeaf avatar Nov 12 '16 06:11 VinayLeaf

@serhiisol i found that if i am not using ng2-bootstrap module than my lazy loading is working fine. and i also looked for import * as moment from 'moment' but i foundvar moment = require('moment'); i also tried to user workaround but still its not working.

VinayLeaf avatar Nov 12 '16 12:11 VinayLeaf

@serhiisol i found solution i was doing mistake in my code, its my bad.

look at here: http://stackoverflow.com/questions/40531095/lazy-loading-not-working-with-angular-cli-webpack

VinayLeaf avatar Nov 12 '16 13:11 VinayLeaf