1 var lrSnippet = require('connect-livereload')();
\r
2 var mountFolder = function (connect, dir) {
\r
3 return connect.static(require('path').resolve(dir));
\r
6 module.exports = function ( grunt ) {
\r
9 * Load required Grunt tasks. These are installed based on the versions listed
\r
10 * in `package.json` when you do `npm install` in this directory.
\r
12 grunt.loadNpmTasks('grunt-contrib-clean');
\r
13 grunt.loadNpmTasks('grunt-contrib-copy');
\r
14 grunt.loadNpmTasks('grunt-contrib-jshint');
\r
15 grunt.loadNpmTasks('grunt-contrib-concat');
\r
16 grunt.loadNpmTasks('grunt-contrib-watch');
\r
17 grunt.loadNpmTasks('grunt-contrib-uglify');
\r
18 grunt.loadNpmTasks('grunt-conventional-changelog');
\r
19 grunt.loadNpmTasks('grunt-bump');
\r
20 //grunt.loadNpmTasks('grunt-recess');
\r
21 grunt.loadNpmTasks('grunt-karma');
\r
22 grunt.loadNpmTasks('grunt-ngmin');
\r
23 grunt.loadNpmTasks('grunt-html2js');
\r
24 grunt.loadNpmTasks('grunt-contrib-less');
\r
25 grunt.loadNpmTasks('grunt-contrib-connect');
\r
26 grunt.loadNpmTasks('grunt-open');
\r
29 * Load in our build configuration file.
\r
31 var userConfig = require( './build.config.js' );
\r
34 * This is the configuration object Grunt uses to give each plugin its
\r
39 * We read in our `package.json` file so we can access the package name and
\r
40 * version. It's already there, so we don't repeat ourselves here.
\r
42 pkg: grunt.file.readJSON("package.json"),
\r
45 * The banner is the comment that is placed at the top of our compiled
\r
46 * source files. It is first processed as a Grunt template, where the `<%=`
\r
47 * pairs are evaluated based on this very configuration object.
\r
52 ' * <%= pkg.name %> - v<%= pkg.version %> - <%= grunt.template.today("yyyy-mm-dd") %>\n' +
\r
53 ' * <%= pkg.homepage %>\n' +
\r
55 ' * Copyright (c) <%= grunt.template.today("yyyy") %> <%= pkg.author %>\n' +
\r
56 ' * Licensed <%= pkg.licenses.type %> <<%= pkg.licenses.url %>>\n' +
\r
61 * Creates a changelog on a new version.
\r
65 dest: 'CHANGELOG.md',
\r
66 template: 'changelog.tpl'
\r
71 * Increments the version number, etc.
\r
80 commitMessage: 'chore(release): v%VERSION%',
\r
86 tagName: 'v%VERSION%',
\r
87 tagMessage: 'Version %VERSION%',
\r
94 * The directories to delete when `grunt clean` is executed.
\r
97 '<%= build_dir %>',
\r
98 '<%= compile_dir %>'
\r
102 * The `copy` task just copies files from A to B. We use it here to copy
\r
103 * our project assets (images, fonts, etc.) and javascripts into
\r
104 * `build_dir`, and then to copy the assets to `compile_dir`.
\r
107 build_app_assets: {
\r
111 dest: '<%= build_dir %>/assets/',
\r
117 build_vendor_assets: {
\r
120 src: [ '<%= vendor_files.assets %>' ],
\r
121 dest: '<%= build_dir %>/assets/',
\r
131 src: [ '<%= app_files.js %>' ],
\r
132 dest: '<%= build_dir %>/',
\r
141 src: ['<%= app_files.templates %>'],
\r
142 dest: '<%= build_dir %>/',
\r
151 src: [ '<%= vendor_files.js %>' ],
\r
152 dest: '<%= build_dir %>/',
\r
158 build_vendorimages: {
\r
161 src: [ '<%= vendor_files.images %>' ],
\r
162 dest: '<%= build_dir %>/',
\r
171 src: [ '<%= vendor_files.css %>' ],
\r
172 dest: '<%= build_dir %>',
\r
182 dest: '<%= compile_dir %>/assets',
\r
183 cwd: '<%= build_dir %>/assets',
\r
193 dest: '<%= compile_dir %>/font',
\r
194 cwd: '<%= build_dir %>/font',
\r
202 * `grunt concat` concatenates multiple source files into a single file.
\r
206 * The `build_css` target concatenates compiled CSS and vendor CSS
\r
211 '<%= vendor_files.css %>',
\r
212 '<%= build_dir %>/assets/<%= pkg.name %>-<%= pkg.version %>.css'
\r
214 dest: '<%= build_dir %>/assets/<%= pkg.name %>-<%= pkg.version %>.css'
\r
217 * The `compile_js` target is the concatenation of our application source
\r
218 * code and all specified vendor source code into a single file.
\r
222 banner: '<%= meta.banner %>'
\r
225 '<%= vendor_files.js %>',
\r
227 '<%= build_dir %>/src/**/*.js',
\r
228 '<%= html2js.common.dest %>',
\r
229 '<%= html2js.app.dest %>',
\r
232 dest: '<%= compile_dir %>/assets/<%= pkg.name %>-<%= pkg.version %>.js'
\r
237 * `ng-min` annotates the sources before minifying. That is, it allows us
\r
238 * to code without the array syntax.
\r
244 src: [ '<%= app_files.js %>' ],
\r
245 cwd: '<%= build_dir %>',
\r
246 dest: '<%= build_dir %>',
\r
254 * Minify the sources!
\r
259 banner: '<%= meta.banner %>'
\r
262 '<%= concat.compile_js.dest %>': '<%= concat.compile_js.dest %>'
\r
268 * `less` less plugin handles the LESS compilation and minification automatically
\r
269 * this has been changed to the LESS plugin from recess plugin above because of
\r
270 * out of memory issues with the original plugin.
\r
276 paths: ["assets/css"],
\r
279 strictImports: true
\r
282 '<%= build_dir %>/assets/<%= pkg.name %>-<%= pkg.version %>.css': '<%= app_files.less %>'
\r
287 paths: ["assets/css"],
\r
292 '<%= build_dir %>/assets/<%= pkg.name %>-<%= pkg.version %>.css': '<%= app_files.less %>'
\r
298 * `jshint` defines the rules of our linter as well as which files we
\r
299 * should check. This file, all javascript sources, and all our unit tests
\r
300 * are linted based on the policies listed in `options`. But we can also
\r
301 * specify exclusionary patterns by prefixing them with an exclamation
\r
302 * point (!); this is useful when code comes from a third party but is
\r
303 * nonetheless inside `src/`.
\r
307 '<%= app_files.js %>'
\r
310 '<%= app_files.jsunit %>'
\r
313 'OriginalGruntfile.js'
\r
329 * HTML2JS is a Grunt plugin that takes all of your template files and
\r
330 * places them into JavaScript files as strings that are added to
\r
331 * AngularJS's template cache. This means that the templates too become
\r
332 * part of the initial payload as one JavaScript file. Neat!
\r
336 * These are the templates from `src/app`.
\r
342 src: [ '<%= app_files.atpl %>' ],
\r
343 dest: '<%= build_dir %>/templates-app.js'
\r
347 * These are the templates from `src/common`.
\r
353 src: [ '<%= app_files.ctpl %>' ],
\r
354 dest: '<%= build_dir %>/templates-common.js'
\r
359 * The Karma configurations.
\r
363 configFile: '<%= build_dir %>/karma-unit.js'
\r
368 port: 9877 // IMPORTANT!
\r
376 * The `index` task compiles the `index.html` file as a Grunt template. CSS
\r
377 * and JS files co-exist here but they get split apart later.
\r
382 * During development, we don't want to have wait for compilation,
\r
383 * concatenation, minification, etc. So to avoid these steps, we simply
\r
384 * add all script files directly to the `<head>` of `index.html`. The
\r
385 * `src` property contains the list of included files.
\r
388 dir: '<%= build_dir %>',
\r
390 '<%= html2js.common.dest %>',
\r
391 '<%= html2js.app.dest %>',
\r
392 '<%= vendor_files.css %>',
\r
393 '<%= build_dir %>/assets/<%= pkg.name %>-<%= pkg.version %>.css'
\r
398 * When it is time to have a completely compiled application, we can
\r
399 * alter the above to include only a single JavaScript and a single CSS
\r
400 * file. Now we're back!
\r
403 dir: '<%= compile_dir %>',
\r
405 '<%= concat.compile_js.dest %>',
\r
406 '<%= concat.build_css.dest %>'
\r
407 //'<%= recess.compile.dest %>'
\r
413 * This task compiles the karma template so that changes to its file array
\r
414 * don't have to be managed manually.
\r
418 dir: '<%= build_dir %>',
\r
420 '<%= vendor_files.js %>',
\r
421 '<%= html2js.app.dest %>',
\r
422 '<%= html2js.common.dest %>',
\r
423 '<%= app_files.js_common %>',
\r
424 '<%= app_files.js_app %>',
\r
425 '<%= app_files.jsunit %>'
\r
433 hostname: '0.0.0.0',
\r
434 middleware: function (connect) {
\r
436 mountFolder(connect, 'build'),
\r
444 hostname: '0.0.0.0',
\r
459 path: 'http://127.0.0.1:9000/'
\r
462 path: 'http://127.0.0.1:9001/'
\r
466 * And for rapid development, we have a watch set up that checks to see if
\r
467 * any of the files listed below change, and then to execute the listed
\r
468 * tasks when they do. This just saves us from having to type "grunt" into
\r
469 * the command-line every time we want to see what we're working on; we can
\r
470 * instead just leave "grunt watch" running in a background terminal. Set it
\r
471 * and forget it, as Ron Popeil used to tell us.
\r
473 * But we don't need the same thing to happen for all the files.
\r
477 * By default, we want the Live Reload to work for all tasks; this is
\r
478 * overridden in some tasks (like this file) where browser resources are
\r
479 * unaffected. It runs by default on port 35729, which your browser
\r
480 * plugin should auto-detect.
\r
487 * When the Gruntfile changes, we just want to lint it. In fact, when
\r
488 * your Gruntfile changes, it will automatically be reloaded!
\r
491 files: 'OriginalGruntfile.js',
\r
492 tasks: [ 'jshint:gruntfile' ],
\r
499 * When our JavaScript source files change, we want to run lint them and
\r
500 * run our unit tests.
\r
504 '<%= app_files.js %>'
\r
506 tasks: [ 'jshint:src', 'karma:unit:run', 'copy:build_appjs' ]
\r
510 * When assets are changed, copy them. Note that this will *not* copy new
\r
511 * files, so this is probably not very useful.
\r
517 tasks: [ 'copy:build_app_assets' ]
\r
521 * When index.html changes, we need to compile it.
\r
524 files: [ '<%= app_files.html %>' ],
\r
525 tasks: [ 'index:build' ]
\r
529 * When our templates change, we only rewrite the template cache.
\r
533 '<%= app_files.atpl %>',
\r
534 '<%= app_files.ctpl %>'
\r
536 tasks: [ 'html2js' ]
\r
540 * When the CSS files change, we need to compile and minify them.
\r
543 files: [ 'src/**/*.less' ],
\r
544 tasks: [ 'less:development' ]
\r
548 * When a JavaScript unit test file changes, we only want to lint it and
\r
549 * run the unit tests. We don't want to do any live reloading.
\r
553 '<%= app_files.jsunit %>'
\r
555 tasks: [ 'jshint:test', 'karma:unit:run' ],
\r
563 grunt.initConfig( grunt.util._.extend( taskConfig, userConfig ) );
\r
566 * In order to make it safe to just compile or copy *only* what was changed,
\r
567 * we need to ensure we are starting from a clean, fresh build. So we rename
\r
568 * the `watch` task to `delta` (that's why the configuration var above is
\r
569 * `delta`) and then add a new task called `watch` that does a clean build
\r
570 * before watching for changes.
\r
572 grunt.renameTask( 'watch', 'delta' );
\r
573 grunt.registerTask( 'watch', [ 'build', 'karma:unit', 'delta' ] );
\r
575 grunt.registerTask('live', ['build', 'connect:dev', 'delta']);
\r
577 * The default task is to build and compile.
\r
579 grunt.registerTask( 'default', [ 'build', 'compile' ] );
\r
582 * The `build` task gets your app ready to run for development and testing.
\r
584 grunt.registerTask( 'build', [
\r
585 'clean', 'html2js', 'jshint', 'less:development',
\r
586 'concat:build_css', 'copy:build_app_assets', 'copy:build_vendor_assets',
\r
587 'copy:build_appjs', 'copy:copy_template', 'copy:build_vendorimages', 'copy:build_vendorjs', 'copy:build_vendorcss', 'karmaconfig', 'index:build'
\r
591 * The `compile` task gets your app ready for deployment by concatenating and
\r
592 * minifying your code.
\r
594 grunt.registerTask( 'compile', [
\r
595 'less:production', 'concat:build_css', 'copy:compile_assets', 'ngmin:compile', 'concat:compile_js', 'uglify', 'index:compile'
\r
599 * A utility function to get all app JavaScript sources.
\r
601 function filterForJS ( files ) {
\r
602 return files.filter( function ( file ) {
\r
603 return file.match( /\.js$/ );
\r
608 * A utility function to get all app CSS sources.
\r
610 function filterForCSS ( files ) {
\r
611 return files.filter( function ( file ) {
\r
612 return file.match( /\.css$/ );
\r
617 * The index.html template includes the stylesheet and javascript sources
\r
618 * based on dynamic names calculated in this Gruntfile. This task assembles
\r
619 * the list into variables for the template to use and then runs the
\r
622 grunt.registerMultiTask( 'index', 'Process index.html template', function () {
\r
623 var dirRE = new RegExp( '^('+grunt.config('build_dir')+'|'+grunt.config('compile_dir')+')\/', 'g' );
\r
624 var jsFiles = filterForJS( this.filesSrc ).map( function ( file ) {
\r
625 return file.replace( dirRE, '' );
\r
627 var cssFiles = filterForCSS( this.filesSrc ).map( function ( file ) {
\r
628 return file.replace( dirRE, '' );
\r
631 grunt.file.copy('src/index.html', this.data.dir + '/index.html', {
\r
632 process: function ( contents, path ) {
\r
633 return grunt.template.process( contents, {
\r
637 version: grunt.config( 'pkg.version' )
\r
643 grunt.file.copy('src/login.html', this.data.dir + '/login.html', {
\r
644 process: function ( contents, path ) {
\r
645 return grunt.template.process( contents, {
\r
649 version: grunt.config( 'pkg.version' )
\r
657 * In order to avoid having to specify manually the files needed for karma to
\r
658 * run, we use grunt to manage the list for us. The `karma/*` files are
\r
659 * compiled as grunt templates for use by Karma. Yay!
\r
661 grunt.registerMultiTask( 'karmaconfig', 'Process karma config templates', function () {
\r
662 var jsFiles = filterForJS( this.filesSrc );
\r
664 grunt.file.copy( 'karma/karma-unit.tpl.js', grunt.config( 'build_dir' ) + '/karma-unit.js', {
\r
665 process: function ( contents, path ) {
\r
666 return grunt.template.process( contents, {
\r