Skip to content

Instantly share code, notes, and snippets.

@timthez
Created June 26, 2015 20:03
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save timthez/d1b29ea02cce7a2a59ff to your computer and use it in GitHub Desktop.
Save timthez/d1b29ea02cce7a2a59ff to your computer and use it in GitHub Desktop.
/**
* Closer plugin example
*/
(function ($window, $document, bs) {
var socket = bs.socket;
socket.on("disconnect", function (client) {
window.close();
});
})(window, document, ___browserSync___);
var browserSync = require('browser-sync');
.task('webserver', function() {
browserSync.use({
plugin: function () { /* noop */},
hooks: {
'client:js': require("fs").readFileSync("./closer.js", "utf-8")
}
});
browserSync(['public/**/*.*'],{
port: 9001,
open: true,
server: {
baseDir: './public'
},
ui: {
port: 9002
}
});
})
@electricjesus
Copy link

IIFE uses global window instead of $window arg: https://gist.github.com/timthez/d1b29ea02cce7a2a59ff#file-closer-js-L8

is this intentional?

@adamreisnz
Copy link

@electricjesus I don't think so. I've rewritten the closer to:

/* jshint -W117 */
(function(window, bs) {
  bs.socket.on('disconnect', function() {
    window.close();
  });
})(window, ___browserSync___);

@linus-amg
Copy link

closer.js

___browserSync___.socket.on('disconnect', window.close.bind(window));

thank you for your work on that, it was really annoying, productivity +1

@linus-amg
Copy link

inspired me to do a little module: https://www.npmjs.com/package/browser-sync-close-hook

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment