Gruntfile-example.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. // Generated on 2015-02-03 using
  2. // generator-webapp 0.5.1
  3. 'use strict';
  4. // # Globbing
  5. // for performance reasons we're only matching one level down:
  6. // 'test/spec/{,*/}*.js'
  7. // If you want to recursively match all subfolders, use:
  8. // 'test/spec/**/*.js'
  9. module.exports = function (grunt) {
  10. // Time how long tasks take. Can help when optimizing build times
  11. require('time-grunt')(grunt);
  12. // Load grunt tasks automatically
  13. require('load-grunt-tasks')(grunt);
  14. // Configurable paths
  15. var config = {
  16. app: 'app',
  17. dist: 'dist'
  18. };
  19. // Define the configuration for all the tasks
  20. grunt.initConfig({
  21. // Project settings
  22. config: config,
  23. // Watches files for changes and runs tasks based on the changed files
  24. watch: {
  25. bower: {
  26. files: ['bower.json'],
  27. tasks: ['wiredep']
  28. },
  29. js: {
  30. files: ['<%= config.app %>/scripts/{,*/}*.js'],
  31. tasks: ['jshint'],
  32. options: {
  33. livereload: true
  34. }
  35. },
  36. jstest: {
  37. files: ['test/spec/{,*/}*.js'],
  38. tasks: ['test:watch']
  39. },
  40. gruntfile: {
  41. files: ['Gruntfile.js']
  42. },
  43. styles: {
  44. files: ['<%= config.app %>/styles/{,*/}*.css'],
  45. tasks: ['newer:copy:styles', 'autoprefixer']
  46. },
  47. livereload: {
  48. options: {
  49. livereload: '<%= connect.options.livereload %>'
  50. },
  51. files: [
  52. '<%= config.app %>/{,*/}*.html',
  53. '.tmp/styles/{,*/}*.css',
  54. '<%= config.app %>/assets/{,*/}*'
  55. ]
  56. }
  57. },
  58. // The actual grunt server settings
  59. connect: {
  60. options: {
  61. port: 8090,
  62. open: true,
  63. livereload: 35729,
  64. // Change this to '0.0.0.0' to access the server from outside
  65. hostname: 'localhost'
  66. },
  67. livereload: {
  68. options: {
  69. middleware: function(connect) {
  70. return [
  71. connect.static('.tmp'),
  72. connect().use('/bower_components', connect.static('./bower_components')),
  73. connect.static(config.app)
  74. ];
  75. }
  76. }
  77. },
  78. test: {
  79. options: {
  80. open: false,
  81. port: 9001,
  82. middleware: function(connect) {
  83. return [
  84. connect.static('.tmp'),
  85. connect.static('test'),
  86. connect().use('/bower_components', connect.static('./bower_components')),
  87. connect.static(config.app)
  88. ];
  89. }
  90. }
  91. },
  92. dist: {
  93. options: {
  94. base: '<%= config.dist %>',
  95. livereload: false
  96. }
  97. }
  98. },
  99. // Empties folders to start fresh
  100. clean: {
  101. dist: {
  102. files: [{
  103. dot: true,
  104. src: [
  105. '.tmp',
  106. '<%= config.dist %>/*',
  107. '!<%= config.dist %>/.git*'
  108. ]
  109. }]
  110. },
  111. server: '.tmp'
  112. },
  113. // Make sure code styles are up to par and there are no obvious mistakes
  114. jshint: {
  115. options: {
  116. jshintrc: '.jshintrc',
  117. reporter: require('jshint-stylish')
  118. },
  119. all: [
  120. 'Gruntfile.js',
  121. '<%= config.app %>/scripts/{,*/}*.js',
  122. '!<%= config.app %>/scripts/vendor/*',
  123. 'test/spec/{,*/}*.js'
  124. ]
  125. },
  126. // Add vendor prefixed styles
  127. autoprefixer: {
  128. options: {
  129. browsers: ['> 1%', 'last 2 versions', 'Firefox ESR', 'Opera 12.1']
  130. },
  131. dist: {
  132. files: [{
  133. expand: true,
  134. cwd: '.tmp/styles/',
  135. src: '{,*/}*.css',
  136. dest: '.tmp/styles/'
  137. }]
  138. }
  139. },
  140. // Automatically inject Bower components into the HTML file
  141. wiredep: {
  142. app: {
  143. ignorePath: /^\/|\.\.\//,
  144. src: ['<%= config.app %>/index.html'],
  145. exclude: ['bower_components/bootstrap/dist/js/bootstrap.js']
  146. }
  147. },
  148. // Renames files for browser caching purposes
  149. rev: {
  150. dist: {
  151. files: {
  152. src: [
  153. '<%= config.dist %>/scripts/{,*/}*.js',
  154. '<%= config.dist %>/styles/{,*/}*.css',
  155. '<%= config.dist %>/assets/{,*/}*.*',
  156. '<%= config.dist %>/styles/fonts/{,*/}*.*',
  157. '<%= config.dist %>/*.{ico,png}'
  158. ]
  159. }
  160. }
  161. },
  162. // Reads HTML for usemin blocks to enable smart builds that automatically
  163. // concat, minify and revision files. Creates configurations in memory so
  164. // additional tasks can operate on them
  165. useminPrepare: {
  166. options: {
  167. dest: '<%= config.dist %>'
  168. },
  169. html: '<%= config.app %>/index.html'
  170. },
  171. // Performs rewrites based on rev and the useminPrepare configuration
  172. usemin: {
  173. options: {
  174. assetsDirs: [
  175. '<%= config.dist %>',
  176. '<%= config.dist %>/assets',
  177. '<%= config.dist %>/styles'
  178. ]
  179. },
  180. html: ['<%= config.dist %>/{,*/}*.html'],
  181. css: ['<%= config.dist %>/styles/{,*/}*.css']
  182. },
  183. // The following *-min tasks produce minified files in the dist folder
  184. imagemin: {
  185. dist: {
  186. files: [{
  187. expand: true,
  188. cwd: '<%= config.app %>/assets',
  189. src: '{,*/}*.{gif,jpeg,jpg,png}',
  190. dest: '<%= config.dist %>/assets'
  191. }]
  192. }
  193. },
  194. svgmin: {
  195. dist: {
  196. files: [{
  197. expand: true,
  198. cwd: '<%= config.app %>/assets',
  199. src: '{,*/}*.svg',
  200. dest: '<%= config.dist %>/assets'
  201. }]
  202. }
  203. },
  204. htmlmin: {
  205. dist: {
  206. options: {
  207. collapseBooleanAttributes: true,
  208. collapseWhitespace: true,
  209. conservativeCollapse: true,
  210. removeAttributeQuotes: true,
  211. removeCommentsFromCDATA: true,
  212. removeEmptyAttributes: true,
  213. removeOptionalTags: true,
  214. removeRedundantAttributes: true,
  215. useShortDoctype: true
  216. },
  217. files: [{
  218. expand: true,
  219. cwd: '<%= config.dist %>',
  220. src: '{,*/}*.html',
  221. dest: '<%= config.dist %>'
  222. }]
  223. }
  224. },
  225. // By default, your `index.html`'s <!-- Usemin block --> will take care
  226. // of minification. These next options are pre-configured if you do not
  227. // wish to use the Usemin blocks.
  228. // cssmin: {
  229. // dist: {
  230. // files: {
  231. // '<%= config.dist %>/styles/main.css': [
  232. // '.tmp/styles/{,*/}*.css',
  233. // '<%= config.app %>/styles/{,*/}*.css'
  234. // ]
  235. // }
  236. // }
  237. // },
  238. // uglify: {
  239. // dist: {
  240. // files: {
  241. // '<%= config.dist %>/scripts/scripts.js': [
  242. // '<%= config.dist %>/scripts/scripts.js'
  243. // ]
  244. // }
  245. // }
  246. // },
  247. // concat: {
  248. // dist: {}
  249. // },
  250. // Copies remaining files to places other tasks can use
  251. copy: {
  252. dist: {
  253. files: [{
  254. expand: true,
  255. dot: true,
  256. cwd: '<%= config.app %>',
  257. dest: '<%= config.dist %>',
  258. src: [
  259. '*.{ico,png,txt}',
  260. 'assets/{,*/}*.webp',
  261. '{,*/}*.html',
  262. 'styles/fonts/{,*/}*.*'
  263. ]
  264. }, {
  265. src: 'node_modules/apache-server-configs/dist/.htaccess',
  266. dest: '<%= config.dist %>/.htaccess'
  267. }, {
  268. expand: true,
  269. dot: true,
  270. cwd: 'bower_components/bootstrap/dist',
  271. src: 'fonts/*',
  272. dest: '<%= config.dist %>'
  273. }]
  274. },
  275. styles: {
  276. expand: true,
  277. dot: true,
  278. cwd: '<%= config.app %>/styles',
  279. dest: '.tmp/styles/',
  280. src: '{,*/}*.css'
  281. }
  282. },
  283. // Generates a custom Modernizr build that includes only the tests you
  284. // reference in your app
  285. modernizr: {
  286. dist: {
  287. devFile: 'bower_components/modernizr/modernizr.js',
  288. outputFile: '<%= config.dist %>/scripts/vendor/modernizr.js',
  289. files: {
  290. src: [
  291. '<%= config.dist %>/scripts/{,*/}*.js',
  292. '<%= config.dist %>/styles/{,*/}*.css',
  293. '!<%= config.dist %>/scripts/vendor/*'
  294. ]
  295. },
  296. uglify: true
  297. }
  298. },
  299. // Run some tasks in parallel to speed up build process
  300. concurrent: {
  301. server: [
  302. 'copy:styles'
  303. ],
  304. test: [
  305. 'copy:styles'
  306. ],
  307. dist: [
  308. 'copy:styles',
  309. 'imagemin',
  310. 'svgmin'
  311. ]
  312. }
  313. });
  314. grunt.registerTask('serve', 'start the server and preview your app, --allow-remote for remote access', function (target) {
  315. if (grunt.option('allow-remote')) {
  316. grunt.config.set('connect.options.hostname', '0.0.0.0');
  317. }
  318. if (target === 'dist') {
  319. return grunt.task.run(['build', 'connect:dist:keepalive']);
  320. }
  321. grunt.task.run([
  322. 'clean:server',
  323. 'wiredep',
  324. 'concurrent:server',
  325. 'autoprefixer',
  326. 'connect:livereload',
  327. 'watch'
  328. ]);
  329. });
  330. grunt.registerTask('server', function (target) {
  331. grunt.log.warn('The `server` task has been deprecated. Use `grunt serve` to start a server.');
  332. grunt.task.run([target ? ('serve:' + target) : 'serve']);
  333. });
  334. grunt.registerTask('test', function (target) {
  335. if (target !== 'watch') {
  336. grunt.task.run([
  337. 'clean:server',
  338. 'concurrent:test',
  339. 'autoprefixer'
  340. ]);
  341. }
  342. grunt.task.run([
  343. 'connect:test',
  344. 'mocha'
  345. ]);
  346. });
  347. grunt.registerTask('build', [
  348. 'clean:dist',
  349. 'wiredep',
  350. 'useminPrepare',
  351. 'concurrent:dist',
  352. 'autoprefixer',
  353. 'concat',
  354. 'cssmin',
  355. 'uglify',
  356. 'copy:dist',
  357. 'modernizr',
  358. 'rev',
  359. 'usemin',
  360. 'htmlmin'
  361. ]);
  362. grunt.registerTask('default', [
  363. 'newer:jshint',
  364. 'test',
  365. 'build'
  366. ]);
  367. };