Skip to content
This repository has been archived by the owner on Aug 7, 2018. It is now read-only.

😁 Angular1 + Angular4 + Webpack2 + UI-Router + Lazy Loading + Babel

Notifications You must be signed in to change notification settings

swimlane/ng1-ng4-webpack-lazy-uirouter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

56 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

angular-hybrid-demo

This is a demo project of lazy-loading both AngularJS AND Angular states with ui-router and bundling them with Webpack!

Under the hood this uses:

  • Angular 4.x
  • AngularJS ~1.5.x
  • UI-Router
  • Angular NgUpgrade 4.x
  • ocLazyLoad
  • Webpack 2.x
  • TypeScript 2.x

Running

Nitty-gritty

Structure

β”œβ”€β”€ app
β”‚Β Β  β”œβ”€β”€ admin - ng2 component module
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ admin.component.ts - ng2 module for admin state
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ admin.module.ts - ng2 primary component for admin state
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ admin.state.ts - State definitions for admin state
β”‚Β Β  β”‚Β Β  └── reset  - ng2 sub-state component
β”‚Β Β  β”‚Β Β      └── reset.component.ts - ng2 component
β”‚Β Β  β”‚
β”‚Β Β  β”œβ”€β”€ common - ng1/ng2 common modules
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ common-ng1.module.js - ng1 common module (includes downgraded ng2)
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ common-ng2.module.ts - ng2 common module (includes upgraded ng1)
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ components
β”‚Β Β  β”‚Β Β  β”‚Β Β  β”œβ”€β”€ button.component.js - ng1 component
β”‚Β Β  β”‚Β Β  β”‚Β Β  └── toggle.component.ts - ng2 component
β”‚Β Β  β”‚Β Β  └── services
β”‚Β Β  β”‚Β Β      β”œβ”€β”€ admin.service.js - ng2 service
β”‚Β Β  β”‚Β Β      └── login.service.ts - ng1 service
β”‚Β Β  β”‚
β”‚Β Β  β”œβ”€β”€ login - ng1 component module
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ hello - ng1 subview component
β”‚Β Β  β”‚Β Β  β”‚Β Β  β”œβ”€β”€ hello.controller.js - ng1 controller
β”‚Β Β  β”‚Β Β  β”‚Β Β  └── hello.template.js - es6 template
β”‚Β Β  β”‚Β Β  β”‚
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ login.controller.js - ng1 controller
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ login.module.js - ng1 module
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ login.state.js - login states
β”‚Β Β  β”‚Β Β  └── login.template.js - es6 template
β”‚   β”‚
β”‚Β Β  β”œβ”€β”€ app.module.js - ng1 main app module
β”‚Β Β  β”œβ”€β”€ app.state.js- state definitions for lazy-loading
β”‚Β Β  └── upgrade.module.js - ng2 upgrade module
β”‚
β”œβ”€β”€ utils
β”‚Β Β  β”œβ”€β”€ config-routing.js - Basic config for our ui-router
β”‚Β Β  β”œβ”€β”€ load-ng1-module.js - ocLazyLoad wrapper inside of ui-router transition callback
β”‚
β”œβ”€β”€ adapter.js - angular upgrade adapter
β”œβ”€β”€ bootstrap.js - bootstrap ng1 module via adapter
β”œβ”€β”€ polyfills.js - corejs and such to be loaded by webpack
└── vendor.js - external libs that need to be loaded by webpack

How does this work?

Adapter Magic

In ./adapter.js we create a new Angular2 UpgradeAdapter. During this time we include require in ./app/upgrade.module.js which is the Angular2 master module that is Angular uses under the hood to orchestrate the upgrade process.

If you have components/pipes/providers that need to be registered globally UpgradeModule is the module to do that in. In there you can see where I include CommonNg2Module which contains ng2 providers and components along with upgraded ng1 services and directives.

You might also notice that in the adapter file, I also use require to bring in the UpgradeModule after the adapter. You might also notice that in the UpgradeAdapter I also use forwardRef in the function invocation. This is because several other files need to include and use the adapter. For example, in ./app/common/common-ng2-module.js we import the adapter so we can upgrade ng1 components. Without doing this, the module loader tries to include UpgradeModule which imports CommonNg2Module which imports the adapter. By including the UpgradeModule after the adapter is created the modules load in order, however, because classes aren't hoisted UpgradeModule is not available. We can use the forwardRef function to accomplish this so that at runtime things are brought together at the right time.

Since we are upgrading ng1 components and services, we need a common module for ng1 that is registered at runtime without lazy-loading so that the adapter knows how to resolve those ng1 dependencies.

Its important to note due to the scoping of ng2, we also have to include CommonNg2Module in all the state modules such as AdminModule.

Router Magic

UI-Router has the ability for us to define what we call 'future states'. These are states that tell the router, 'hey i know about something here!' lets load it and then re-run state compilation which now includes the new state metadata.

Ok, thats cool but why not just declare them all in one place? Sometimes your states can get very comprehensive with resolves, substate definitions, etc. All that information would need to be loaded in the initial download causing the payload to increase. Also, all of that logic should live next to where its defined so having the comprehensive states in the module definition is ideal.

Lazy Loading Magic

Webpack allows us to do 'code splitting' which will statically analyze the code to find System.import('some-file.js') and at build time re-write this to reference some magical bundle it made. Its important to note, this is static code analysis, so doing fancy code that does this all black magic will not work since it actually changes your code to a new path. If you use the system-js loader plugin, you can accomplish the later but for this demonstration we do not do that.

Looking at ./app/app.state.js we see I have some System.imports wrapped in 2 different functions. The first function (loadNg1Module) is a Angular1 helper that calls ./utils/load-ng1-module.js function that when invoked will pass the resolve promise from the import to ocLazyLoad to recursively resolve and register all our ng1 modules. loadChildren in will lazy-load the Angular modules for us.

About

😁 Angular1 + Angular4 + Webpack2 + UI-Router + Lazy Loading + Babel

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •