Skip to content

Commit

Permalink
Support injecting livereload plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
wearhere committed Aug 12, 2016
1 parent b5f49da commit dbddac0
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
8 changes: 8 additions & 0 deletions README.md
Expand Up @@ -95,6 +95,14 @@ These are the available options with the following defaults:

// Set this option to `true` to set `req.headers['accept-encoding']` to 'identity' (disabling compression)
disableCompression: false,

// Locations where livereload plugins are provided (not by connect-livereload).
// These plugins should handle being loaded before _or_ after the livereload
// script itself (the order is not guaranteed), like
// https://github.com/mixmaxhq/livereload-require-js-includes/blob/5a431793d6fdfcf93d66814ddc58338515a3254f/index.js#L40-L45
plugins: [
"http://localhost:3001/livereload-require-js-includes/index.js"
]
```

please see the [examples](https://github.com/intesso/connect-livereload/tree/master/examples) for the app and Grunt configuration.
Expand Down
6 changes: 5 additions & 1 deletion index.js
Expand Up @@ -20,10 +20,14 @@ module.exports = function livereload(opt) {
}];
var disableCompression = opt.disableCompression || false;
var port = opt.port || 35729;
var plugins = opt.plugins || [];

function snippet(host) {
var src = opt.src || '//' + host + ':' + port + '/livereload.js?snipver=1';
return '<script src="' + src + '" async="" defer=""></script>';
return '<script src="' + src + '" async="" defer=""></script>' +
plugins.map(function(pluginSrc) {
return '<script src="' + pluginSrc + '" async="" defer=""></script>';
}).join('');
}

// helper functions
Expand Down
53 changes: 53 additions & 0 deletions test/app.options.plugins.js
@@ -0,0 +1,53 @@
var express = require("express");
var app = express();

// load liveReload script and plugin
app.use(require('../index.js')({
plugins: [
"http://localhost/plugin.js"
]
}));

// load static content before routing takes place
app.use(express["static"](__dirname + "/fixtures"));

app.get("/default-test", function (req, res) {
var html = '<html><head></head><body><p>default test </p></body></html>';
res.send(html);
});

app.get("/index.html", function (req, res) {
var html = '<html><head></head><body><p>default test </p></body></html>';
res.send(html);
});

// start the server
if (!module.parent) {
var port = settings.webserver.port || 3000;
app.listen(port);
console.log("Express app started on port " + port);
}

// run the tests
var request = require('supertest');
var assert = require('assert');


describe('GET /default-test', function () {
it('respond with inserted script and plugin', function (done) {
request(app)
.get('/default-test')
.set('Accept', 'text/html')
.expect(200)
.end(function (err, res) {
// Assert that it has the script.
assert(~res.text.indexOf('livereload.js?snipver=1'));

// Assert that it has the plugin.
assert(~res.text.indexOf('plugin.js'));

if (err) return done(err);
done()
});
})
});

0 comments on commit dbddac0

Please sign in to comment.