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 @@