Skip to content

Instantly share code, notes, and snippets.

@romaricpascal
Last active January 29, 2019 20:12
Show Gist options
  • Save romaricpascal/5577385 to your computer and use it in GitHub Desktop.
Save romaricpascal/5577385 to your computer and use it in GitHub Desktop.
Sample configuration for grunt-contrib-connect, grunt-contrib-livereload and grunt-open
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",
// Prevents Grunt to close just after the task (starting the server) completes
// This will be removed later as `watch` will take care of that
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%>'
}
}
});
// Creates the `server` task
grunt.registerTask('server',[
// Open before connect because connect uses keepalive at the moment
// so anything after connect wouldn't run
'open',
// Starts the livereload server to which the browser will connect to
// get notified of when it needs to reload
'livereload-start',
'connect'
]);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment