• Stars
    star
    504
  • Rank 87,537 (Top 2 %)
  • Language
    TypeScript
  • License
    Apache License 2.0
  • Created over 8 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

πŸ”₯ Angular Hot Module Replacement for Hot Module Reloading

Angular HMR

Angular Hot Module Replacement

Angular-HMR Hot Module Reloading for Webpack and Angular. All versions of Angular and Webpack will work with this module

npm install @angularclass/hmr

hmr-state-dom

main.browser.ts

import { removeNgStyles, createNewHosts, bootloader } from '@angularclass/hmr';

@NgModule({
  bootstrap: [ App ],
  declarations: [ App ],
  imports: [
    // Angular 2
    BrowserModule,
    FormsModule,
    HttpModule,
    RouterModule.forRoot([], {
      useHash: true
    }),
    // app
    appModule
    // vendors
  ],
  providers: []
})
class MainModule {
  constructor(public appRef: ApplicationRef) {}
  hmrOnInit(store) {
    if (!store || !store.state) return;
    console.log('HMR store', store);
    console.log('store.state.data:', store.state.data)
    // inject AppStore here and update it
    // this.AppStore.update(store.state)
    if ('restoreInputValues' in store) {
      store.restoreInputValues();
    }
    // change detection
    this.appRef.tick();
    delete store.state;
    delete store.restoreInputValues;
  }
  hmrOnDestroy(store) {
    var cmpLocation = this.appRef.components.map(cmp => cmp.location.nativeElement);
    // recreate elements
    store.disposeOldHosts = createNewHosts(cmpLocation)
    // inject your AppStore and grab state then set it on store
    // var appState = this.AppStore.get()
    store.state = {data: 'yolo'};
    // store.state = Object.assign({}, appState)
    // save input values
    store.restoreInputValues  = createInputTransfer();
    // remove styles
    removeNgStyles();
  }
  hmrAfterDestroy(store) {
    // display new elements
    store.disposeOldHosts()
    delete store.disposeOldHosts;
    // anything you need done the component is removed
  }
}

export function main() {
  return platformBrowserDynamic().bootstrapModule(MainModule)
    // use `hmrModule` or the "@angularclass/hmr-loader"
    .then((ngModuleRef: any) => {
      // `module` global ref for webpackhmr
      // Don't run this in Prod
      return hmrModule(ngModuleRef, module);
    });
}

// boot on document ready
bootloader(main);

bootloader is only needed to detect that the dom is ready before bootstraping otherwise bootstrap. This is needed because that dom is already ready during reloading.

Important Helpers

  • removeNgStyles: remove angular styles
  • createNewHosts and disposeOldHosts: recreate root elements for bootstrapping
  • bootloader: boot on document ready or boot if it's already ready
  • createInputTransfer and restoreInputValues: transfer input DOM state during replacement

Production

In production you only need bootloader which just does this:

export function bootloader(main) {
  if (document.readyState === 'complete') {
    main()
  } else {
    document.addEventListener('DOMContentLoaded', main);
  }
}

You would bootstrap your app the normal way, in production, after dom is ready. Also, in production, you should remove the loader:


@NGRX/platform (NGRX 4.x.x)

To hook into NGRX 4 you simply need to supply a reducer to set the state, and include it in your development metaReducers.

// make sure you export for AoT
export function stateSetter(reducer: ActionReducer<any>): ActionReducer<any> {
  return function(state: any, action: any) {
    if (action.type === 'SET_ROOT_STATE') {
      return action.payload;
    }
    return reducer(state, action);
  };
}

In your root reducer you can do something like this to include it in your metaReducers. You should access your environment here and only include this in development.

/**
 * By default, @ngrx/store uses combineReducers with the reducer map to compose
 * the root meta-reducer. To add more meta-reducers, provide an array of meta-reducers
 * that will be composed to form the root meta-reducer.
 */
export const metaReducers: ActionReducer<any, any>[] = [stateSetter]

Simply supply the metaReducer to the StoreModule and your hmr is hooked in.

 StoreModule.forRoot(reducers, { metaReducers }),

enjoy β€” PatrickJS

More Repositories

1

PatrickJS-starter

MFE Starter
JavaScript
10,326
star
2

awesome-angular

πŸ“„ A curated list of awesome Angular resources
HTML
9,505
star
3

NG6-starter

πŸ†– An AngularJS Starter repo for AngularJS + ES6 + Webpack
JavaScript
1,906
star
4

angular-websocket

↖️ The missing Angular WebSocket module for connecting client applications to servers by @AngularClass
JavaScript
1,222
star
5

ngExam

An AngularJS exam with questions from beginner to expert by @gdi2290 from @AngularClass
363
star
6

Reddit-Insight

Reddit Analytics built in Angular.js
JavaScript
142
star
7

NG6-todomvc-starter

Basic example of TodoMVC application written with angular and with use of components
JavaScript
138
star
8

angular-md5

md5 for Angular.js and Gravatar filter
JavaScript
133
star
9

angular-idle-preload

πŸ”œ Angular Idle Preload for preloading async routes via @TipeIO
TypeScript
99
star
10

angular-intercom

An Angular.js wrapper for Intercom.io providing a simple and familiar API for Angular Developer.
HTML
89
star
11

angular-raven

A Raven.js / Sentry wrapper for Angular.js
JavaScript
89
star
12

redis-dataloader

Batching and Caching layer using Redis as the Caching layer
JavaScript
86
star
13

node-everything

npm install everything. Please don't actually use this
82
star
14

awesome-cursorrules

πŸ“„ A curated list of awesome .cursorrules files
59
star
15

ng-vegas-angular2-d3

Angular 2, d3, and TypeScript
JavaScript
53
star
16

angular-momentjs

Moment.js with Angular
JavaScript
46
star
17

angular-password

The most performant AngularJS directive for matching two password input fields
JavaScript
45
star
18

data-structures

Data Structures in Javascript, Ruby, and Coffeescript
JavaScript
43
star
19

angular2do

TodoMVC: React setState/Immutable data pattern done in Angular2
HTML
39
star
20

koa-angular-seed

An Angular.js seed for Koa apps with gulp, stylus, and browserify
JavaScript
36
star
21

list-of-morphic-javascript

Javascript Morphisms
36
star
22

angular-crypto

angular-crypto provides standard and secure cryptographic algorithms for Angular.js with support for: MD5, SHA-1, SHA-256, RC4, Rabbit, AES, DES, PBKDF2, HMAC, OFB, CFB, CTR, CBC, Base64
JavaScript
31
star
23

everyone-ssn-usa

releasing everyone's SSN and the hacks used to acquire them
JavaScript
22
star
24

es6-promise-loader

JavaScript
21
star
25

ng-conf-2016-universal-patterns

ng-conf 2016 universal patterns
TypeScript
21
star
26

ng4-universal-demo

TypeScript
21
star
27

ruby_calculator

The simplest form of a calculator made in Ruby 2.0.0
Ruby
20
star
28

core-js-webpack-plugin

core-js builder as a Webpack Plugin
JavaScript
15
star
29

es7-reflect-metadata

Webpack and Universal support for reflect-metadata refactored by @gdi2290
HTML
13
star
30

hackreactor-presentation-slides

my HackReactor presentation
JavaScript
12
star
31

request-idle-callback

requestIdleCallback for Angular
TypeScript
10
star
32

angular-off

Providing the method $off for $rootScope in Angular.js
JavaScript
9
star
33

play-angular2

[WIP] Universal Angular 2 server rendering with Scala Play Framework
JavaScript
9
star
34

angular2-webpack2-seed

angular2 webpack2 with tree-shaking and precompiler??
TypeScript
9
star
35

awesome-JAMstack

A collection of awesome JAMstack starters, workflows, articles, resources and shiny things.
9
star
36

angular-example-basic-form

An AngularJS example for a basic signup form example
JavaScript
9
star
37

angular-d3

d3 for Angular.js
JavaScript
8
star
38

angular-event-emitter

Event emitters for AngularJS
JavaScript
8
star
39

angular-keen.io

An Angular.js wrapper for Keen.io
JavaScript
8
star
40

angular-pokemon

Pokemon directive for Angular.js
JavaScript
8
star
41

generator-easy

An EASY stack generator, Express Angular Stylus Yeoman
CSS
7
star
42

angular-beforeunload

Angular.js service for onBeforeUnload
JavaScript
7
star
43

dokku-bower-gulp-build-plugin

dokku-bower-gulp-build is a plugin for dokku that runs bower install and gulp build in a post-release hook.
7
star
44

angular-cors

Fast CORs support for angular1
JavaScript
6
star
45

ngSanitize

angular-sanitize module without including angular.js for Backbone and other frameworks
JavaScript
6
star
46

angular2-falcor-starter

Angular 2 Universal FalcorJS starter kit by @AngularClass
TypeScript
6
star
47

bower-everything

bower install everything
JavaScript
6
star
48

simple-universal-starter

TypeScript
5
star
49

read-this-Howard

Status: offline!
5
star
50

angular-groupon

An Angular.js wrapper for Groupon API
JavaScript
5
star
51

no-zone.js

No Zone.js allows you to use zone.js API without actually using it. (recommended for advanced use cases only)
JavaScript
5
star
52

angular-uuid-secure

Most secure uuid generator in for Angular.js
JavaScript
4
star
53

angular-balanced

An Angular.js adapter for Balanced.js
4
star
54

angular2-webpack

Webpack helpers for Angular 2 via @AngularClass
JavaScript
4
star
55

ng-click

Passive URL shortening for Angular.JS Docs
JavaScript
4
star
56

github-commit-messages

This chrome extension will show and hide scoped messages so its easier to read quickly
JavaScript
4
star
57

angular-facebook

An Angular.js wrapper for Facebook API
4
star
58

traceur-compiler-loader

Up to date Traceur Compiler loader for Webpack
JavaScript
4
star
59

ngx-loader

ngx-loader
JavaScript
4
star
60

angular2-amp

angular2-amp: nothing to see here
4
star
61

javascript-class-examples

Different examples of OOP "class" with "inheritance" done using JavaScript including languages that transpile into js. Take notice to the amount of boilerplate that's needed in ES5 compared to ES6. These examples all have the same interface with pros/cons for each pattern. If they seem similar that's whole point especially the difference between prototypal and pseudo-classical.
JavaScript
4
star
62

Data-Collaborator

Data visualizations unlock insights and initiate important conversations Our tool, Data Collaborator, enables people to create data visualizations about their Fitbit data, and collaborate with their friends and family, doctors and personal trainers about insights they have about these visualizations
JavaScript
4
star
63

promises

Promises/A+ implementation base on http://promises-aplus.github.io/promises-spec/
JavaScript
3
star
64

git-mv

Keep the git history of your git mv files after undo
Shell
3
star
65

angular-pubnub

A PubNub component for Angular.js
JavaScript
3
star
66

angular-webrtc

3
star
67

angular-algolia

wip
3
star
68

es7-tools

An aggregation of tooling for using ES7 today
3
star
69

angular-async

Async unwrap filters for Angular1 from Angular2 AsyncPipe
HTML
3
star
70

universal-lit-html

Universal Rendering support for lit-html by Polymer
JavaScript
3
star
71

ng2dist

AsyncPipe build of angular 2
JavaScript
3
star
72

angular2-server-rendering

(Initial prototype) Angular 2 Server Rendering
JavaScript
3
star
73

FrontendMasters-2015-02-13-React

React.js Workshop with Ryan Florence
JavaScript
3
star
74

HackerTweet

JavaScript Twitter clone project for HackReactor pre-work made in Rails 4.0.0.beta1 and jQuery
JavaScript
3
star
75

ie-shim

ie-shim for Angular 2
JavaScript
3
star
76

angular-flux

Flux Application Architecture for Angular.js allow AngularJS apps to have unidirectional data flow in a single direction, in a cycle
3
star
77

made-at-hackreactor

Made at HackReactor built in Rails 3.2.13, Backbone.js 1.0.0, Redis 2.6.13, Socket.io 0.9, Node.js 0.10.12
JavaScript
3
star
78

angular-free-style

An Angular.js module for free-style that is designed to make cross-browser style objects easier to work with in JavaScript
JavaScript
3
star
79

angular2-example-falcor

Angular2 + FalcorJS
JavaScript
3
star
80

systemjs-d3

SystemJS wrapper for d3.js
2
star
81

preload_cache.js

PreloadCache: preload_cache defers cache until after the app bootstraps by providing deferred refs. NOTE: In process of moving this repo to angular/universal
HTML
2
star
82

test-static-file

testing static files
HTML
2
star
83

angular2-example-iot

Angular 2 example for Internet of Things
JavaScript
2
star
84

angular-bluebird

An Angular.js wrapper around Bluebird's API as $promise
2
star
85

angular2-universal-cluster

node cluster for Angular 2 Universal
TypeScript
2
star
86

node-hammerjs

Node "support" for hammerjs
JavaScript
2
star
87

gulp-loopback-angular

Gulp plugin for auto-generating Angular $resource services for LoopBack
2
star
88

gdi2290.github.io

HTML
2
star
89

lower-case

Lowercase a string in JavaScript
JavaScript
2
star
90

test-repo_login-to-see-resolved-comment

testing public resolved comments
2
star
91

underbar

this is for learning javascript
JavaScript
2
star
92

angular-reddit

Reddit API for Angular.js
2
star
93

angular-olark

An Angular.js service for Olark a live chat service
2
star
94

PatrickJS

2
star
95

awesome-rx

Reactive Extensions (Rx) is a library for composing asynchronous and event-based programs using observable sequences and LINQ-style query operators.
2
star
96

angular-alertify

JavaScript
2
star
97

is-iojs-production-ready-yet

is io.js production ready yet?
2
star
98

snake-case

Snake case a string in JavaScript
JavaScript
2
star
99

angular2-beta-to-rc-alias

Alias Angular 2 beta.17 3rd party modules to Angular 2 rc.3 modules
TypeScript
2
star
100

ng2for1

angular1 hacks for angular2 style coding
JavaScript
2
star