Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 22 additions & 15 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

var sendevent = require('sendevent')
, inject = require('./inject')
, serve = require('./serve')
Expand All @@ -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'

Expand All @@ -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) {
Expand All @@ -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);

}
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
3 changes: 3 additions & 0 deletions test/fixture/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="main.css">
<link rel="stylesheet" type="text/css" href="main2.css">
<script src="main.js"></script>
<script src="main2.js"></script>
</head>
<body>
<h1>Hello</h1>
<a href="index2.html">to index2</a>
</body>
</html>
14 changes: 14 additions & 0 deletions test/fixture2/index2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="main.css">
<link rel="stylesheet" type="text/css" href="main2.css">
<script src="main.js"></script>
<script src="main2.js"></script>
</head>
<body>
<h1>Hello2</h1>
<a href="index.html">to index</a>
</body>
</html>
3 changes: 3 additions & 0 deletions test/fixture2/main2.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
html {
background: silver
}
1 change: 1 addition & 0 deletions test/fixture2/main2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('hello2')
9 changes: 9 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use strict';
/* global describe, it */

var instant = require('..')
Expand All @@ -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) {
Expand Down Expand Up @@ -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(/<script src="\/instant\/client\/bundle\.js"><\/script>/, done)
})
})

function waitFor(re) {
return function(res, done) {
Expand Down
20 changes: 20 additions & 0 deletions test/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';
var express = require('express');
var app = module.exports = express();

var instance = require('../lib/index.js');
var instantClient = instance();
app.use( instantClient.add( __dirname + '/fixture' ) );
app.use( instantClient.add( __dirname + '/fixture2' ) );

app.start = function(port) {
// start the web server
return app.listen(port,function() {
console.log('Web server listening at: %s', port);
});
};

// start the server if `$ node server.js`
if (require.main === module) {
app.start(3000);
}