Skip to content

Instantly share code, notes, and snippets.

@danemacaulay
Last active August 29, 2015 14:05
Show Gist options
  • Save danemacaulay/8bb2998d7aaf4d57022f to your computer and use it in GitHub Desktop.
Save danemacaulay/8bb2998d7aaf4d57022f to your computer and use it in GitHub Desktop.
/*global module: true, require: true, __dirname: true */
module.exports = function(grunt) {
'use strict';
var scripts,
config;
// Load grunt tasks automatically
require('load-grunt-tasks')(grunt);
var proxyCache = require('proxy-cache/lib/proxy-cache');
scripts = grunt.file.readJSON('scripts.json');
// project specific configuration
config = {
assetsPathPrefix: '/cdn/trunk-dev/DnB360',
livereload: true,
};
grunt.initConfig({
config: config,
// Empties folders to start fresh
clean: {
deploy: {
files: [{
dot: true,
src: [
'deploy/*',
'.tmp'
]
}]
}
},
// output linking errors to the webpage
'browser_output': {},
// run bower and npm install
'auto_install': {
local: {}
},
// run 'grunt test' before commit
githooks: {
all: {
'pre-commit': 'test',
}
},
// add script tags to our entry points.
// this task sits before concat, which will use the scripts tags created
// with this task to create the minified file
scriptlinker: {
app: {
options: {
fileTmpl: '\n <script src="../%s"></script>',
},
// @TODO combine these entry points into one or two
files: {
'html/index.html': scripts.app,
'html/indexNoCrm.html': scripts.app,
'html/crm-entity.html': scripts.app,
'html/crmCredentialEntry.html': scripts.app,
'html/ultralite-index.html': scripts.app,
'js/SpecRunner.html': scripts.app.concat(scripts.test),
},
},
},
// serve static assets
connect: {
options: {
port: 9090,
hostname: '127.0.0.1',
livereload: config.livereload,
},
dev: {
options: {
middleware: function (connect) {
return [
// prefix the assets path
// make html/index.html available at cdn/trunk-dev/DnB360/html/index.html
connect().use(
config.assetsPathPrefix,
connect.static(__dirname)
)
];
}
}
},
// this connect task serves the deploy codebase
// grunt serve:deploy
deploy: {
options: {
middleware: function (connect) {
return [
// prefix the assets path
// make deploy/html/index.html available at cdn/trunk-dev/DnB360/html/index.html
connect().use(
config.assetsPathPrefix,
connect.static(__dirname.concat('/deploy'))
),
];
}
}
}
},
uglify: {
options: {
sourceMap: true,
sourceMapIncludeSources: true,
// have some injector functions that dont follow correct injector notation
// remove this when they are fixed for smaller build files
mangle: false
},
},
less: {
deploy: {
options: {
paths: ['less'],
cleancss: true,
compress: true
},
files: {'css/main.css': 'less/main.less'}
}
},
jshint: {
options: {
jshintrc: 'js/jshintrc.json',
},
all: ['Gruntfile.js', 'js/src-angular/**/*.js']
},
execute: {
jsdom: {
src: ['js/src-node-test/run.js']
}
},
watch: {
options: {
livereloadOnError: false,
},
less : {
files: ['less/*'],
tasks: ['less']
},
jshint : {
files: ['Gruntfile.js', 'js/src-angular/**/*.js'],
tasks: ['jshint'],
options: {
livereload: config.livereload,
nospawn: true,
}
},
bower: {
files: ['bower.json'],
tasks: ['auto_install', 'wiredep']
},
linker: {
files: ['scripts.json'],
tasks: ['scriptlinker']
},
gruntfile: {
files: ['Gruntfile.js'],
options: {
reload: true
}
},
livereload: {
options: {
livereload: config.livereload,
},
files: [
'html/**/*.html',
'less/**/*.less'
]
}
},
// Reads HTML for usemin blocks to enable smart deploys that automatically
// concat, minify and revision files. Creates configurations in memory so
// additional tasks can operate on them
useminPrepare: {
html: 'html/*.html',
options: {
dest: 'deploy',
flow: {
steps: {
js: ['concat', 'uglifyjs'],
css: ['cssmin']
},
post: {}
}
}
},
// Performs rewrites based on filerev and the useminPrepare configuration
usemin: {
html: 'deploy/html/*.html',
options: {
assetsDirs: ['deploy'], // defaults to dir of html, ie. deploy/html so look for revved assets in deploy
}
},
// Automatically inject Bower components into the app
wiredep: {
app: {
src: ['html/*.html', 'js/SpecRunner.html'],
// these don't provide bower component definitions
// or should only include parts of the library
// or are used for testing
exclude: [
'modernizer',
'CryptoJS',
'handlebars',
'es5-shim',
'respond',
'json3',
'angular-mocks',
'bootstrap-css-only' // when bootrap is ready we will remove this line
// and add the following lines
// we're using bootstrap.js#2.3.2 but bootstrap-css-only/boostrap.css#3.2.0
// 'bootstrap/docs/assets/css/bootstrap.css'
],
},
},
// Populate $http cache with partials on app.run()
ngtemplates: {
app: {
options: {
module: 'dnb360',
append: true,
},
cwd: 'html',
src: ['partials/**/*.html'],
dest: '.tmp/concat/js/dnb360.min.js',
}
},
// Renames files for browser caching purposes,
filerev: {
deploy: {
src: [
'deploy/js/*.js',
'deploy/css/*.css',
]
}
},
copy: {
deploy: {
files: [{
src: 'html/*.html',
dest: 'deploy/',
}, {
src: 'images/**/*',
dest: 'deploy/'
}, {
src: ['css/main.css', 'css/vendor.css'],
dest: 'deploy/'
}]
},
},
// run watch and proxy tasks concurrently
concurrent: {
watchAndProxy: ['proxy', 'watch'],
options: {
logConcurrentOutput: true
}
}
});
// proxy jboss
grunt.registerTask('proxy', 'Proxy jboss calls', function() {
this.async();
proxyCache.proxy('trunk-dev');
});
// on watch events configure jshint:all to only run on changed file
grunt.event.on('watch', function(action, filepath) {
grunt.config(['jshint', 'all'], filepath);
});
grunt.registerTask('serve', 'Compile then start a connect web server', function (target) {
var task;
switch(target){
case 'deploy':
case 'dist':
task = grunt.task.run([
'deploy',
'connect:deploy:keepalive'
]);
break;
case 'proxy':
task = grunt.task.run([
'wiring',
'connect:dev',
'concurrent:watchAndProxy',
]);
break;
default:
task = grunt.task.run([
'wiring',
'connect:dev',
'browser_output',
'watch',
]);
break;
}
return task;
});
grunt.registerTask('wiring', [
'githooks',
'auto_install',
'scriptlinker',
'wiredep',
'less'
]);
grunt.registerTask('test', ['jshint', 'execute:jsdom']);
grunt.registerTask('deploy', [
// 'test',
'wiring',
'clean:deploy',
'copy:deploy',
'useminPrepare',
// the following tasks have their configuration provided for by useMinPrepare
'concat',
'ngtemplates',
'cssmin',
'uglify',
'filerev',
// update the script/link tags of deploy/html with new revved file names
'usemin',
]);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment