Skip to content

Instantly share code, notes, and snippets.

@oleksiilevzhynskyi
Last active March 26, 2019 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oleksiilevzhynskyi/29bfa800010bf92b1436f966513deacf to your computer and use it in GitHub Desktop.
Save oleksiilevzhynskyi/29bfa800010bf92b1436f966513deacf to your computer and use it in GitHub Desktop.
/* https://www.npmjs.com/package/webpack-watch-livereload-plugin
* in webpack.config:
* new WatchLiveReloadPlugin({
* files: ['./dist']
* })
*
* somewhere in index.ts
* if (process.env.NODE_ENV === 'development') {
* require('./livereload')()
* }
*/
import * as Rx from 'rxjs'
const LIVERELOAD_HOST = 'localhost'
const LIVERELOAD_PORT = 35729
const url = `ws://${LIVERELOAD_HOST}:${LIVERELOAD_PORT}/livereload`
export default function livereload() {
const connection = new WebSocket(url)
Rx.Observable.fromEvent(connection, 'message')
.skip(1)
.throttleTime(1000)
.subscribe(() => {
console.info('reloading extension')
chrome.runtime.reload()
})
Rx.Observable.fromEvent(connection, 'open').subscribe(() => {
console.info('livereload tunnel enstablished, send reload to active tab')
chrome.tabs.query(
{ active: true },
([{ url, id }]) => url && url.includes('http') && chrome.tabs.reload(id)
)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment