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
80 lines
4.0 KiB
// Generated by CoffeeScript 1.4.0
|
|
var __hasProp = {}.hasOwnProperty,
|
|
__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; };
|
|
|
|
module.exports = function(BasePlugin) {
|
|
var LivereloadPlugin;
|
|
return LivereloadPlugin = (function(_super) {
|
|
|
|
__extends(LivereloadPlugin, _super);
|
|
|
|
function LivereloadPlugin() {
|
|
return LivereloadPlugin.__super__.constructor.apply(this, arguments);
|
|
}
|
|
|
|
LivereloadPlugin.prototype.name = 'livereload';
|
|
|
|
LivereloadPlugin.prototype.config = {
|
|
channel: '/docpad-livereload',
|
|
enabled: false,
|
|
inject: true,
|
|
socketOptions: null,
|
|
getSocket: null,
|
|
defaultLogLevel: 1,
|
|
browserLog: true,
|
|
regenerateBlock: null,
|
|
environments: {
|
|
development: {
|
|
enabled: true
|
|
}
|
|
}
|
|
};
|
|
|
|
LivereloadPlugin.prototype.populateCollections = function(opts) {
|
|
var config, docpad, injectBlock, listenBlock, regenerateBlock, scriptBlock, scriptsBlock;
|
|
docpad = this.docpad;
|
|
config = this.config;
|
|
scriptsBlock = docpad.getBlock('scripts');
|
|
regenerateBlock = config.regenerateBlock || ("if ( log ) {\n localStorage.setItem('" + config.channel + "/reloaded', 'yes');\n}\ndocument.location.reload();");
|
|
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};";
|
|
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};";
|
|
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})();";
|
|
scriptsBlock.add(scriptBlock, {
|
|
defer: false
|
|
});
|
|
return this;
|
|
};
|
|
|
|
LivereloadPlugin.prototype.serverAfter = function(opts) {
|
|
var config, docpad, existingSocket, logLevel, server, serverHttp, socket, socketOptions, _base, _ref;
|
|
server = opts.server, serverHttp = opts.serverHttp;
|
|
docpad = this.docpad;
|
|
config = this.config;
|
|
logLevel = docpad.getLogLevel() === 7 ? 3 : config.defaultLogLevel;
|
|
socketOptions = config.socketOptions || {};
|
|
if ((_ref = socketOptions['log level']) == null) {
|
|
socketOptions['log level'] = logLevel;
|
|
}
|
|
existingSocket = true;
|
|
socket = typeof (_base = this.config).getSocket === "function" ? _base.getSocket() : void 0;
|
|
if (!socket) {
|
|
existingSocket = false;
|
|
socket = require('socket.io').listen(serverHttp || server, socketOptions);
|
|
}
|
|
this.socket = socket.of(config.channel);
|
|
docpad.log('info', "LiveReload listening to " + (existingSocket ? 'existing' : 'new') + " socket on channel " + config.channel + " with log level " + logLevel);
|
|
return this;
|
|
};
|
|
|
|
LivereloadPlugin.prototype.generateAfter = function(opts) {
|
|
var _ref;
|
|
if ((_ref = this.socket) != null) {
|
|
_ref.emit('regenerated');
|
|
}
|
|
return this;
|
|
};
|
|
|
|
return LivereloadPlugin;
|
|
|
|
})(BasePlugin);
|
|
};
|