| 1 | import hotEmitter from "webpack/hot/emitter.js";
|
|---|
| 2 | import { log } from "./log.js";
|
|---|
| 3 |
|
|---|
| 4 | /** @typedef {import("../index").Options} Options
|
|---|
| 5 | /** @typedef {import("../index").Status} Status
|
|---|
| 6 |
|
|---|
| 7 | /**
|
|---|
| 8 | * @param {Options} options
|
|---|
| 9 | * @param {Status} status
|
|---|
| 10 | */
|
|---|
| 11 | function reloadApp(_ref, status) {
|
|---|
| 12 | var hot = _ref.hot,
|
|---|
| 13 | liveReload = _ref.liveReload;
|
|---|
| 14 | if (status.isUnloading) {
|
|---|
| 15 | return;
|
|---|
| 16 | }
|
|---|
| 17 | var currentHash = status.currentHash,
|
|---|
| 18 | previousHash = status.previousHash;
|
|---|
| 19 | var isInitial = currentHash.indexOf( /** @type {string} */previousHash) >= 0;
|
|---|
| 20 | if (isInitial) {
|
|---|
| 21 | return;
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| 24 | /**
|
|---|
| 25 | * @param {Window} rootWindow
|
|---|
| 26 | * @param {number} intervalId
|
|---|
| 27 | */
|
|---|
| 28 | function applyReload(rootWindow, intervalId) {
|
|---|
| 29 | clearInterval(intervalId);
|
|---|
| 30 | log.info("App updated. Reloading...");
|
|---|
| 31 | rootWindow.location.reload();
|
|---|
| 32 | }
|
|---|
| 33 | var search = self.location.search.toLowerCase();
|
|---|
| 34 | var allowToHot = search.indexOf("webpack-dev-server-hot=false") === -1;
|
|---|
| 35 | var allowToLiveReload = search.indexOf("webpack-dev-server-live-reload=false") === -1;
|
|---|
| 36 | if (hot && allowToHot) {
|
|---|
| 37 | log.info("App hot update...");
|
|---|
| 38 | hotEmitter.emit("webpackHotUpdate", status.currentHash);
|
|---|
| 39 | if (typeof self !== "undefined" && self.window) {
|
|---|
| 40 | // broadcast update to window
|
|---|
| 41 | self.postMessage("webpackHotUpdate".concat(status.currentHash), "*");
|
|---|
| 42 | }
|
|---|
| 43 | }
|
|---|
| 44 | // allow refreshing the page only if liveReload isn't disabled
|
|---|
| 45 | else if (liveReload && allowToLiveReload) {
|
|---|
| 46 | var rootWindow = self;
|
|---|
| 47 |
|
|---|
| 48 | // use parent window for reload (in case we're in an iframe with no valid src)
|
|---|
| 49 | var intervalId = self.setInterval(function () {
|
|---|
| 50 | if (rootWindow.location.protocol !== "about:") {
|
|---|
| 51 | // reload immediately if protocol is valid
|
|---|
| 52 | applyReload(rootWindow, intervalId);
|
|---|
| 53 | } else {
|
|---|
| 54 | rootWindow = rootWindow.parent;
|
|---|
| 55 | if (rootWindow.parent === rootWindow) {
|
|---|
| 56 | // if parent equals current window we've reached the root which would continue forever, so trigger a reload anyways
|
|---|
| 57 | applyReload(rootWindow, intervalId);
|
|---|
| 58 | }
|
|---|
| 59 | }
|
|---|
| 60 | });
|
|---|
| 61 | }
|
|---|
| 62 | }
|
|---|
| 63 | export default reloadApp; |
|---|