説明なし

livereload.js 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * grunt-contrib-watch
  3. * http://gruntjs.com/
  4. *
  5. * Copyright (c) 2014 "Cowboy" Ben Alman, contributors
  6. * Licensed under the MIT license.
  7. */
  8. 'use strict';
  9. var tinylr = require('tiny-lr-fork');
  10. var _ = require('lodash');
  11. // Holds the servers out of scope in case watch is reloaded
  12. var servers = Object.create(null);
  13. module.exports = function(grunt) {
  14. var defaults = { port: 35729 };
  15. function LR(options) {
  16. if (options === true) {
  17. options = defaults;
  18. } else if (typeof options === 'number') {
  19. options = {port: options};
  20. } else {
  21. options = _.defaults(options, defaults);
  22. }
  23. if (servers[options.port]) {
  24. this.server = servers[options.port];
  25. } else {
  26. this.server = tinylr(options);
  27. this.server.server.removeAllListeners('error');
  28. this.server.server.on('error', function(err) {
  29. if (err.code === 'EADDRINUSE') {
  30. grunt.fatal('Port ' + options.port + ' is already in use by another process.');
  31. } else {
  32. grunt.fatal(err);
  33. }
  34. process.exit(1);
  35. });
  36. this.server.listen(options.port, function(err) {
  37. if (err) { return grunt.fatal(err); }
  38. grunt.log.verbose.writeln('Live reload server started on port: ' + options.port);
  39. });
  40. servers[options.port] = this.server;
  41. }
  42. }
  43. LR.prototype.trigger = function(files) {
  44. grunt.log.verbose.writeln('Live reloading ' + grunt.log.wordlist(files) + '...');
  45. this.server.changed({body:{files:files}});
  46. };
  47. return function(options) {
  48. return new LR(options);
  49. };
  50. };