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