Created
November 4, 2014 16:04
-
-
Save rohn/43675e3508e8940082ee to your computer and use it in GitHub Desktop.
Grunt livereload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function(grunt) { | |
// Load Grunt tasks declared in the package.json file | |
require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks); | |
// Configure Grunt | |
grunt.initConfig({ | |
// grunt-contrib-connect will serve the files of the project | |
// on specified port and hostname | |
connect: { | |
all: { | |
options:{ | |
port: 9000, | |
hostname: "0.0.0.0", | |
// No need for keepalive anymore as watch will keep Grunt running | |
//keepalive: true, | |
// Livereload needs connect to insert a cJavascript snippet | |
// in the pages it serves. This requires using a custom connect middleware | |
middleware: function(connect, options) { | |
return [ | |
// Load the middleware provided by the livereload plugin | |
// that will take care of inserting the snippet | |
require('grunt-contrib-livereload/lib/utils').livereloadSnippet, | |
// Serve the project folder | |
connect.static(options.base) | |
]; | |
} | |
} | |
} | |
}, | |
// grunt-open will open your browser at the project's URL | |
open: { | |
all: { | |
// Gets the port from the connect configuration | |
path: 'http://localhost:<%= connect.all.options.port%>' | |
} | |
}, | |
// grunt-regarde monitors the files and triggers livereload | |
// Surprisingly, livereload complains when you try to use grunt-contrib-watch instead of grunt-regarde | |
regarde: { | |
all: { | |
// This'll just watch the index.html file, you could add **/*.js or **/*.css | |
// to watch Javascript and CSS files too. | |
files:[ | |
'index.html', | |
'**/*.css', | |
'**/*.js' | |
], | |
// This configures the task that will run when the file change | |
tasks: ['livereload'] | |
} | |
} | |
}); | |
// Creates the `server` task | |
grunt.registerTask('server',[ | |
// Starts the livereload server to which the browser will connect to | |
// get notified of when it needs to reload | |
'livereload-start', | |
'connect', | |
// Connect is no longer blocking other tasks, so it makes more sense to open the browser after the server starts | |
'open', | |
// Starts monitoring the folders and keep Grunt alive | |
'regarde' | |
]); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name":"Homepage-00a", | |
"version":"1.0.0", | |
"dependencies":{}, | |
"devDependencies":{ | |
"matchdep": "~0.1.1", | |
"grunt": "~0.4.0", | |
"grunt-regarde": "~0.1.1", | |
"grunt-contrib-connect": "0.1.2", | |
"grunt-contrib-livereload": "0.1.1", | |
"grunt-open": "~0.2.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment