UNPKG

angular2-hmr

Version:

angular2-hmr: Hot Module Replacement for Webpack and Angular 2

105 lines 4 kB
"use strict"; var hmr_store_1 = require('./hmr-store'); function hotModuleReplacement(bootloader, module, options) { if (options === void 0) { options = {}; } if (!module.hot) { console.warn('Warning: please use webpack hot flag'); return document.addEventListener('DOMContentLoaded', function () { return bootloader(); }); } hmr_store_1.HmrStore.dev = true; var LOCALSTORAGE_KEY = options.LOCALSTORAGE_KEY || '@@WEBPACK_INITIAL_DATA'; var LOCAL = options.localStorage || false; var TOKEN = options.storeToken || hmr_store_1.HmrStore; var DISPOSE = options.globalDispose || 'WEBPACK_HMR_beforeunload'; var GET_STATE = options.getState || getState; var DATA = options.data || module.hot.data && module.hot.data.state; var COMPONENT_REF = null; var disposed = false; function getState(appState) { var json = appState.toJSON(); if (LOCAL) { console.time('localStorage'); localStorage.setItem(LOCALSTORAGE_KEY, JSON.stringify(appState)); console.timeEnd('localStorage'); } return json; } console.log('DATA', DATA); if (!DATA && LOCAL) { try { console.time('start localStorage'); DATA = JSON.parse(localStorage.getItem(LOCALSTORAGE_KEY)) || DATA; console.timeEnd('start localStorage'); } catch (e) { console.log('JSON.parse Error', e); } } console.time('bootstrap'); if (document.readyState === 'complete') { bootloader(DATA) .then(function (cmpRef) { return COMPONENT_REF = cmpRef; }) .then((function (cmpRef) { return (console.timeEnd('bootstrap'), cmpRef); })); } else { document.addEventListener('DOMContentLoaded', function () { bootloader(DATA) .then(function (cmpRef) { return COMPONENT_REF = cmpRef; }) .then((function (cmpRef) { return (console.timeEnd('bootstrap'), cmpRef); })); }); } function beforeunload(event) { var injector = COMPONENT_REF.injector; var appState; if ('getOptional' in injector) { appState = COMPONENT_REF.injector.getOptional(TOKEN) || TOKEN; } else { appState = COMPONENT_REF.injector.get(TOKEN, TOKEN); } return GET_STATE(appState); } window[DISPOSE] = function () { disposed = true; window.removeEventListener('beforeunload', beforeunload); if (LOCAL) { localStorage.removeItem(LOCALSTORAGE_KEY); } }; module.hot.accept(); window.addEventListener('beforeunload', beforeunload); module.hot.dispose(function (data) { console.time('dispose'); var componentNode = COMPONENT_REF.location.nativeElement; var newNode = document.createElement(componentNode.tagName); // display none var currentDisplay = newNode.style.display; newNode.style.display = 'none'; var parentNode = componentNode.parentNode; parentNode.insertBefore(newNode, componentNode); var injector = COMPONENT_REF.injector; var appState; if ('getOptional' in injector) { appState = COMPONENT_REF.injector.getOptional(TOKEN) || TOKEN; } else { appState = COMPONENT_REF.injector.get(TOKEN, TOKEN); } var json = GET_STATE(appState, COMPONENT_REF); data.state = json; if ('destroy' in COMPONENT_REF) { COMPONENT_REF.destroy(); } else if ('dispose' in COMPONENT_REF) { COMPONENT_REF.dispose(); } newNode.style.display = currentDisplay; if (!disposed) { window.removeEventListener('beforeunload', beforeunload); } disposed = true; console.timeEnd('dispose'); }); } exports.hotModuleReplacement = hotModuleReplacement; //# sourceMappingURL=webpack-hmr.js.map