|
| 1 | +module.exports = function ( grunt ) { |
| 2 | + |
| 3 | + /** |
| 4 | + * Load required Grunt tasks. These are installed based on the versions listed |
| 5 | + * in `package.json` when you do `npm install` in this directory. |
| 6 | + */ |
| 7 | + grunt.loadNpmTasks('grunt-contrib-clean'); |
| 8 | + grunt.loadNpmTasks('grunt-contrib-copy'); |
| 9 | + grunt.loadNpmTasks('grunt-contrib-jshint'); |
| 10 | + grunt.loadNpmTasks('grunt-contrib-concat'); |
| 11 | + grunt.loadNpmTasks('grunt-contrib-watch'); |
| 12 | + grunt.loadNpmTasks('grunt-contrib-uglify'); |
| 13 | + grunt.loadNpmTasks('grunt-conventional-changelog'); |
| 14 | + grunt.loadNpmTasks('grunt-bump'); |
| 15 | + grunt.loadNpmTasks('grunt-karma'); |
| 16 | + grunt.loadNpmTasks('grunt-ngmin'); |
| 17 | + grunt.loadNpmTasks('grunt-html2js'); |
| 18 | + |
| 19 | + /** |
| 20 | + * Load in our build configuration file. |
| 21 | + */ |
| 22 | + var userConfig = require( './build.config.js' ); |
| 23 | + |
| 24 | + /** |
| 25 | + * This is the configuration object Grunt uses to give each plugin its |
| 26 | + * instructions. |
| 27 | + */ |
| 28 | + var taskConfig = { |
| 29 | + /** |
| 30 | + * We read in our `package.json` file so we can access the package name and |
| 31 | + * version. It's already there, so we don't repeat ourselves here. |
| 32 | + */ |
| 33 | + pkg: grunt.file.readJSON("package.json"), |
| 34 | + |
| 35 | + /** |
| 36 | + * The banner is the comment that is placed at the top of our compiled |
| 37 | + * source files. It is first processed as a Grunt template, where the `<%=` |
| 38 | + * pairs are evaluated based on this very configuration object. |
| 39 | + */ |
| 40 | + meta: { |
| 41 | + banner: |
| 42 | + '/**\n' + |
| 43 | + ' * <%= pkg.name %> - v<%= pkg.version %> - <%= grunt.template.today("yyyy-mm-dd") %>\n' + |
| 44 | + ' * <%= pkg.homepage %>\n' + |
| 45 | + ' *\n' + |
| 46 | + ' * Copyright (c) <%= grunt.template.today("yyyy") %> <%= pkg.author %>\n' + |
| 47 | + ' * Licensed <%= pkg.licenses.type %> <<%= pkg.licenses.url %>>\n' + |
| 48 | + ' */\n' |
| 49 | + }, |
| 50 | + |
| 51 | + /** |
| 52 | + * Creates a changelog on a new version. |
| 53 | + */ |
| 54 | + changelog: { |
| 55 | + options: { |
| 56 | + dest: 'CHANGELOG.md', |
| 57 | + template: 'changelog.tpl' |
| 58 | + } |
| 59 | + }, |
| 60 | + |
| 61 | + /** |
| 62 | + * Increments the version number, etc. |
| 63 | + */ |
| 64 | + bump: { |
| 65 | + options: { |
| 66 | + files: [ |
| 67 | + "package.json", |
| 68 | + "bower.json" |
| 69 | + ], |
| 70 | + commit: false, |
| 71 | + commitMessage: 'chore(release): v%VERSION%', |
| 72 | + commitFiles: [ |
| 73 | + "package.json", |
| 74 | + "client/bower.json" |
| 75 | + ], |
| 76 | + createTag: false, |
| 77 | + tagName: 'v%VERSION%', |
| 78 | + tagMessage: 'Version %VERSION%', |
| 79 | + push: false, |
| 80 | + pushTo: 'origin' |
| 81 | + } |
| 82 | + }, |
| 83 | + |
| 84 | + /** |
| 85 | + * The directories to delete when `grunt clean` is executed. |
| 86 | + */ |
| 87 | + clean: [ |
| 88 | + '<%= build_dir %>', |
| 89 | + '<%= compile_dir %>' |
| 90 | + ], |
| 91 | + |
| 92 | + /** |
| 93 | + * The `copy` task just copies files from A to B. We use it here to copy |
| 94 | + * our project assets (images, fonts, etc.) and javascripts into |
| 95 | + * `build_dir`, and then to copy the assets to `compile_dir`. |
| 96 | + */ |
| 97 | + copy: { |
| 98 | + build_appjs: { |
| 99 | + files: [ |
| 100 | + { |
| 101 | + src: [ '<%= app_files.js %>' ], |
| 102 | + dest: '<%= build_dir %>/', |
| 103 | + cwd: '.', |
| 104 | + expand: true |
| 105 | + } |
| 106 | + ] |
| 107 | + }, |
| 108 | + copy_appjs: { |
| 109 | + files: [ |
| 110 | + { |
| 111 | + src : '<%= build_dir %>/app/js/timer.js', |
| 112 | + dest: '<%= compile_dir %>/', |
| 113 | + cwd: '.', |
| 114 | + filter: 'isFile', |
| 115 | + flatten: true, |
| 116 | + expand: true, |
| 117 | + rename: function(dest, src) { |
| 118 | + return dest + '<%= pkg.name %>.js'; |
| 119 | + } |
| 120 | + } |
| 121 | + ] |
| 122 | + } |
| 123 | + }, |
| 124 | + |
| 125 | + |
| 126 | + /** |
| 127 | + * `grunt concat` concatenates multiple source files into a single file. |
| 128 | + */ |
| 129 | + concat: { |
| 130 | + /** |
| 131 | + * The `compile_js` target is the concatenation of our application source |
| 132 | + * code and all specified vendor source code into a single file. |
| 133 | + */ |
| 134 | + compile_js: { |
| 135 | + options: { |
| 136 | + banner: '<%= meta.banner %>' |
| 137 | + }, |
| 138 | + src: [ |
| 139 | + 'module.prefix', |
| 140 | + '<%= build_dir %>/app/**/*.js', |
| 141 | + 'module.suffix' |
| 142 | + ], |
| 143 | + dest: '<%= compile_dir %>/<%= pkg.name %>.min.js' |
| 144 | + } |
| 145 | + }, |
| 146 | + |
| 147 | + /** |
| 148 | + * `ng-min` annotates the sources before minifying. That is, it allows us |
| 149 | + * to code without the array syntax. |
| 150 | + */ |
| 151 | + ngmin: { |
| 152 | + compile: { |
| 153 | + files: [ |
| 154 | + { |
| 155 | + src: [ '<%= app_files.js %>' ], |
| 156 | + cwd: '<%= build_dir %>', |
| 157 | + dest: '<%= build_dir %>', |
| 158 | + expand: true |
| 159 | + } |
| 160 | + ] |
| 161 | + } |
| 162 | + }, |
| 163 | + |
| 164 | + /** |
| 165 | + * Minify the sources! |
| 166 | + */ |
| 167 | + uglify: { |
| 168 | + compile: { |
| 169 | + options: { |
| 170 | + banner: '<%= meta.banner %>' |
| 171 | + }, |
| 172 | + files: { |
| 173 | + '<%= concat.compile_js.dest %>': '<%= concat.compile_js.dest %>' |
| 174 | + } |
| 175 | + } |
| 176 | + }, |
| 177 | + |
| 178 | + /** |
| 179 | + * `jshint` defines the rules of our linter as well as which files we |
| 180 | + * should check. This file, all javascript sources, and all our unit tests |
| 181 | + * are linted based on the policies listed in `options`. But we can also |
| 182 | + * specify exclusionary patterns by prefixing them with an exclamation |
| 183 | + * point (!); this is useful when code comes from a third party but is |
| 184 | + * nonetheless inside `src/`. |
| 185 | + */ |
| 186 | + jshint: { |
| 187 | + src: [ |
| 188 | + '<%= app_files.js %>' |
| 189 | + ], |
| 190 | + gruntfile: [ |
| 191 | + 'Gruntfile.js' |
| 192 | + ], |
| 193 | + options: { |
| 194 | + curly: true, |
| 195 | + immed: true, |
| 196 | + newcap: true, |
| 197 | + noarg: true, |
| 198 | + sub: true, |
| 199 | + boss: true, |
| 200 | + eqnull: true |
| 201 | + }, |
| 202 | + globals: {} |
| 203 | + }, |
| 204 | + |
| 205 | + /** |
| 206 | + * The `index` task compiles the `index.html` file as a Grunt template. CSS |
| 207 | + * and JS files co-exist here but they get split apart later. |
| 208 | + */ |
| 209 | + index: { |
| 210 | + |
| 211 | + /** |
| 212 | + * During development, we don't want to have wait for compilation, |
| 213 | + * concatenation, minification, etc. So to avoid these steps, we simply |
| 214 | + * add all script files directly to the `<head>` of `index.html`. The |
| 215 | + * `src` property contains the list of included files. |
| 216 | + */ |
| 217 | + build: { |
| 218 | + dir: '<%= build_dir %>', |
| 219 | + src: [ |
| 220 | + '<%= build_dir %>/src/**/*.js' |
| 221 | + ] |
| 222 | + }, |
| 223 | + |
| 224 | + /** |
| 225 | + * When it is time to have a completely compiled application, we can |
| 226 | + * alter the above to include only a single JavaScript and a single CSS |
| 227 | + * file. Now we're back! |
| 228 | + */ |
| 229 | + compile: { |
| 230 | + dir: '<%= compile_dir %>', |
| 231 | + src: [ |
| 232 | + '<%= concat.compile_js.dest %>' |
| 233 | + ] |
| 234 | + } |
| 235 | + } |
| 236 | + |
| 237 | + }; |
| 238 | + |
| 239 | + grunt.initConfig( grunt.util._.extend( taskConfig, userConfig ) ); |
| 240 | + |
| 241 | + /** |
| 242 | + * In order to make it safe to just compile or copy *only* what was changed, |
| 243 | + * we need to ensure we are starting from a clean, fresh build. So we rename |
| 244 | + * the `watch` task to `delta` (that's why the configuration var above is |
| 245 | + * `delta`) and then add a new task called `watch` that does a clean build |
| 246 | + * before watching for changes. |
| 247 | + */ |
| 248 | + grunt.renameTask( 'watch', 'delta' ); |
| 249 | + grunt.registerTask( 'watch', [ 'build'] ); |
| 250 | + |
| 251 | + /** |
| 252 | + * The default task is to build and compile. |
| 253 | + */ |
| 254 | + grunt.registerTask( 'default', [ 'build', 'compile' ] ); |
| 255 | + |
| 256 | + /** |
| 257 | + * The `build` task gets your app ready to run for development and testing. |
| 258 | + */ |
| 259 | + grunt.registerTask( 'build', [ |
| 260 | + 'clean', 'jshint', |
| 261 | + 'copy:build_appjs', |
| 262 | + 'copy:copy_appjs' |
| 263 | + |
| 264 | + ]); |
| 265 | + |
| 266 | + /** |
| 267 | + * The `compile` task gets your app ready for deployment by concatenating and |
| 268 | + * minifying your code. |
| 269 | + */ |
| 270 | + grunt.registerTask( 'compile', [ |
| 271 | + 'ngmin', 'concat', 'uglify' |
| 272 | + ]); |
| 273 | + |
| 274 | + /** |
| 275 | + * A utility function to get all app JavaScript sources. |
| 276 | + */ |
| 277 | + function filterForJS ( files ) { |
| 278 | + return files.filter( function ( file ) { |
| 279 | + return file.match( /\.js$/ ); |
| 280 | + }); |
| 281 | + } |
| 282 | + |
| 283 | + |
| 284 | +}; |
0 commit comments