Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bundle lazy loaded components inside the production dist folder in angular-cli?

I am using angular-cli for development and I have used the following commands and code to build my project.

npm install angular-cli (angular-cli: 1.0.0-beta.10)

ng new my-app

ng g component lazy-me

Then added a file app.router.ts with the following script

import { provideRouter, RouterConfig } from '@angular/router';
import { AppComponent } from './app.component';
// import { LazyMeComponent } from './+lazy-me/lazy-me.component';

const appRoutes : RouterConfig = [
  {path: '', component: AppComponent},
//   {path: 'lazyme', component: LazyMeComponent}
  {path: 'lazyme', component: 'app/+lazy-me#LazyMeComponent'}
];

export const APP_ROUTER_PROVIDER = [
    provideRouter(appRoutes)
];

And changed my main.ts as following

import { bootstrap } from '@angular/platform-browser-dynamic';
import { enableProdMode,
    SystemJsComponentResolver, 
    ComponentResolver } from '@angular/core';
import {RuntimeCompiler} from '@angular/compiler';
import { AppComponent, environment } from './app/';

import { APP_ROUTER_PROVIDER } from './app/app.router';

if (environment.production) {
    enableProdMode();
}

bootstrap(AppComponent,[
  APP_ROUTER_PROVIDER,
  {
    provide: ComponentResolver,
    useFactory: (r) => new SystemJsComponentResolver(r),
    deps: [RuntimeCompiler]
  },
]);

And to do a production build I have used the following command ng build -prod

When I deploy my code to a webserver and navigate to lazyme path, I get 404 error for app/lazy-me/lazy-me.component.js

The folder exists but lazy-me.component.js is missing as expected as everything gets bundled in main.js except .css and .html files. However, I want ng build -prod to include lazy-me.component.js in dist/app/lazy-me/.

Is there any settings in system-config.ts or anywhere else where I can include lazy loaded components to be part of the dist folder when doing a -prod build?

like image 776
Gan Avatar asked Jul 31 '16 18:07

Gan


People also ask

What is the use of dist folder in Angular?

The dist folder is the build folder which contains all the files and folders which can be hosted in the server. The dist folder contains the compiled code of your angular application in the format of JavaScript and also the required HTML and CSS files.

What is needed to set up a lazy loaded feature module in Angular?

To lazy load Angular modules, use loadChildren (instead of component ) in your AppRoutingModule routes configuration as follows. content_copy const routes: Routes = [ { path: 'items', loadChildren: () => import('./items/items. module'). then(m => m.

Can component be lazy loaded in Angular?

You can lazily load a component in any other component, hence creating a parent-child relationship between them. You want to lazy load GreetComponent on the click of the button in the parent component, so to do that add a button as shown next.

How do you use a lazy load module without routing?

define where we want to load our component in the template with the ng-template tag, define its view query through ViewChild decorator, which gives us access to the DOM and defines the container to which the component will be added, finally, dynamic import the component and add it to the container.


1 Answers

It appears your 'lazy' component is not actually loaded lazily in your router configuration. First, you need to wrap it in its own module, which you want to load lazily. Then, you need the 'loadChildren' property in your router configuration to refer to that module, rather than referring to the component directly.

This article might shed some more light onto the entire issue, hope it doesn't go stale and helps: https://vsavkin.com/angular-router-declarative-lazy-loading-7071d1f203ee

Also, to debug issues like this: if you are willing to upgrade your version of Angular CLI to at least beta.32, they have added an 'eject' command similar to create-react-app. Using that, you get access to the webpack config file. Using this, you can change the production build to fit your needs, if the default CLI is not working as expected. I'm aware this is kind of a work-around though. Good luck!

like image 166
jberndsen Avatar answered Oct 10 '22 11:10

jberndsen