Skip to content

Instantly share code, notes, and snippets.

@edoloughlin
Created July 15, 2014 14:48
Show Gist options
  • Save edoloughlin/4ae2afe39668cdb4c0e6 to your computer and use it in GitHub Desktop.
Save edoloughlin/4ae2afe39668cdb4c0e6 to your computer and use it in GitHub Desktop.
'use strict';
var LIVERELOAD_PORT = 35729;
var lrSnippet = require('connect-livereload')({
port: LIVERELOAD_PORT
});
var mountFolder = function (connect, dir) {
return connect.static(require('path').resolve(dir));
};
module.exports = function (grunt) {
// Let *load-grunt-tasks* require everything
require('load-grunt-tasks')(grunt);
// Read configuration from package.json
var pkgConfig = grunt.file.readJSON('package.json');
var jshintConfig = grunt.file.readJSON('.jshintrc');
var loaders = [{
test: /\.css$/,
loader: 'style!css'
}, {
test: /\.gif/,
loader: 'url-loader?limit=10000&minetype=image/gif'
}, {
test: /\.jpg/,
loader: 'url-loader?limit=10000&minetype=image/jpg'
}, {
test: /\.png/,
loader: 'url-loader?limit=10000&minetype=image/png'
}, {
test: /\.js$/,
loader: 'jsx-loader'
}];
grunt.initConfig({
pkg: pkgConfig,
webpack: {
development: {
entry: './<%= pkg.src %>/scripts/components/<%= pkg.mainInput %>.js',
output: {
path: '<%= pkg.dev.localdir %>/scripts/',
filename: '<%= pkg.mainOutput %>.js'
},
debug: true,
cache: true,
stats: {
colors: true,
reasons: true
},
jshint: grunt.util._.merge(jshintConfig, {
emitErrors: false,
failOnHint: false
}),
module: {
preLoaders: [{
test: '\\.js$',
exclude: 'node_modules',
loader: 'jshint'
}],
loaders: loaders
}
}
},
copy: {
// Copy non-source files
development: {
files: [
{expand:true,
cwd: '<%= pkg.src %>/',
src: ['./images/**',
'./index.html',
'./styles/**'],
dest: './<%= pkg.dev.localdir %>/'
}
],
},
dist: {
files: [
{expand:true,
cwd: '<%= pkg.src %>/',
src: ['./images/**',
'./index.html',
'./styles/**'],
dest: './<%= pkg.dist.localdir %>/'
}
],
}
},
rsync: {
options: {
args: ["--verbose", "--delete"],
recursive: true
},
// Configure acess to pkg.dev.host in your local ~/.ssh/config. See the
// related comment in package.json
development: {
options: {
host: '<%= pkg.dev.host %>',
src: '<%= pkg.dev.localdir %>/*',
dest: '<%= pkg.dev.remotedir %>',
},
},
// TODO: release deployment. Does it belong here?
dist: {
}
},
watch: {
webpack: {
files: ['<%= pkg.src %>/scripts/{,*/}*.js',
'<%= pkg.src %>/styles/{,*/}*.css',
'!<%= pkg.src %>/scripts/<%= pkg.mainOutput %>.js'
],
tasks: ['webpack:development']
},
livereload: {
options: {
livereload: LIVERELOAD_PORT
},
files: [
'<%= pkg.src %>/{,*/}*.html',
'<%= pkg.src %>/scripts/<%= pkg.mainOutput %>.js'
]
}
},
connect: {
options: {
port: 8000,
// Change this to '0.0.0.0' to access the server from outside.
hostname: '0.0.0.0'
},
livereload: {
options: {
middleware: function (connect) {
return [
lrSnippet,
mountFolder(connect, pkgConfig.src)
];
}
}
},
dist: {
options: {
middleware: function (connect) {
return [
mountFolder(connect, pkgConfig.dist)
];
}
}
}
},
open: {
server: {
url: 'http://localhost:<%= connect.options.port %>'
}
},
karma: {
unit: {
configFile: 'karma.conf.js'
}
}
});
grunt.registerTask('serve', function (target) {
if (target === 'dist') {
return grunt.task.run(['build',
'open',
'connect:dist:keepalive']);
}
grunt.task.run([
'connect:livereload',
'webpack:development',
'open',
'watch'
]);
});
grunt.registerTask('test', ['karma']);
grunt.registerTask('build', 'Run webpack', function(target) {
if(target === 'dist') {
return;
}
grunt.task.run([
'webpack:development',
'copy:development'
]);
});
grunt.registerTask('deploy', 'Copy files to your dev server',
function(target) {
if(target === 'dist') {
// TODO: Intentionally ignoring deploy:dist for now. It probably shouldn't be
// TODO: done from this script.
return;
}
grunt.task.run([
// We assume 'grunt watch' is being run so we can skip 'webpack:development'
'copy:development',
'rsync:development'
]);
});
grunt.registerTask('default', []);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment