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