Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I do code splitting with webpack on angular 2 app?

I have an app structure like this:

├── /dashboard
│   ├── dashboard.css
│   ├── dashboard.html
│   ├── dashboard.component.ts
│   ├── dashboard.service.ts
│   ├── index.ts
├── /users
│   ├── users.css
│   ├── users.html
│   ├── users.component.ts
│   ├── users.service.ts
│   ├── index.ts
├── /login
│   ├── login.css
│   ├── login.html
│   ├── login.component.ts
│   ├── login.service.ts
│   ├── index.ts
├── app.component.ts
├── app.module.ts
├── app.routes.ts
├── app.styles.css

And I want to code split my app into something like this:

├── dashboard.js
├── users.js
├── login.js
├── app.js

I cannot seem to find an example of how I can do this with webpack. So 2 questions. Can this be done? And, How can this be done?

Any leads or help would be appreciated. I have been researching this all morning.

Angular documentation suggests it here, but no examples or tutorials that I can find. So it's possible, yet no one know how to do it?.

you can find the webpack configuration here

like image 658
Jason Spick Avatar asked Sep 27 '16 15:09

Jason Spick


People also ask

How do you code split in Webpack?

There are three general approaches to code splitting available: Entry Points: Manually split code using entry configuration. Prevent Duplication: Use Entry dependencies or SplitChunksPlugin to dedupe and split chunks. Dynamic Imports: Split code via inline function calls within modules.

What is code splitting in Angular?

Code splitting is achieved in Angular by creating lazy-loaded modules. In order to ensure that a module is lazy loaded and split into its own chunk of JavaScript, you must first create a lazy-loaded route. The following code demonstrates a lazy-loaded route in Angular.

How does Webpack chunking work?

Webpack injects some code into main. js which takes care of lazy loading async chunks and stops from loading same chunks again and again. When a route changes, React router calls a Webpack function to load a chunk file and Webpack after done loading runs it, which chunk then would internally ask React to do something.

Which bundler is used by Angular?

Webpack is probably the most widely used bundler for large Angular applications today. It is also currently the backing bundler for the Angular CLI.


1 Answers

You will have to put each one of them as an entry point

entry: {
  'dashboard': './src/dashboard/index.ts',
  'users': './src/users/index.ts',
  'login': './src/login/index.ts',
  'app': './src/app.module.ts'
}

and then to make sure no code is duplicate across the different entry points set them in the commons chunk plugin. The order is important code encountered in app and subsequently also important in dashboard or users will only show up in the last one it is present/required in.

plugins: [
    new webpack.optimize.CommonsChunkPlugin({
       name: ['app', 'dashboard', 'login', 'users'] 
    })
]

you can also get some inspiration from here: https://angular.io/docs/ts/latest/guide/webpack.html#!#common-configuration

like image 131
Eldin Avatar answered Oct 25 '22 21:10

Eldin