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

Look for 3rd party modules that perform such imports. Best is to strip them out until you get the lazy route bundles and then isolate the offending one.

To use moment.js, add it as a script to your angular-cli config and do

declare const moment: any;

in your components. This works around the problem.

achimha avatar Jan 17 '17 13:01 achimha

@achimha How to strip them out? i don't want to interfere into node_modules file. I have +100 usages of "import *" in node modules.

I added moment to scripts and declare to typings

tomson7777 avatar Jan 17 '17 13:01 tomson7777

If you're not importing the "wrong" way, then it must be one of your third party modules. The only way to find it is by commenting code out until bundles get generated. I am not aware of an easier way. It's a major PITA and I do not understand the root of the problem now whether it can/will every be fixed.

achimha avatar Jan 17 '17 13:01 achimha

I believe it is a webpack issue. @TheLarkInn is the bundles not being created when using * a cli problem or a webpack problem?

deebloo avatar Jan 17 '17 13:01 deebloo

problem

I am using ng-cli with lazy loading:

...
{path: 'studioweb', pathMatch: 'full', redirectTo: '/App1/Dashboard'},  // IE/FF compatibility
    {
        path: 'App1', component: Appwrap,
        children: [
            {path: '', component: Appwrap, canActivate: [AuthService]},
            {path: 'Dashboard', component: Dashboard, data: {title: 'Dashboard'}, canActivate: [AuthService]},
            {path: 'Campaigns', loadChildren: '../app/campaigns/index#CampaignsLazyModule', data: {title: 'Campaigns'}, canActivate: [AuthService]},
            {path: 'Fasterq', loadChildren: '../app/fasterq/index#FasterqLazyModule', data: {title: 'Fasterq'}, canActivate: [AuthService]},
...

I generate the build dist with:

ng build --target=production --base-href ./

and I don't see any chunks. and while all "seems" to work, I am not sure lazy loading is working, almost as if Webpack bundles it all in?!?!?!

How can I confirm if lazy loading is working? Why are chunks not created? bug?

OS?

angular-cli: 1.0.0-beta.25.5 node: 6.5.0 os: win32 x64 @angular/common: 2.4.4 @angular/compiler: 2.4.4 @angular/core: 2.4.4 @angular/forms: 2.4.4 @angular/http: 2.4.4 @angular/language-service: 2.4.4 @angular/platform-browser: 2.4.4 @angular/platform-browser-dynamic: 2.4.4 @angular/router: 3.2.1 @angular/compiler-cli: 2.4.4

snap:

nochunks

Angular 2 Kitchen sink: http://ng2.javascriptninja.io and source@ https://github.com/born2net/Angular-kitchen-sink Regards,

Sean

born2net avatar Jan 19 '17 20:01 born2net

It doesn't matter if the offending import is in a lazy bundle, the main bundle or a 3rd party module. As soon as it appears somewhere, bundling is broken.

achimha avatar Jan 19 '17 20:01 achimha

yes I do have the following all over my code:

import * as Immutable from "immutable";
import * as _ from "lodash";
import * as moment_ from "moment";

Angular 2 Kitchen sink: http://ng2.javascriptninja.io and source@ https://github.com/born2net/Angular-kitchen-sink Regards,

Sean

born2net avatar Jan 19 '17 20:01 born2net

I can confirm this behaviour too in CLI 25.5 - lazy loading and chunking work fine until I add any of these in a Feature module and then chunking stops chunking so to speak... import { Moment } from 'moment'; import { MomentModule } from 'angular2-moment';

rodneyjoyce avatar Jan 20 '17 00:01 rodneyjoyce

FYI, for those who are using Angular2-moment and moment, I have come to the conclusion that while I can get moment to work (with Locales, using the workarounds above) - it is not possible to use with angular2-moment pipes at this stage: https://github.com/urish/angular2-moment/issues/114

rodneyjoyce avatar Jan 20 '17 17:01 rodneyjoyce

Not a fix for import * as breaking Lazy loading, but I have a workaround for those using angular2-moment.

I was previously using angular2-moment for the DateFormatPipe (for cross-browser formatting). That adds 50kb of code for one pipe and currently breaks Lazy-loading: screenshot 2017-01-21 17 23 07

Solution: switch to date-fns instead, it's modern, modular, smaller and works with lazy-loading:

Site: https://date-fns.org/ Project: https://github.com/date-fns/date-fns Example pipe for formatting a date:

import { Pipe, PipeTransform } from '@angular/core';
import { format } from 'date-fns';

@Pipe({
  name: 'fnsDateFormat'
})
export class DateFormatPipe implements PipeTransform {
  transform(value: Date | string | number, ...args: any[]): any {
    if (!value) return '';
    return format(value, args[0]);
  }
}

And from switching, now it uses 27.8kb and I have lazy-loading again: screenshot 2017-01-21 17 39 04

intellix avatar Jan 21 '17 16:01 intellix

Yes, I did the exact same thing.

rodneyjoyce avatar Jan 21 '17 17:01 rodneyjoyce

https://github.com/angular/angular-cli/pull/4153 should fix this problem.

filipesilva avatar Jan 22 '17 00:01 filipesilva

@born2net Are you using the current master directly with something like npm link angular-cli or traditionally installing/using the CLI? Thanks!

jwuliger avatar Jan 22 '17 04:01 jwuliger

ha sorry I installed via npm, I figured it was latest as I saw .26, I guess its its not published yet? if so I will wait, np

born2net avatar Jan 22 '17 04:01 born2net

@born2net You can do what I have been doing to use the latest changes in my project.

Check out: https://github.com/angular/angular-cli#development-hints-for-hacking-on-angular-cli

I am setting up to test this new change myself.

jwuliger avatar Jan 22 '17 04:01 jwuliger

tried it but I could not get the symlink to work. prob cause I am on windows, will wait for npm relase to check, tx!

born2net avatar Jan 22 '17 04:01 born2net

FYI, here is another TimeAgo filter if you are looking to swap Moment for DateFNS and need a pipe for the template (including localisation). This replaces the angular2-moment pipes which are not currently compatible with chunking Usage: <div>Date with TimeAgo DateFNS Pipe : {{this.exampleDateTime | timeAgo : true }}</div>

import { Pipe, PipeTransform } from '@angular/core';
import * as distanceInWordsToNow from 'date-fns/distance_in_words_to_now'
import * as esLocale from 'date-fns/locale/es/index.js'

@Pipe({
    name: 'timeAgo'
})
export class TimeAgoPipe implements PipeTransform {
    transform(value: Date | string | number, ...args: any[]): any {
        if (!value) return '';
        return distanceInWordsToNow(value, { addSuffix: args[0], locale: esLocale });
    }
}

rodneyjoyce avatar Jan 22 '17 18:01 rodneyjoyce

seems like .28 fixed chunks, but AOT broken so can't 100% test yet.

born2net avatar Feb 01 '17 22:02 born2net

looks like build .28-3 still does not generate any chunks for lazy loading so .28 worked with chunks (had diff issues) and 28-3 looks good (no crashes) but no chunks just want to make sure devs are aware of this regards

Sean

born2net avatar Feb 02 '17 20:02 born2net

@born2net for me it does and I have lot of import * as moment in my code. Try to update @ngtools/webpack to latest version.

JiriBalcar avatar Feb 02 '17 21:02 JiriBalcar