You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
4.0 KiB

  1. // Generated by CoffeeScript 1.4.0
  2. var __hasProp = {}.hasOwnProperty,
  3. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  4. module.exports = function(BasePlugin) {
  5. var LivereloadPlugin;
  6. return LivereloadPlugin = (function(_super) {
  7. __extends(LivereloadPlugin, _super);
  8. function LivereloadPlugin() {
  9. return LivereloadPlugin.__super__.constructor.apply(this, arguments);
  10. }
  11. LivereloadPlugin.prototype.name = 'livereload';
  12. LivereloadPlugin.prototype.config = {
  13. channel: '/docpad-livereload',
  14. enabled: false,
  15. inject: true,
  16. socketOptions: null,
  17. getSocket: null,
  18. defaultLogLevel: 1,
  19. browserLog: true,
  20. regenerateBlock: null,
  21. environments: {
  22. development: {
  23. enabled: true
  24. }
  25. }
  26. };
  27. LivereloadPlugin.prototype.populateCollections = function(opts) {
  28. var config, docpad, injectBlock, listenBlock, regenerateBlock, scriptBlock, scriptsBlock;
  29. docpad = this.docpad;
  30. config = this.config;
  31. scriptsBlock = docpad.getBlock('scripts');
  32. regenerateBlock = config.regenerateBlock || ("if ( log ) {\n localStorage.setItem('" + config.channel + "/reloaded', 'yes');\n}\ndocument.location.reload();");
  33. listenBlock = "// Did we just livereload?\nvar log = " + (JSON.stringify(config.browserLog)) + " && localStorage && console && console.log && true;\nif ( log && localStorage.getItem('" + config.channel + "/reloaded') === 'yes' ) {\n localStorage.removeItem('" + config.channel + "/reloaded');\n console.log('LiveReloaded at', new Date())\n}\n\n// Listen for the regenerated event\n// and perform a reload of the page when the event occurs\nvar listen = function(){\n var socket = io.connect('" + config.channel + "');\n socket.on('regenerated',function(){\n " + regenerateBlock + "\n });\n};";
  34. injectBlock = "// Inject socket.io into our page then listen once loaded\nvar inject = function(){\n var t = document.createElement('script');\n t.type = 'text/javascript';\n t.async = true;\n t.src = '/socket.io/socket.io.js';\n t.onload = listen;\n var s = document.getElementsByTagName('script')[0];\n s.parentNode.insertBefore(t,s);\n};";
  35. scriptBlock = config.inject ? "(function(){\n " + listenBlock + "\n if ( typeof io !== 'undefined' ) {\n listen();\n } else {\n " + injectBlock + "\n inject();\n }\n})();" : "(function(){\n " + listenBlock + "\n if ( typeof io !== 'undefined' ) {\n listen();\n }\n})();";
  36. scriptsBlock.add(scriptBlock, {
  37. defer: false
  38. });
  39. return this;
  40. };
  41. LivereloadPlugin.prototype.serverAfter = function(opts) {
  42. var config, docpad, existingSocket, logLevel, server, serverHttp, socket, socketOptions, _base, _ref;
  43. server = opts.server, serverHttp = opts.serverHttp;
  44. docpad = this.docpad;
  45. config = this.config;
  46. logLevel = docpad.getLogLevel() === 7 ? 3 : config.defaultLogLevel;
  47. socketOptions = config.socketOptions || {};
  48. if ((_ref = socketOptions['log level']) == null) {
  49. socketOptions['log level'] = logLevel;
  50. }
  51. existingSocket = true;
  52. socket = typeof (_base = this.config).getSocket === "function" ? _base.getSocket() : void 0;
  53. if (!socket) {
  54. existingSocket = false;
  55. socket = require('socket.io').listen(serverHttp || server, socketOptions);
  56. }
  57. this.socket = socket.of(config.channel);
  58. docpad.log('info', "LiveReload listening to " + (existingSocket ? 'existing' : 'new') + " socket on channel " + config.channel + " with log level " + logLevel);
  59. return this;
  60. };
  61. LivereloadPlugin.prototype.generateAfter = function(opts) {
  62. var _ref;
  63. if ((_ref = this.socket) != null) {
  64. _ref.emit('regenerated');
  65. }
  66. return this;
  67. };
  68. return LivereloadPlugin;
  69. })(BasePlugin);
  70. };