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

Anybody get the workaround working with AOT? (I'm using 1.0.0-beta.20-4) Without AOT it just shows warning "Cannot find namespace 'moment'." for each component file that I use moment in but app still runs, With AOT I get

ERROR in ./src/app/app.module.ngfactory.ts
Module build failed: Error: <my component that uses moment>): Cannot find namespace 'moment'.)
.....

And app doesnt start.

slubowsky avatar Nov 16 '16 15:11 slubowsky

I've just read this thread up to the first comment mentioning momentjs. I remembered that I had this similar issue once, declaring moment in angular-cli.json scripts fixed it.

I don't know if this solution has been provided, as I said, I haven't read the entire thing. If not, you could try that.

monojack avatar Nov 21 '16 18:11 monojack

angular-cli: 1.0.0-beta.20-4 node: 7.0.0 os: darwin x64

The lazy loading still not working.. with aot I was able to generate the map files for all the modules. But only getting one main chunk created.. no module chunks. So everything gets loaded when the app loads with one big main chunk.

a5hik avatar Nov 22 '16 01:11 a5hik

Adding simply the below Fixed the issue..

"../node_modules/moment/moment.js" in angular-cli.json

But this should be a workaround.. and not the actual Fix.

a5hik avatar Nov 22 '16 01:11 a5hik

Lazy loading is still not working for me...

angular-cli: 1.0.0-beta.21
node: 6.9.1
os: win32 x64

Sanderand avatar Nov 30 '16 20:11 Sanderand

PSA: My previously suggested workaround (declare var moment instead of importing moment) should also be applied to code in lazy modules as well. Per my issue reference above, I found that if one were to use import * as moment from 'momemt in a lazy module, that module will then duplicate the code from all other lazy chunks. bad!

However, even if you don't import moment, if one of the dependency in a lazy module uses such import, such as ng2-bootstrap's datepicker, the module will still duplicate. sad!

aaronleestic avatar Dec 06 '16 06:12 aaronleestic

@Gillespie59 I've been running beta.22 / angular 2.2.3 and this bug still appears.

Correction, when I say it appears, I mean in the form of lazy chunks code being duplicated, per #3410 . I have not reproducing this bug by having import moment the eager module.

aaronleestic avatar Dec 08 '16 22:12 aaronleestic

Just upgraded to beta.22 from beta.18. Lazy loading is still not working but instead of not generating the chunks now my application does not run at all.

As my project comes from several angualr-cli upgrades I created a brand new project to be sure the error is not related to my code. When I try to navigate to a lazy route I get this error:

error_handler.js:47EXCEPTION: Uncaught (in promise): Error: Cannot find module 'app/my-route/my-route.module'.
Error: Cannot find module 'app/my-route/my-route.module'.
    at webpackEmptyContext (http://localhost:4200/main.bundle.js:41:8)
    at SystemJsNgModuleLoader.loadAndCompile (http://localhost:4200/vendor.bundle.js:51252:40)
    at SystemJsNgModuleLoader.load (http://localhost:4200/vendor.bundle.js:51244:60)
    at RouterConfigLoader.loadModuleFactory (http://localhost:4200/vendor.bundle.js:66070:128)
    at RouterConfigLoader.load (http://localhost:4200/vendor.bundle.js:66061:81)
    at MergeMapSubscriber.project (http://localhost:4200/vendor.bundle.js:68848:111)
    at MergeMapSubscriber._tryNext (http://localhost:4200/vendor.bundle.js:66195:27)
    at MergeMapSubscriber._next (http://localhost:4200/vendor.bundle.js:66185:18)
    at MergeMapSubscriber.Subscriber.next (http://localhost:4200/vendor.bundle.js:41971:18)
    at ScalarObservable._subscribe (http://localhost:4200/vendor.bundle.js:68111:24)

cladera avatar Dec 13 '16 10:12 cladera

I also struggled with loadChildren a whole workday and discovered in the source code, that using backticks ` will not work, because it's not ts.SyntaxKind.StringLiteral. You have to use double- " or singlequotes ' for enclosing the loadChildren path. Maybe some of you stepped into the same pitfall than me.

sclausen avatar Dec 13 '16 10:12 sclausen

@sclausen I am already using single quotes. I've tested with double quotes and I get the same error...

cladera avatar Dec 13 '16 11:12 cladera

@filipesilva thanks for all your hardwork on improving the cli. Do you or anyone know whether this bug is on the angular-cli side or on the angular framework side? If the latter, I might look to open a bug on their github issues instead.

aaronleestic avatar Dec 21 '16 16:12 aaronleestic

Lazy loading is still not work [angular-cli beta.24]

vthinkxie avatar Dec 29 '16 02:12 vthinkxie

super urgent ticket :)

serhiisol avatar Jan 12 '17 16:01 serhiisol

I solved it by getting rid of everything importing moment.js. Could not find any other way.

achimha avatar Jan 12 '17 16:01 achimha

It seems that importing any lib via * as thing. Will cause bundles not to be created

deebloo avatar Jan 12 '17 16:01 deebloo

@deebloo but you mean using * in lazy modules only? or wherever you use "import *" in app, it will crush creating bundles?

i am trying to add lazy load, i have no errors and Augury dev tool shows that i have lazy route. But when i enter a lazy module, then in Network in dev tools, i don't see any bundle being downloaded.

It's normal? i see only main.bundle.js

tomson7777 avatar Jan 17 '17 12:01 tomson7777

Whenever you use import * (at least in my experience)

deebloo avatar Jan 17 '17 12:01 deebloo

@deebloo but only in our app, or in Node_modules too? if in Node_modules too, then it's impossible to use lazy load...

tomson7777 avatar Jan 17 '17 12:01 tomson7777

@tomasznastaly I cannot speak with 100% certainty but that is what it seems like

deebloo avatar Jan 17 '17 12:01 deebloo

@deebloo

I removed * from my moment usages. No errors, Augury shows the route is lazy, i can enter these module. But still the separated bundle is not created, only main.bundle.js on app loading: /

i use Angular 2.4.1 and Angular-CLI-1.0.0.25-5 you achieved working lazy routes?

tomson7777 avatar Jan 17 '17 13:01 tomson7777