Gruntfile.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. // Compile to src when not building
  56. src:{
  57. options: {
  58. paths: ["<%= srcDir %>/vendor/bootstrap/less"],
  59. yuicompress:true
  60. },
  61. files: {
  62. "<%= srcDir %>/css/bootstrap.dark.min.css": "<%= srcDir %>/vendor/bootstrap/less/bootstrap.dark.less",
  63. "<%= srcDir %>/css/bootstrap.light.min.css": "<%= srcDir %>/vendor/bootstrap/less/bootstrap.light.less"
  64. }
  65. }
  66. },
  67. cssmin: {
  68. dist: {
  69. expand: true,
  70. cwd: '<%= srcDir %>',
  71. src: [
  72. '**/*.css'
  73. ],
  74. dest: '<%= tempDir %>'
  75. }
  76. },
  77. htmlmin:{
  78. dist: {
  79. options:{
  80. removeComments: true,
  81. collapseWhitespace: true
  82. },
  83. expand: true,
  84. cwd: '<%= srcDir %>',
  85. src: [
  86. 'index.html',
  87. 'app/panels/**/*.html',
  88. 'app/partials/**/*.html'
  89. ],
  90. dest: '<%= tempDir %>'
  91. }
  92. },
  93. ngmin: {
  94. scripts: {
  95. expand:true,
  96. cwd:'<%= tempDir %>',
  97. src: [
  98. 'app/controllers/**/*.js',
  99. 'app/directives/**/*.js',
  100. 'app/services/**/*.js',
  101. 'app/filters/**/*.js',
  102. 'app/panels/**/*.js',
  103. 'app/app.js',
  104. 'vendor/angular/**/*.js',
  105. 'vendor/elasticjs/elastic-angular-client.js'
  106. ],
  107. dest: '<%= tempDir %>'
  108. }
  109. },
  110. requirejs: {
  111. compile_temp: {
  112. options: {
  113. appDir: '<%= tempDir %>',
  114. dir: '<%= destDir %>',
  115. mainConfigFile: '<%= tempDir %>/app/components/require.config.js',
  116. modules: [], // populated below
  117. optimize: 'none',
  118. optimizeCss: 'none',
  119. removeCombined: true,
  120. preserveLicenseComments: false,
  121. findNestedDependencies: true,
  122. normalizeDirDefines: "none",
  123. inlineText: true,
  124. skipPragmas: true,
  125. optimizeAllPluginResources: false,
  126. done: function (done, output) {
  127. var duplicates = require('rjs-build-analysis').duplicates(output);
  128. if (duplicates.length > 0) {
  129. grunt.log.subhead('Duplicates found in requirejs build:');
  130. grunt.log.warn(duplicates);
  131. done(new Error('r.js built duplicate modules, please check the excludes option.'));
  132. }
  133. done();
  134. }
  135. }
  136. }
  137. },
  138. uglify: {
  139. dest: {
  140. expand: true,
  141. src: ['**/*.js', '!config.js', '!app/dashboards/*.js'],
  142. dest: '<%= destDir %>',
  143. cwd: '<%= destDir %>',
  144. options: {
  145. quite: true,
  146. compress: true,
  147. preserveComments: false,
  148. banner: '<%= meta.banner %>'
  149. }
  150. }
  151. },
  152. 'git-describe': {
  153. me: {
  154. // Target-specific file lists and/or options go here.
  155. },
  156. }
  157. };
  158. var fs = require('fs');
  159. var requireModules = [
  160. {
  161. // main/common module
  162. name: 'app',
  163. include: [
  164. 'kbn',
  165. 'jquery',
  166. 'underscore',
  167. 'angular',
  168. 'bootstrap',
  169. 'modernizr',
  170. 'jquery',
  171. 'angular-sanitize',
  172. 'timepicker',
  173. 'datepicker',
  174. 'elasticjs',
  175. 'angular-strap',
  176. 'directives/all',
  177. 'filters/all',
  178. 'services/all',
  179. 'jquery.flot',
  180. 'jquery.flot.pie',
  181. 'text',
  182. 'settings'
  183. ]
  184. }
  185. ];
  186. // create a module for each directory in src/app/panels/
  187. fs.readdirSync(config.srcDir+'/app/panels').forEach(function (panelName) {
  188. requireModules.push({
  189. name: 'panels/'+panelName+'/module',
  190. exclude: ['app']
  191. });
  192. });
  193. // exclude the literal config definition from all modules
  194. requireModules.forEach(function (module) {
  195. module.excludeShallow = module.excludeShallow || [];
  196. module.excludeShallow.push('config');
  197. });
  198. config.requirejs.compile_temp.options.modules = requireModules;
  199. // load plugins
  200. grunt.loadNpmTasks('grunt-ngmin');
  201. grunt.loadNpmTasks('grunt-contrib-copy');
  202. grunt.loadNpmTasks('grunt-contrib-less');
  203. grunt.loadNpmTasks('grunt-git-describe');
  204. grunt.loadNpmTasks('grunt-contrib-clean');
  205. grunt.loadNpmTasks('grunt-contrib-jshint');
  206. grunt.loadNpmTasks('grunt-contrib-cssmin');
  207. grunt.loadNpmTasks('grunt-contrib-uglify');
  208. grunt.loadNpmTasks('grunt-string-replace');
  209. grunt.loadNpmTasks('grunt-contrib-htmlmin');
  210. grunt.loadNpmTasks('grunt-contrib-requirejs');
  211. // Project configuration.
  212. grunt.initConfig(config);
  213. // Default task.
  214. grunt.registerTask('default', ['jshint:source','less:src']);
  215. grunt.registerTask('build', [
  216. 'jshint:source',
  217. 'clean:on_start',
  218. 'htmlmin',
  219. 'less:dist',
  220. 'cssmin',
  221. 'copy:everthing_left_in_src',
  222. 'ngmin',
  223. 'requirejs:compile_temp',
  224. 'clean:after_require',
  225. 'write_revision_to_dest', // runs git-describe and replace:config
  226. 'uglify:dest'
  227. ]);
  228. grunt.registerTask('write_revision_to_dest', function() {
  229. grunt.event.once('git-describe', function (desc) {
  230. grunt.config('string-replace.config', {
  231. src: '<%= destDir %>/app/components/require.config.js',
  232. dest: '<%= destDir %>/app/components/require.config.js',
  233. options: {
  234. replacements: [
  235. {
  236. pattern: /(?:^|\/\/)(.*)@REV@/,
  237. replacement: '$1'+desc.object
  238. }
  239. ]
  240. }
  241. });
  242. grunt.task.run('string-replace:config');
  243. });
  244. grunt.task.run('git-describe');
  245. });
  246. };