Created
July 18, 2017 19:52
-
-
Save paulirwin/0262cdede91793c2f6b9efab0b369d76 to your computer and use it in GitHub Desktop.
Webpack Hot Reload Notification with Toastr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <reference path="../types/webpack-hot-middleware.ts" /> | |
import { Message } from "webpack-hot-middleware/client"; | |
import * as Toastr from "toastr"; | |
declare global { | |
interface MessageEventHandler { | |
(event: MessageEvent): void; | |
} | |
interface EventSourceWrapper { | |
addMessageListener(handler: MessageEventHandler): void; | |
} | |
interface WebpackHotMiddlewareEventSourceMap { | |
[key: string]: EventSourceWrapper; | |
} | |
interface Window { | |
__whmEventSourceWrapper?: WebpackHotMiddlewareEventSourceMap; | |
} | |
} | |
if (window.__whmEventSourceWrapper) { | |
for (let key of Object.keys(window.__whmEventSourceWrapper)) { | |
window.__whmEventSourceWrapper[key].addMessageListener(msg => { | |
if (typeof msg.data === "string" && msg.data.startsWith("{")) { | |
const data: Message = JSON.parse(msg.data); | |
if (data && data.action) { | |
switch (data.action) { | |
case "building": | |
Toastr.clear(); | |
Toastr.info("Webpack is building..."); | |
case "built": | |
Toastr.clear(); | |
Toastr.info("Webpack built and reloading..."); | |
} | |
} | |
} | |
}); | |
} | |
} | |
export = null; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "./hot-reload-notifier"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Will not be needed once typings are available | |
declare module "webpack-hot-middleware/client" { | |
export interface Message { | |
action: string; | |
name?: string; | |
hash?: string; | |
} | |
export interface MessageHandler { | |
(obj: Message): void; | |
} | |
export interface Subscriber { | |
(handler: MessageHandler): void; | |
} | |
export var subscribeAll: Subscriber; | |
export var subscribe: Subscriber; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment