Skip to content

Commit

Permalink
Move live reload config to its own function
Browse files Browse the repository at this point in the history
  • Loading branch information
bclinkinbeard committed Jun 13, 2014
1 parent ba7aa68 commit 459c0ab
Showing 1 changed file with 48 additions and 44 deletions.
92 changes: 48 additions & 44 deletions lib/server.js
Expand Up @@ -41,50 +41,7 @@ module.exports = function (args) {

baseUrl = parsedUrl.protocol + '//' + parsedUrl.hostname

if (lr) {
// if live reloading, use watchify
args.js.watch = true
args.js.output = args.js.output || defaultBundlePath
// listen for initial bundle completion before opening
js(args.js).on('end', function () {
if (launch) open(baseUrl + ':' + port + serverPath)
})

if (!lr.quiet) {
js.emitter.on('changed', function (filepath) {
console.log(path.relative(process.cwd(), filepath), 'changed'.grey);
})

js.emitter.on('bundle', function (time) {
console.log(String('bundle updated in ' + time + 'ms').grey);
})
}

lr = typeof lr === 'boolean' ? {} : lr
lr.patterns = lr.patterns ? lr.patterns.concat(args.js.output) : ['*.html', '*.css', args.js.output]

lr.port = lr.port || 3000
lr.sync = parseSyncOptions(lr.sync || lr.s)
startFileWatch(lr)
console.log(('Live reload server listening on port ' + lr.port).grey)

browserSync = browserSync.init(lr.patterns, {
proxy: {
host: parsedUrl.hostname,
port: port
}
, ports: {
min: lr.port,
max: lr.port + 2
}
, injectChanges: false
, ghostMode: lr.sync
, notify: Boolean(lr.sync)
, debugInfo: Boolean(lr.verbose)
}, function (err, config) {
snippet = config.api.snippet;
})
}
configureLiveReload(lr)

http.createServer(function (req, res) {

Expand Down Expand Up @@ -130,6 +87,53 @@ function responder (type, res) {
}
}

function configureLiveReload (lr) {
if (!lr) return

// if live reloading, use watchify
args.js.watch = true
args.js.output = args.js.output || defaultBundlePath
// listen for initial bundle completion before opening
js(args.js).on('end', function () {
if (launch) open(baseUrl + ':' + port + serverPath)
})

if (!lr.quiet) {
js.emitter.on('changed', function (filepath) {
console.log(path.relative(process.cwd(), filepath), 'changed'.grey);
})

js.emitter.on('bundle', function (time) {
console.log(String('bundle updated in ' + time + 'ms').grey);
})
}

lr = typeof lr === 'boolean' ? {} : lr
lr.patterns = lr.patterns ? lr.patterns.concat(args.js.output) : ['*.html', '*.css', args.js.output]

lr.port = lr.port || 3000
lr.sync = parseSyncOptions(lr.sync || lr.s)
startFileWatch(lr)
console.log(('Live reload server listening on port ' + lr.port).grey)

browserSync = browserSync.init(lr.patterns, {
proxy: {
host: parsedUrl.hostname,
port: port
}
, ports: {
min: lr.port,
max: lr.port + 2
}
, injectChanges: false
, ghostMode: lr.sync
, notify: Boolean(lr.sync)
, debugInfo: Boolean(lr.verbose)
}, function (err, config) {
snippet = config.api.snippet;
})
}

function startFileWatch (lr) {
gaze(lr.patterns, function () {
this.on('changed', function (filepath) {
Expand Down

0 comments on commit 459c0ab

Please sign in to comment.