browser-sync.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * grunt-browser-sync
  3. * https://github.com/BrowserSync/grunt-browser-sync
  4. *
  5. * Copyright (c) 2015 Shane Osbourne
  6. * Licensed under the MIT license.
  7. */
  8. 'use strict';
  9. module.exports = function (grunt) {
  10. var bs = require("browser-sync").create("Grunt");
  11. grunt.registerMultiTask("browserSync", "Keep your browsers in sync", function () {
  12. var done = this.async();
  13. var options = this.options({
  14. keepalive: true
  15. });
  16. var patterns;
  17. if (this.data && this.data.bsFiles && this.data.bsFiles.src) {
  18. patterns = this.data.bsFiles.src;
  19. if (typeof patterns === "string") {
  20. patterns = [patterns];
  21. }
  22. }
  23. if (!patterns) {
  24. if (this.data.src) {
  25. patterns = this.data.src;
  26. if (typeof this.data.src === "string") {
  27. patterns = [this.data.src];
  28. }
  29. }
  30. }
  31. if (!patterns) {
  32. if (this.filesSrc.length) {
  33. patterns = this.filesSrc;
  34. }
  35. }
  36. bs.init(patterns || [], options, function (err) {
  37. if (err) {
  38. done(err);
  39. return;
  40. }
  41. if (options.watchTask ||
  42. options.watchtask ||
  43. options.background ||
  44. !options.keepalive) {
  45. done();
  46. }
  47. });
  48. });
  49. grunt.registerMultiTask("bsReload", function () {
  50. if (bs && bs.active) {
  51. bs.reload(this.data.reload);
  52. }
  53. });
  54. grunt.registerMultiTask("bsNotify", function () {
  55. if (bs && bs.active) {
  56. bs.notify(this.data.notify, this.data.timeout);
  57. }
  58. });
  59. };