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.

81 lines
4.2 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, injectCall, 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 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. injectCall = "var readyStateCheckInterval = setInterval(function() {\n if (document.readyState === \"complete\") {\n inject();\n clearInterval(readyStateCheckInterval);\n }\n}, 10);";
  36. scriptBlock = config.inject ? "(function(){\n " + listenBlock + "\n if ( typeof io !== 'undefined' ) {\n listen();\n } else {\n " + injectBlock + "\n " + injectCall + "\n }\n})();" : "(function(){\n " + listenBlock + "\n if ( typeof io !== 'undefined' ) {\n listen();\n }\n})();";
  37. scriptsBlock.add(scriptBlock, {
  38. defer: false
  39. });
  40. return this;
  41. };
  42. LivereloadPlugin.prototype.serverAfter = function(opts) {
  43. var config, docpad, existingSocket, logLevel, server, serverHttp, socket, socketOptions, _base, _ref;
  44. server = opts.server, serverHttp = opts.serverHttp;
  45. docpad = this.docpad;
  46. config = this.config;
  47. logLevel = docpad.getLogLevel() === 7 ? 3 : config.defaultLogLevel;
  48. socketOptions = config.socketOptions || {};
  49. if ((_ref = socketOptions['log level']) == null) {
  50. socketOptions['log level'] = logLevel;
  51. }
  52. existingSocket = true;
  53. socket = typeof (_base = this.config).getSocket === "function" ? _base.getSocket() : void 0;
  54. if (!socket) {
  55. existingSocket = false;
  56. socket = require('socket.io').listen(serverHttp || server, socketOptions);
  57. }
  58. this.socket = socket.of(config.channel);
  59. docpad.log('info', "LiveReload listening to " + (existingSocket ? 'existing' : 'new') + " socket on channel " + config.channel + " with log level " + logLevel);
  60. return this;
  61. };
  62. LivereloadPlugin.prototype.generateAfter = function(opts) {
  63. var _ref;
  64. if ((_ref = this.socket) != null) {
  65. _ref.emit('regenerated');
  66. }
  67. return this;
  68. };
  69. return LivereloadPlugin;
  70. })(BasePlugin);
  71. };