diff --git a/lib/index.js b/lib/index.js index 81c0966..ae5711f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,3 +1,5 @@ +'use strict'; + var sendevent = require('sendevent') , inject = require('./inject') , serve = require('./serve') @@ -18,12 +20,15 @@ module.exports = function(root, opts) { var fn = stack() fn.reload = function() {} + var watcher = filewatcher() + var re; // bypass in production var bypass = opts.bypass if (bypass === undefined) bypass = process.env.NODE_ENV == 'production' if (!bypass) { + // the prefix under which the eventstream and the client is exposed var prefix = opts.prefix || '/instant' @@ -42,20 +47,20 @@ module.exports = function(root, opts) { if (typeof ev == 'string') ev = { url: ev } if (ev) events.broadcast(ev) } + var urlsByFile = {} - if (root && opts.watch !== false) { - var urlsByFile = {} - , watcher = filewatcher() - - // when a file is modifed tell all clients to reload it - watcher.on('change', function(file) { - fn.reload(urlsByFile[file]) - }) + // when a file is modifed tell all clients to reload it + watcher.on('change', function(file) { + fn.reload(urlsByFile[file]) + }) - // build a RegExp to match all watched file extensions - var exts = opts.watch || ['html', 'js', 'css'] - , re = new RegExp('\\.(' + exts.join('|') + ')$') + // build a RegExp to match all watched file extensions + var exts = opts.watch || ['html', 'js', 'css'] + re = new RegExp('\\.(' + exts.join('|') + ')$') + } + fn.add = function( new_root ) { + if ( !bypass && new_root && opts.watch !== false) { // pass an `onfile` handler that watches matching files opts = Object.create(opts, { onfile: { value: function(path, stat) { @@ -66,11 +71,13 @@ module.exports = function(root, opts) { }} }) } - } - if (root) { - fn.use(serve(root, opts)) + if (new_root) { + fn.use(serve(new_root, opts)) + } + return fn; } - return fn + return fn.add(root); + } diff --git a/package.json b/package.json index ed5bb94..4fced1f 100644 --- a/package.json +++ b/package.json @@ -28,9 +28,10 @@ "sendevent": "^1.0.2" }, "devDependencies": { - "should": "^4.3.0", - "supertest": "^0.15.0", + "browserify": "^6.3.2", + "express": "^4.10.7", "mocha": "^2.0.1", - "browserify": "^6.3.2" + "should": "^4.3.0", + "supertest": "^0.15.0" } } diff --git a/test/fixture/index.html b/test/fixture/index.html index f92c3e4..169be7c 100644 --- a/test/fixture/index.html +++ b/test/fixture/index.html @@ -3,9 +3,12 @@ + +

Hello

+to index2 \ No newline at end of file diff --git a/test/fixture2/index2.html b/test/fixture2/index2.html new file mode 100644 index 0000000..fbd6c4c --- /dev/null +++ b/test/fixture2/index2.html @@ -0,0 +1,14 @@ + + + + + + + + + + +

Hello2

+to index + + \ No newline at end of file diff --git a/test/fixture2/main2.css b/test/fixture2/main2.css new file mode 100644 index 0000000..937a3c0 --- /dev/null +++ b/test/fixture2/main2.css @@ -0,0 +1,3 @@ +html { + background: silver +} \ No newline at end of file diff --git a/test/fixture2/main2.js b/test/fixture2/main2.js new file mode 100644 index 0000000..fea888a --- /dev/null +++ b/test/fixture2/main2.js @@ -0,0 +1 @@ +console.log('hello2') \ No newline at end of file diff --git a/test/index.js b/test/index.js index ffde149..ab421d8 100644 --- a/test/index.js +++ b/test/index.js @@ -1,3 +1,4 @@ +'use strict'; /* global describe, it */ var instant = require('..') @@ -8,6 +9,7 @@ var instant = require('..') var ins = instant(__dirname + '/fixture') , app = http.createServer(ins) + ins.add(__dirname + '/fixture2') describe('instant', function() { it('should inject the client script', function(done) { @@ -57,6 +59,13 @@ describe('instant', function() { }) }) +describe('instant add', function() { + it('should inject the client script', function(done) { + request(app) + .get('/index2.html') + .expect(/