Last active
January 29, 2019 20:12
-
-
Save romaricpascal/5577385 to your computer and use it in GitHub Desktop.
Sample configuration for grunt-contrib-connect, grunt-contrib-livereload and grunt-open
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", | |
// 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