Gruntfile.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /* jshint node:true */
  2. 'use strict';
  3. module.exports = function (grunt) {
  4. var config = {
  5. pkg: grunt.file.readJSON('package.json'),
  6. srcDir: 'src',
  7. destDir: 'dist',
  8. tempDir: 'tmp',
  9. meta: {
  10. banner: '/*! <%= pkg.name %> - v<%= pkg.version %> - ' +
  11. '<%= grunt.template.today("yyyy-mm-dd") %>\n' +
  12. '<%= pkg.homepage ? " * " + pkg.homepage + "\\n" : "" %>' +
  13. ' * Copyright (c) <%= grunt.template.today("yyyy") %> <%= pkg.author.name %>;' +
  14. ' Licensed <%= pkg.license %> */\n\n'
  15. },
  16. clean: {
  17. on_start: ['<%= destDir %>', '<%= tempDir %>'],
  18. after_require: ['<%= tempDir %>'],
  19. },
  20. copy: {
  21. everthing_left_in_src: {
  22. cwd: '<%= srcDir %>',
  23. expand: true,
  24. src: [
  25. '**/*.js',
  26. '**/*.json',
  27. 'font/**/*',
  28. 'img/**/*',
  29. 'panels/bettermap/leaflet/*.png'
  30. ],
  31. dest: '<%= tempDir %>'
  32. }
  33. },
  34. jshint: {
  35. // just lint the source dir
  36. source: {
  37. files: {
  38. src: ['Gruntfile.js', '<%= srcDir %>/app/**/*.js']
  39. }
  40. },
  41. options: {
  42. jshintrc: '.jshintrc'
  43. }
  44. },
  45. less: {
  46. dist:{
  47. options:{
  48. compress: true
  49. },
  50. expand: true,
  51. cwd:'<%= srcDir %>/vendor/bootstrap/less/',
  52. src: ['bootstrap.dark.less', 'bootstrap.light.less'],
  53. dest: '<%= tempDir %>/css/',
  54. }
  55. },
  56. cssmin: {
  57. dist: {
  58. expand: true,
  59. cwd: '<%= srcDir %>',
  60. src: [
  61. '**/*.css'
  62. ],
  63. dest: '<%= tempDir %>'
  64. }
  65. },
  66. htmlmin:{
  67. dist: {
  68. options:{
  69. removeComments: true,
  70. collapseWhitespace: true
  71. },
  72. expand: true,
  73. cwd: '<%= srcDir %>',
  74. src: [
  75. 'index.html',
  76. 'app/panels/**/*.html',
  77. 'app/partials/**/*.html'
  78. ],
  79. dest: '<%= tempDir %>'
  80. }
  81. },
  82. ngmin: {
  83. scripts: {
  84. expand:true,
  85. cwd:'<%= tempDir %>',
  86. src: [
  87. 'app/controllers/**/*.js',
  88. 'app/directives/**/*.js',
  89. 'app/services/**/*.js',
  90. 'app/filters/**/*.js',
  91. 'app/panels/**/*.js',
  92. 'app/app.js',
  93. 'vendor/angular/**/*.js',
  94. 'vendor/elasticjs/elastic-angular-client.js'
  95. ],
  96. dest: '<%= tempDir %>'
  97. }
  98. },
  99. requirejs: {
  100. compile_temp: {
  101. options: {
  102. appDir: '<%= tempDir %>',
  103. dir: '<%= destDir %>',
  104. mainConfigFile: '<%= tempDir %>/app/components/require.config.js',
  105. modules: [], // populated below
  106. optimize: 'none',
  107. optimizeCss: 'none',
  108. removeCombined: true,
  109. preserveLicenseComments: false,
  110. findNestedDependencies: true,
  111. normalizeDirDefines: "none",
  112. inlineText: true,
  113. skipPragmas: true,
  114. optimizeAllPluginResources: false,
  115. done: function (done, output) {
  116. var duplicates = require('rjs-build-analysis').duplicates(output);
  117. if (duplicates.length > 0) {
  118. grunt.log.subhead('Duplicates found in requirejs build:');
  119. grunt.log.warn(duplicates);
  120. done(new Error('r.js built duplicate modules, please check the excludes option.'));
  121. }
  122. done();
  123. }
  124. }
  125. }
  126. },
  127. uglify: {
  128. dest: {
  129. expand: true,
  130. src: ['**/*.js', '!config.js', '!app/dashboards/*.js'],
  131. dest: '<%= destDir %>',
  132. cwd: '<%= destDir %>',
  133. options: {
  134. quite: true,
  135. compress: true,
  136. preserveComments: false,
  137. banner: '<%= meta.banner %>'
  138. }
  139. }
  140. },
  141. 'git-describe': {
  142. me: {
  143. // Target-specific file lists and/or options go here.
  144. },
  145. }
  146. };
  147. var fs = require('fs');
  148. var requireModules = [
  149. {
  150. // main/common module
  151. name: 'app',
  152. include: [
  153. 'kbn',
  154. 'jquery',
  155. 'underscore',
  156. 'angular',
  157. 'bootstrap',
  158. 'modernizr',
  159. 'jquery',
  160. 'angular-sanitize',
  161. 'timepicker',
  162. 'datepicker',
  163. 'elasticjs',
  164. 'angular-strap',
  165. 'directives/all',
  166. 'filters/all',
  167. 'services/all',
  168. 'jquery.flot',
  169. 'jquery.flot.pie',
  170. 'text',
  171. 'settings'
  172. ]
  173. }
  174. ];
  175. // create a module for each directory in src/app/panels/
  176. fs.readdirSync(config.srcDir+'/app/panels').forEach(function (panelName) {
  177. requireModules.push({
  178. name: 'panels/'+panelName+'/module',
  179. exclude: ['app']
  180. });
  181. });
  182. // exclude the literal config definition from all modules
  183. requireModules.forEach(function (module) {
  184. module.excludeShallow = module.excludeShallow || [];
  185. module.excludeShallow.push('config');
  186. });
  187. config.requirejs.compile_temp.options.modules = requireModules;
  188. // load plugins
  189. grunt.loadNpmTasks('grunt-ngmin');
  190. grunt.loadNpmTasks('grunt-contrib-copy');
  191. grunt.loadNpmTasks('grunt-contrib-less');
  192. grunt.loadNpmTasks('grunt-git-describe');
  193. grunt.loadNpmTasks('grunt-contrib-clean');
  194. grunt.loadNpmTasks('grunt-contrib-jshint');
  195. grunt.loadNpmTasks('grunt-contrib-cssmin');
  196. grunt.loadNpmTasks('grunt-contrib-uglify');
  197. grunt.loadNpmTasks('grunt-string-replace');
  198. grunt.loadNpmTasks('grunt-contrib-htmlmin');
  199. grunt.loadNpmTasks('grunt-contrib-requirejs');
  200. // Project configuration.
  201. grunt.initConfig(config);
  202. // Default task.
  203. grunt.registerTask('default', ['jshint:source','less']);
  204. grunt.registerTask('build', [
  205. 'jshint:source',
  206. 'clean:on_start',
  207. 'htmlmin',
  208. 'less',
  209. 'cssmin',
  210. 'copy:everthing_left_in_src',
  211. 'ngmin',
  212. 'requirejs:compile_temp',
  213. 'clean:after_require',
  214. 'write_revision_to_dest', // runs git-describe and replace:config
  215. 'uglify:dest'
  216. ]);
  217. grunt.registerTask('write_revision_to_dest', function() {
  218. grunt.event.once('git-describe', function (desc) {
  219. grunt.config('string-replace.config', {
  220. src: '<%= destDir %>/app/components/require.config.js',
  221. dest: '<%= destDir %>/app/components/require.config.js',
  222. options: {
  223. replacements: [
  224. {
  225. pattern: /(?:^|\/\/)(.*)@REV@/,
  226. replacement: '$1'+desc.object
  227. }
  228. ]
  229. }
  230. });
  231. grunt.task.run('string-replace:config');
  232. });
  233. grunt.task.run('git-describe');
  234. });
  235. };