Gruntfile.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*jslint node: true */
  2. 'use strict';
  3. // var pkg = require('./package.json');
  4. module.exports = function (grunt) {
  5. // Time how long tasks take. Can help when optimizing build times
  6. require('time-grunt')(grunt);
  7. // load all grunt tasks
  8. require('jit-grunt')(grunt, {
  9. includereplace: 'grunt-include-replace',
  10. useminPrepare: 'grunt-usemin'
  11. });
  12. // Configurable paths
  13. var config = {
  14. app: './src',
  15. dist: 'dist'
  16. };
  17. var ip = grunt.option('ip');
  18. var debug = grunt.option('debug');
  19. var uglify_config = {
  20. options: {
  21. sourceMap: true,
  22. sourceMapIncludeSources:true
  23. }
  24. };
  25. if (debug) {
  26. uglify_config['options']['mangle'] = debug ? false : true;
  27. uglify_config['options']['beautify'] = debug ? false : true;
  28. uglify_config['options']['compress'] = debug ? false : true;
  29. }
  30. // Project configuration.
  31. grunt.initConfig({
  32. uglify: uglify_config,
  33. // Project settings
  34. config: config,
  35. // Watch things
  36. watch: {
  37. bower: {
  38. files: ['bower.json'],
  39. tasks: ['wiredep']
  40. },
  41. styles: {
  42. files: ['<%= config.app %>/css/{,*/}*.css'],
  43. tasks: ['newer:copy:styles', 'postcss']
  44. },
  45. gruntfile: {
  46. files: ['Gruntfile.js']
  47. }
  48. },
  49. wiredep: {
  50. app: {
  51. src: ['src/index.html'],
  52. ignorePath: /\.\.\//
  53. }
  54. },
  55. revision: {
  56. options: {
  57. property: 'meta.revision',
  58. ref: 'HEAD',
  59. short: true
  60. }
  61. },
  62. preprocess: {
  63. options: {
  64. context: {
  65. revision: '<%= meta.revision %>'
  66. }
  67. },
  68. js: {
  69. src: 'src/vertoControllers/controllers/AboutController.source.js',
  70. dest: 'src/vertoControllers/controllers/AboutController.js'
  71. },
  72. },
  73. postcss: {
  74. options: {
  75. map: true,
  76. processors: [
  77. // Add vendor prefixed styles
  78. require('autoprefixer-core')({
  79. browsers: ['> 1%', 'last 2 versions', 'Firefox ESR', 'Opera 12.1']
  80. })
  81. ]
  82. },
  83. dist: {
  84. files: [{
  85. expand: true,
  86. cwd: '.tmp/css/',
  87. src: '{,*/}*.css',
  88. dest: '.tmp/css/'
  89. }]
  90. }
  91. },
  92. browserSync: {
  93. options: {
  94. notify: false,
  95. background: true,
  96. https: true,
  97. open: false,
  98. // logLevel: "debug",
  99. ghostMode: false,
  100. logConnections: true,
  101. ui: false
  102. },
  103. livereload: {
  104. options: {
  105. files: [
  106. '<%= config.app %>/**/*.html',
  107. '.tmp/styles/{,*/}*.css',
  108. '<%= config.app %>/images/{,*/}*',
  109. '<%= config.app %>/locales/{,*/}*',
  110. '.tmp/**/*.js',
  111. '<%= config.app %>/**/*.js'
  112. ],
  113. port: 9001,
  114. server: {
  115. baseDir: ['../js/src/', './js', '.'],
  116. index: 'src/index.html',
  117. middleware: [
  118. require("connect-logger")(),
  119. function(req, res, next) {
  120. if (ip) {
  121. var parsed = require("url").parse(req.url);
  122. if (!parsed.pathname.match(/vertoService\.js$/)) {
  123. next();
  124. return;
  125. } else {
  126. grunt.log.writeln('Providing replaced data.');
  127. var path = require('path');
  128. var theFilePath = path.resolve('./src', 'vertoService', 'services', 'vertoService.js');
  129. var f = require('fs').readFileSync(theFilePath).toString();
  130. res.setHeader('Content-Type', 'text/javascript');
  131. res.end(f.replace(/window\.location\.hostname/gi, ip));
  132. return;
  133. }
  134. }
  135. next();
  136. }
  137. ],
  138. routes: {
  139. '/partials': 'src/partials',
  140. '/locales': 'src/locales',
  141. '/config.json': 'src/config.json',
  142. '/contributors.txt': 'src/contributors.txt',
  143. '/bower_components': './bower_components',
  144. '/js/src': '../js/src',
  145. '/js': './js'
  146. }
  147. }
  148. }
  149. },
  150. dist: {
  151. options: {
  152. port: 9001,
  153. background: false,
  154. server: {
  155. baseDir: ['dist']
  156. }
  157. }
  158. }
  159. },
  160. jshint: {
  161. options: {
  162. jshintrc: '.jshintrc',
  163. reporter: require('jshint-stylish'),
  164. ignores: ['js/3rd-party/**/*.js'],
  165. force: true // TODO: Remove this once we get files linted correctly!!!
  166. },
  167. all: {
  168. src: [
  169. 'Gruntfile.js',
  170. 'src/**/*.js'
  171. ]
  172. }
  173. },
  174. clean: {
  175. dist: {
  176. files: [{
  177. dot: true,
  178. src: [
  179. '.tmp',
  180. 'dist/{,*/}*',
  181. '!dist/.git{,*/}*'
  182. ]
  183. }]
  184. }
  185. },
  186. // Renames files for browser caching purposes
  187. filerev: {
  188. dist: {
  189. src: [
  190. 'dist/scripts/{,*/}*.js',
  191. 'dist/css/{,*/}*.css',
  192. 'dist/images/{,*/}*.{png,jpg,jpeg,gif,webp,svg}',
  193. 'dist/css/fonts/*'
  194. ]
  195. }
  196. },
  197. // Reads HTML for usemin blocks to enable smart builds that automatically
  198. // concat, minify and revision files. Creates configurations in memory so
  199. // additional tasks can operate on them
  200. useminPrepare: {
  201. options: {
  202. dest: '<%= config.dist %>'
  203. },
  204. html: '<%= config.app %>/index.html'
  205. },
  206. // Performs rewrites based on rev and the useminPrepare configuration
  207. usemin: {
  208. options: {
  209. assetsDirs: [
  210. '<%= config.dist %>',
  211. '<%= config.dist %>/images',
  212. '<%= config.dist %>/styles'
  213. ]
  214. },
  215. html: ['<%= config.dist %>/**/*.html'],
  216. css: ['<%= config.dist %>/styles/{,*/}*.css']
  217. },
  218. // The following *-min tasks produce minified files in the dist folder
  219. imagemin: {
  220. dist: {
  221. files: [{
  222. expand: true,
  223. cwd: '<%= config.app %>/images',
  224. src: '{,*/}*.{gif,jpeg,jpg,png}',
  225. dest: '<%= config.dist %>/images'
  226. }]
  227. }
  228. },
  229. svgmin: {
  230. dist: {
  231. files: [{
  232. expand: true,
  233. cwd: '<%= config.app %>/images',
  234. src: '{,*/}*.svg',
  235. dest: '<%= config.dist %>/images'
  236. }]
  237. }
  238. },
  239. // htmlmin: {
  240. // dist: {
  241. // options: {
  242. // collapseBooleanAttributes: true,
  243. // collapseWhitespace: true,
  244. // conservativeCollapse: true,
  245. // removeAttributeQuotes: true,
  246. // removeCommentsFromCDATA: true,
  247. // removeEmptyAttributes: true,
  248. // removeOptionalTags: true,
  249. // // true would impact styles with attribute selectors
  250. // removeRedundantAttributes: false,
  251. // useShortDoctype: true
  252. // },
  253. // files: [{
  254. // expand: true,
  255. // cwd: '<%= config.dist %>',
  256. // src: '{,*/}*.html',
  257. // dest: '<%= config.dist %>'
  258. // },
  259. // {
  260. // expand: true,
  261. // cwd: '<%= config.dist %>/partials',
  262. // src: '{,*/}*.html',
  263. // dest: '<%= config.dist %>/partials'
  264. // }]
  265. // }
  266. // },
  267. // ng-annotate tries to make the code safe for minification automatically
  268. // by using the Angular long form for dependency injection.
  269. ngAnnotate: {
  270. dist: {
  271. files: [{
  272. expand: true,
  273. cwd: '.tmp/concat/scripts',
  274. src: '*.js',
  275. dest: '.tmp/concat/scripts'
  276. }]
  277. }
  278. },
  279. // Copies remaining files to places other tasks can use
  280. copy: {
  281. dist: {
  282. files: [{
  283. expand: true,
  284. dot: true,
  285. cwd: '<%= config.app %>',
  286. dest: 'dist',
  287. src: [
  288. '*.{ico,png,txt}',
  289. '*.html',
  290. '*.json',
  291. 'partials/**/*.html',
  292. 'img/*.png',
  293. 'images/{,*/}*.{webp}',
  294. 'css/fonts/{,*/}*.*',
  295. 'sounds/*.*',
  296. 'locales/*.*'
  297. ]
  298. }, {
  299. expand: true,
  300. cwd: '.tmp/images',
  301. dest: 'dist/images',
  302. src: ['generated/*']
  303. }, {
  304. expand: true,
  305. cwd: 'bower_components/bootstrap/dist',
  306. src: 'fonts/*',
  307. dest: 'dist'
  308. }, {
  309. expand: true,
  310. cwd: 'bower_components/bootstrap-material-design/dist',
  311. src: 'fonts/*',
  312. dest: 'dist'
  313. }]
  314. },
  315. styles: {
  316. expand: true,
  317. cwd: '<%= config.app %>/css',
  318. dest: '.tmp/css/',
  319. src: '{,*/}*.css'
  320. }
  321. },
  322. // Run some tasks in parallel to speed up the build process
  323. concurrent: {
  324. server: [
  325. 'copy:styles'
  326. ],
  327. dist: [
  328. 'copy:styles',
  329. 'imagemin',
  330. 'svgmin'
  331. ]
  332. },
  333. });
  334. grunt.loadNpmTasks('grunt-git-revision');
  335. grunt.loadNpmTasks('grunt-preprocess');
  336. grunt.registerTask('serve', function (target) {
  337. var tasks = [
  338. 'wiredep',
  339. 'concurrent:server',
  340. 'postcss'];
  341. tasks = tasks.concat(['browserSync:livereload',
  342. 'watch']);
  343. grunt.task.run(tasks);
  344. });
  345. grunt.registerTask('default', ['build']);
  346. grunt.registerTask('build', [
  347. 'clean:dist',
  348. 'revision',
  349. 'preprocess',
  350. 'wiredep',
  351. 'useminPrepare',
  352. 'concurrent:dist',
  353. 'postcss',
  354. 'concat',
  355. 'cssmin',
  356. 'ngAnnotate',
  357. 'uglify',
  358. 'copy:dist',
  359. 'filerev',
  360. 'usemin',
  361. // 'htmlmin'
  362. ]);
  363. };