/* eslint-disable @typescript-eslint/no-use-before-define */ import { HMR_ACTIONS_SENT_TO_BROWSER } from "../../server/dev/hot-reloader-types"; import { addMessageListener } from "./error-overlay/websocket"; export default function initializeBuildWatcher(toggleCallback, position) { if (position === void 0) position = "bottom-right"; const shadowHost = document.createElement("div"); const [verticalProperty, horizontalProperty] = position.split("-"); shadowHost.id = "__next-build-watcher"; // Make sure container is fixed and on a high zIndex so it shows shadowHost.style.position = "fixed"; // Ensure container's position to be top or bottom (default) shadowHost.style[verticalProperty] = "10px"; // Ensure container's position to be left or right (default) shadowHost.style[horizontalProperty] = "20px"; shadowHost.style.width = "0"; shadowHost.style.height = "0"; shadowHost.style.zIndex = "99999"; document.body.appendChild(shadowHost); let shadowRoot; let prefix = ""; if (shadowHost.attachShadow) { shadowRoot = shadowHost.attachShadow({ mode: "open" }); } else { // If attachShadow is undefined then the browser does not support // the Shadow DOM, we need to prefix all the names so there // will be no conflicts shadowRoot = shadowHost; prefix = "__next-build-watcher-"; } // Container const container = createContainer(prefix); shadowRoot.appendChild(container); // CSS const css = createCss(prefix, { horizontalProperty, verticalProperty }); shadowRoot.appendChild(css); // State let isVisible = false; let isBuilding = false; let timeoutId = null; // Handle events addMessageListener((obj)=>{ try { handleMessage(obj); } catch (e) {} }); function show() { timeoutId && clearTimeout(timeoutId); isVisible = true; isBuilding = true; updateContainer(); } function hide() { isBuilding = false; // Wait for the fade out transition to complete timeoutId = setTimeout(()=>{ isVisible = false; updateContainer(); }, 100); updateContainer(); } function handleMessage(obj) { if (!("action" in obj)) { return; } // eslint-disable-next-line default-case switch(obj.action){ case HMR_ACTIONS_SENT_TO_BROWSER.BUILDING: show(); break; case HMR_ACTIONS_SENT_TO_BROWSER.BUILT: case HMR_ACTIONS_SENT_TO_BROWSER.SYNC: hide(); break; } } toggleCallback({ show, hide }); function updateContainer() { if (isBuilding) { container.classList.add("" + prefix + "building"); } else { container.classList.remove("" + prefix + "building"); } if (isVisible) { container.classList.add("" + prefix + "visible"); } else { container.classList.remove("" + prefix + "visible"); } } } function createContainer(prefix) { const container = document.createElement("div"); container.id = "" + prefix + "container"; container.innerHTML = '\n