UNPKG

4 kBJavaScriptView Raw
1"use strict";
2var hmr_store_1 = require('./hmr-store');
3function hotModuleReplacement(bootloader, module, options) {
4 if (options === void 0) { options = {}; }
5 if (!module.hot) {
6 console.warn('Warning: please use webpack hot flag');
7 return document.addEventListener('DOMContentLoaded', function () { return bootloader(); });
8 }
9 hmr_store_1.HmrStore.dev = true;
10 var LOCALSTORAGE_KEY = options.LOCALSTORAGE_KEY || '@@WEBPACK_INITIAL_DATA';
11 var LOCAL = options.localStorage || false;
12 var TOKEN = options.storeToken || hmr_store_1.HmrStore;
13 var DISPOSE = options.globalDispose || 'WEBPACK_HMR_beforeunload';
14 var GET_STATE = options.getState || getState;
15 var DATA = options.data || module.hot.data && module.hot.data.state;
16 var COMPONENT_REF = null;
17 var disposed = false;
18 function getState(appState) {
19 var json = appState.toJSON();
20 if (LOCAL) {
21 console.time('localStorage');
22 localStorage.setItem(LOCALSTORAGE_KEY, JSON.stringify(appState));
23 console.timeEnd('localStorage');
24 }
25 return json;
26 }
27 console.log('DATA', DATA);
28 if (!DATA && LOCAL) {
29 try {
30 console.time('start localStorage');
31 DATA = JSON.parse(localStorage.getItem(LOCALSTORAGE_KEY)) || DATA;
32 console.timeEnd('start localStorage');
33 }
34 catch (e) {
35 console.log('JSON.parse Error', e);
36 }
37 }
38 console.time('bootstrap');
39 if (document.readyState === 'complete') {
40 bootloader(DATA)
41 .then(function (cmpRef) { return COMPONENT_REF = cmpRef; })
42 .then((function (cmpRef) { return (console.timeEnd('bootstrap'), cmpRef); }));
43 }
44 else {
45 document.addEventListener('DOMContentLoaded', function () {
46 bootloader(DATA)
47 .then(function (cmpRef) { return COMPONENT_REF = cmpRef; })
48 .then((function (cmpRef) { return (console.timeEnd('bootstrap'), cmpRef); }));
49 });
50 }
51 function beforeunload(event) {
52 var injector = COMPONENT_REF.injector;
53 var appState;
54 if ('getOptional' in injector) {
55 appState = COMPONENT_REF.injector.getOptional(TOKEN) || TOKEN;
56 }
57 else {
58 appState = COMPONENT_REF.injector.get(TOKEN, TOKEN);
59 }
60 return GET_STATE(appState);
61 }
62 window[DISPOSE] = function () {
63 disposed = true;
64 window.removeEventListener('beforeunload', beforeunload);
65 if (LOCAL) {
66 localStorage.removeItem(LOCALSTORAGE_KEY);
67 }
68 };
69 module.hot.accept();
70 window.addEventListener('beforeunload', beforeunload);
71 module.hot.dispose(function (data) {
72 console.time('dispose');
73 var componentNode = COMPONENT_REF.location.nativeElement;
74 var newNode = document.createElement(componentNode.tagName);
75 // display none
76 var currentDisplay = newNode.style.display;
77 newNode.style.display = 'none';
78 var parentNode = componentNode.parentNode;
79 parentNode.insertBefore(newNode, componentNode);
80 var injector = COMPONENT_REF.injector;
81 var appState;
82 if ('getOptional' in injector) {
83 appState = COMPONENT_REF.injector.getOptional(TOKEN) || TOKEN;
84 }
85 else {
86 appState = COMPONENT_REF.injector.get(TOKEN, TOKEN);
87 }
88 var json = GET_STATE(appState, COMPONENT_REF);
89 data.state = json;
90 if ('destroy' in COMPONENT_REF) {
91 COMPONENT_REF.destroy();
92 }
93 else if ('dispose' in COMPONENT_REF) {
94 COMPONENT_REF.dispose();
95 }
96 newNode.style.display = currentDisplay;
97 if (!disposed) {
98 window.removeEventListener('beforeunload', beforeunload);
99 }
100 disposed = true;
101 console.timeEnd('dispose');
102 });
103}
104exports.hotModuleReplacement = hotModuleReplacement;
105//# sourceMappingURL=webpack-hmr.js.map
\No newline at end of file