Skip to content
Browse files

Delay opening the browser until the tasks are complete

This prevents the browser from opening to early to an unstyled page.
  • Loading branch information...
1 parent da5a64c commit a46d20b7325f09824c4c60d8a11c0bda885faa7a @sindresorhus sindresorhus committed Sep 5, 2012
Showing with 14 additions and 10 deletions.
  1. +14 −10 cli/tasks/server.js
View
24 cli/tasks/server.js
@@ -192,6 +192,7 @@ module.exports = function(grunt) {
// Note: yeoman-server alone will exit prematurly unless `this.async()` is
// called. The task is designed to work alongside the `watch` task.
grunt.registerTask('server', 'Launch a preview, LiveReload compatible server', function(target) {
+ var opts;
// Get values from config, or use defaults.
var port = grunt.config('server.port') || 0xDAD;
@@ -222,21 +223,30 @@ module.exports = function(grunt) {
return false;
}
- grunt.helper('server', {
+ opts = {
// prevent browser opening on `reload` target
open: target !== 'reload',
// and force 35729 port no matter what when on `reload` target
port: target === 'reload' ? 35729 : port,
base: targets[target],
inject: true,
- target: target
- }, cb);
+ target: target,
+ hostname: grunt.config('server.hostname') || 'localhost'
+ };
+
+ grunt.helper('server', opts, cb);
+
+ grunt.registerTask('open-browser', function() {
+ if ( opts.open ) {
+ open( 'http://' + opts.hostname + ':' + opts.port );
+ }
+ });
if(target === 'app') {
// when serving app, make sure to delete the temp/ dir from w/e was
// previously compiled here, and trigger compass / coffee mostly to make
// sure, those files are compiled and not revved.
- grunt.task.run('clean coffee compass');
+ grunt.task.run('clean coffee compass open-browser');
}
grunt.task.run('watch');
@@ -245,8 +255,6 @@ module.exports = function(grunt) {
grunt.registerHelper('server', function(opts, cb) {
cb = cb || function() {};
- opts.hostname = opts.hostname || 'localhost';
-
var middleware = [];
// add the special livereload snippet injection middleware
@@ -310,10 +318,6 @@ module.exports = function(grunt) {
port: port
});
- if ( opts.open ) {
- open( 'http://' + opts.hostname + ':' + port );
- }
-
cb(null, port);
});
});

0 comments on commit a46d20b

Please sign in to comment.
Something went wrong with that request. Please try again.