Skip to content

Instantly share code, notes, and snippets.

@ninjascribble
Last active August 29, 2015 14:08
Show Gist options
  • Save ninjascribble/ba6ff46205d47d63c9ff to your computer and use it in GitHub Desktop.
Save ninjascribble/ba6ff46205d47d63c9ff to your computer and use it in GitHub Desktop.
grunt-express-server with grunt-contrib-watch
module.exports = function(grunt) {
var PROJ_ROOT = __dirname;
var BUILD_ROOT = PROJ_ROOT + '/build';
var pkg = grunt.file.readJSON('package.json');
var pkgDependencies = Object.keys(pkg.dependencies).map(function(val) {
return 'node_modules/' + val + '/**';
});
grunt.initConfig({
// Define the app's package.json file.
pkg: pkg,
// Delete the /build directory.
clean: [BUILD_ROOT],
// Copy everything into /build. The tasks are broken out
// so we can re-run later through our watch tasks.
copy: {
appDependencies: {
expand: true,
cwd: PROJ_ROOT,
src: pkgDependencies,
dest: BUILD_ROOT
},
app: {
expand: true,
cwd: PROJ_ROOT,
src: ['index.js', 'src/**', 'config/**'],
dest: BUILD_ROOT
}
},
// Watch for changes to src files and run the relevant
// build commands. Only livereload when /build changes.
watch: {
app: {
files: [
PROJ_ROOT + '/index.js',
PROJ_ROOT + '/src/**/*.js',
PROJ_ROOT + '/config/**/*'
],
tasks: ['copy:app', 'express:dev'],
options: {
spawn: false
}
},
},
// Run the server from /build.
express: {
dev: {
options: {
script: BUILD_ROOT + '/index.js',
port: 8080,
debug: true
}
}
}
});
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-express-server');
grunt.registerTask('default', [
'clean',
'build',
'run'
]);
grunt.registerTask('build', [
'copy'
]);
grunt.registerTask('run', [
'express:dev', 'watch'
]);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment