Skip to content

Instantly share code, notes, and snippets.

@ChetHarrison
Created July 25, 2014 19:46
Show Gist options
  • Save ChetHarrison/24e6f9b68a526cfc6fc4 to your computer and use it in GitHub Desktop.
Save ChetHarrison/24e6f9b68a526cfc6fc4 to your computer and use it in GitHub Desktop.
grunt file
/* global requirejs */
'use strict';
var LIVERELOAD_PORT = 35729;
var SERVER_PORT = 9000;
var lrSnippet = require('connect-livereload')({port: LIVERELOAD_PORT});
var mountFolder = function (connect, dir) {
return connect.static(require('path').resolve(dir));
};
// # Globbing
// templateFramework: 'lodash'
module.exports = function (grunt) {
// show elapsed time at the end
require('time-grunt')(grunt);
// load all grunt tasks
// require('load-grunt-tasks')(grunt);
require('load-grunt-tasks')(grunt, {patterns: ['grunt-*', '!grunt-template-jasmine-istanbul', '!grunt-template-jasmine-requirejs']});
// configurable paths
var yeomanConfig = {
app: 'app',
scripts: 'js',
styles: 'css',
images: 'img',
dist: 'dist'
};
grunt.initConfig({
yeoman: yeomanConfig,
dependencygraph: {
targetPath: './app/js/common/base-objects/author.js',
outputPath: './deps',
format: 'amd'
},
watch: {
options: {
nospawn: true,
livereload: true
},
compass: {
files: ['<%= yeoman.app %>/<%= yeoman.styles %>/**/*.{scss,sass}'],
tasks: ['compass']
},
livereload: {
options: {
livereload: LIVERELOAD_PORT
},
files: [
'<%= yeoman.app %>/*.html',
'{.tmp,<%= yeoman.app %>}/<%= yeoman.styles %>/**/*.css',
'{.tmp,<%= yeoman.app %>}/<%= yeoman.scripts %>/**/*.js',
'<%= yeoman.app %>/<%= yeoman.images %>/**/*.{png,jpg,jpeg,gif,webp}',
'<%= yeoman.app %>/<%= yeoman.scripts %>/templates/*.{ejs,mustache,hbs}',
'tests/spec/**/*.js'
]
},
jst: {
files: [
'<%= yeoman.app %>/<%= yeoman.scripts %>/templates/*.ejs'
],
tasks: ['jst']
},
test: {
files: ['<%= yeoman.app %>/<%= yeoman.scripts %>/**/*.js', 'tests/spec/**/*.js'],
tasks: ['test:true']
}
},
connect: {
options: {
port: SERVER_PORT,
// change this to '0.0.0.0' to access the server from outside
hostname: 'localhost'
},
livereload: {
options: {
middleware: function (connect) {
return [
lrSnippet,
mountFolder(connect, '.tmp'),
mountFolder(connect, yeomanConfig.app)
];
}
}
},
test: {
options: {
port: 9001,
middleware: function (connect) {
return [
lrSnippet,
mountFolder(connect, '.tmp'),
mountFolder(connect, 'tests'),
mountFolder(connect, yeomanConfig.app)
];
}
}
},
dist: {
options: {
middleware: function (connect) {
return [
mountFolder(connect, yeomanConfig.dist)
];
}
}
}
},
open: {
server: {
path: 'http://localhost:<%= connect.options.port %>'
},
test: {
path: 'http://localhost:<%= connect.test.options.port %>'
}
},
clean: {
dist: ['.tmp', '<%= yeoman.dist %>/*'],
server: '.tmp'
},
jshint: {
options: {
jshintrc: '.jshintrc',
reporter: require('jshint-stylish')
},
all: [
'Gruntfile.js',
'<%= yeoman.app %>/<%= yeoman.scripts %>/**/*.js',
'!<%= yeoman.app %>/<%= yeoman.scripts %>/vendor/*',
'tests/spec/**/*.js'
]
},
jasmine: {
test: {
src: ['<%= yeoman.app %>/<%= yeoman.scripts %>/**/*.js',
'!<%= yeoman.app %>/<%= yeoman.scripts %>/tpl.js',
'!<%= yeoman.app %>/<%= yeoman.scripts %>main.js'],
options: {
specs: 'tests/spec/*.spec.js',
helpers: 'tests/spec/helper.js',
template: require('grunt-template-jasmine-requirejs'),
templateOptions: {
requireConfigFile: ['tests/main.js']
}
}
},
coverage: {
src: ['<%= yeoman.app %>/<%= yeoman.scripts %>/**/*.js',
'!<%= yeoman.app %>/<%= yeoman.scripts %>/tpl.js',
'!<%= yeoman.app %>/<%= yeoman.scripts %>main.js'],
options: {
specs: ['tests/spec/*.spec.js'],
template: require('grunt-template-jasmine-istanbul'),
templateOptions: {
coverage: 'coverage/coverage.json',
report: 'coverage',
thresholds: {
lines: 75,
statements: 75,
branches: 75,
functions: 90
},
// 1. don't replace src for the mixed-in template with instrumented sources
replace: false,
template: require('grunt-template-jasmine-requirejs'),
templateOptions: {
requireConfig: {
// 2. use the baseUrl you want
baseUrl: './<%= yeoman.app %>/<%= yeoman.scripts %>/',
// 3. pass paths of the sources being instrumented as a configuration option
// these paths should be the same as the jasmine task's src
// unfortunately, grunt.config.get() doesn't work because the config is just being evaluated
config: {
instrumented: {
src: grunt.file.expand(['<%= yeoman.app %>/<%= yeoman.scripts %>/**/*.js',
'!<%= yeoman.app %>/<%= yeoman.scripts %>/tpl.js',
'!<%= yeoman.app %>/<%= yeoman.scripts %>main.js'])
}
},
// 4. use this callback to read the paths of the sources being instrumented and redirect requests to them appropriately
callback: function () {
define('instrumented', ['module'], function (module) {
return module.config().src;
});
require(['instrumented'], function (instrumented) {
var oldLoad = requirejs.load;
requirejs.load = function (context, moduleName, url) {
// normalize paths
if (url.substring(0, 1) === '/') {
url = url.substring(1);
} else if (url.substring(0, 2) === './') {
url = url.substring(2);
}
// redirect
if (instrumented.indexOf(url) > -1) {
url = './.grunt/grunt-contrib-jasmine/' + url;
}
return oldLoad.apply(this, [context, moduleName, url]);
};
});
}
}
}
}
}
}
},
compass: {
options: {
sassDir: '<%= yeoman.app %>/<%= yeoman.styles %>',
cssDir: '.tmp/<%= yeoman.styles %>',
imagesDir: '<%= yeoman.app %>/<%= yeoman.images %>',
javascriptsDir: '<%= yeoman.app %>/<%= yeoman.scripts %>',
fontsDir: '<%= yeoman.app %>/<%= yeoman.styles %>/fonts',
importPath: '<%= yeoman.app %>/bower_components',
relativeAssets: true
},
dist: {},
server: {
options: {
debugInfo: true
}
}
},
requirejs: {
dist: {
// Options: https://github.com/jrburke/r.js/blob/master/build/example.build.js
options: {
baseUrl: '<%= yeoman.app %>/<%= yeoman.scripts %>',
optimize: 'none',
paths: {
'templates': '../../.tmp//templates',
'jquery': '../../app/bower_components/jquery/jquery',
'jquery-ui': '../../app/bower_components/jquery-ui/ui/jquery-ui',
'underscore': '../../app/bower_components/underscore/underscore',
'backbone': '../../app/bower_components/backbone/backbone',
'marionette': '../../app/bower_components/backbone.marionette/lib/core/amd/backbone.marionette',
'masonry': '../../app/bower_components/masonry/masonry',
'd3': '../../app/bower_components/d3/d3'
},
// TODO: Figure out how to make sourcemaps work with grunt-usemin
// https://github.com/yeoman/grunt-usemin/issues/30
//generateSourceMaps: true,
// required to support SourceMaps
// http://requirejs.org/docs/errors.html#sourcemapcomments
preserveLicenseComments: false,
useStrict: true,
wrap: true
//uglify2: {} // https://github.com/mishoo/UglifyJS2
}
}
},
useminPrepare: {
html: '<%= yeoman.app %>/index.html',
options: {
dest: '<%= yeoman.dist %>'
}
},
usemin: {
html: ['<%= yeoman.dist %>/**/*.html'],
css: ['<%= yeoman.dist %>/<%= yeoman.styles %>/**/*.css'],
options: {
dirs: ['<%= yeoman.dist %>']
}
},
imagemin: {
dist: {
files: [{
expand: true,
cwd: '<%= yeoman.app %>/<%= yeoman.images %>',
src: '**/*.{png,jpg,jpeg}',
dest: '<%= yeoman.dist %>/<%= yeoman.images %>'
}]
}
},
cssmin: {
dist: {
files: {
'<%= yeoman.dist %>/<%= yeoman.styles %>/main.css': [
'.tmp/<%= yeoman.styles %>/**/*.css',
'<%= yeoman.app %>/<%= yeoman.styles %>/**/*.css'
]
}
}
},
htmlmin: {
dist: {
options: {
/*removeCommentsFromCDATA: true,
// https://github.com/yeoman/grunt-usemin/issues/44
//collapseWhitespace: true,
collapseBooleanAttributes: true,
removeAttributeQuotes: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeOptionalTags: true*/
},
files: [{
expand: true,
cwd: '<%= yeoman.app %>',
src: '*.html',
dest: '<%= yeoman.dist %>'
}]
}
},
copy: {
dist: {
files: [{
expand: true,
dot: true,
cwd: '<%= yeoman.app %>',
dest: '<%= yeoman.dist %>',
src: [
'*.{ico,txt}',
'.htaccess',
'<%= yeoman.images %>/**/*.{webp,gif}',
'<%= yeoman.styles %>/fonts/**/*.*',
'bower_components/sass-bootstrap/fonts/*.*'
]
}]
}
},
bower: {
all: {
rjsConfig: '<%= yeoman.app %>/<%= yeoman.scripts %>/main.js'
}
},
jst: {
options: {
amd: true
},
compile: {
files: {
'.tmp/<%= yeoman.scripts %>/templates.js': ['<%= yeoman.app %>/<%= yeoman.scripts %>/templates/*.ejs']
}
}
},
rev: {
dist: {
files: {
src: [
'<%= yeoman.dist %>/<%= yeoman.scripts %>/**/*.js',
'<%= yeoman.dist %>/<%= yeoman.styles %>/**/*.css',
'<%= yeoman.dist %>/<%= yeoman.images %>/**/*.{png,jpg,jpeg,gif,webp}',
'<%= yeoman.dist %>/<%= yeoman.styles %>/fonts/**/*.*', // changed % to %%
'bower_components/sass-bootstrap/fonts/*.*'
]
}
}
},
docco: {
doc: {
src: ['app/js/**/*.js', '!app/js/main.js', '!app/js/vendor/tpl.js'],
options: {
output: 'docs/'
}
}
},
plato: {
report: {
options : {
jshint : grunt.file.readJSON('.jshintrc')
},
files: {
'reports': ['app/js/**/*.js', '!app/js/main.js', '!app/js/vendor/tpl.js']
}
}
}
});
grunt.loadNpmTasks('grunt-dependencygraph');
grunt.registerTask('createDefaultTemplate', function () {
grunt.file.write('.tmp//templates.js', 'this.JST = this.JST || {};');
});
grunt.registerTask('server', function (target) {
grunt.log.warn('The `server` task has been deprecated. Use `grunt serve` to start a server.');
if (!target) {
target = 'dist';
}
grunt.task.run(['serve:' + target]);
});
grunt.registerTask('serve', function (target) {
if (target === 'dist') {
return grunt.task.run(['build', 'open:server', 'connect:dist:keepalive']);
}
if (target === 'test') {
return grunt.task.run([
'clean:server',
'createDefaultTemplate',
'jst',
'compass:server',
'connect:test',
'open:test',
'watch:livereload'
]);
}
grunt.task.run([
'clean:server',
'createDefaultTemplate',
'jst',
'compass:server',
'connect:livereload',
'open:server',
'watch'
]);
});
grunt.registerTask('test', function (isConnected) {
isConnected = Boolean(isConnected);
var testTasks = [
'clean:server',
'createDefaultTemplate',
'jst',
'compass',
'jasmine',
'watch:test'
];
if(!isConnected) {
return grunt.task.run(testTasks);
} else {
// already connected so not going to connect again, remove the connect:test task
testTasks.splice(testTasks.indexOf('connect:test'), 1);
return grunt.task.run(testTasks);
}
});
grunt.registerTask('build', [
'clean:dist',
'createDefaultTemplate',
'jst',
'compass:dist',
'useminPrepare',
'requirejs',
'imagemin',
'htmlmin',
'concat',
'cssmin',
'uglify',
'copy',
'rev',
'usemin',
'docco',
'plato'
]);
grunt.registerTask('doc', [
'docco',
'plato'
]);
grunt.registerTask('default', [
'jshint',
'test',
'build'
]);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment