Gruntfile.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. temp: ['<%= 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. zip: {
  158. dist: {
  159. cwd: '<%= destDir %>',
  160. src: ['<%= destDir %>/**/*','LICENSE.md','README.md'],
  161. dest: '<%= tempDir %>/dist.zip'
  162. }
  163. },
  164. s3: {
  165. dist: {
  166. upload: [
  167. {
  168. src: '<%= tempDir %>/dist.zip',
  169. dest: 'kibana/kibana/<%= pkg.name %>-latest.zip',
  170. }
  171. ]
  172. }
  173. }
  174. };
  175. var fs = require('fs');
  176. var requireModules = [
  177. {
  178. // main/common module
  179. name: 'app',
  180. include: [
  181. 'kbn',
  182. 'jquery',
  183. 'underscore',
  184. 'angular',
  185. 'bootstrap',
  186. 'modernizr',
  187. 'jquery',
  188. 'angular-sanitize',
  189. 'timepicker',
  190. 'datepicker',
  191. 'elasticjs',
  192. 'angular-strap',
  193. 'directives/all',
  194. 'filters/all',
  195. 'services/all',
  196. 'jquery.flot',
  197. 'jquery.flot.pie',
  198. 'text',
  199. 'settings'
  200. ]
  201. }
  202. ];
  203. // create a module for each directory in src/app/panels/
  204. fs.readdirSync(config.srcDir+'/app/panels').forEach(function (panelName) {
  205. requireModules.push({
  206. name: 'panels/'+panelName+'/module',
  207. exclude: ['app']
  208. });
  209. });
  210. // exclude the literal config definition from all modules
  211. requireModules.forEach(function (module) {
  212. module.excludeShallow = module.excludeShallow || [];
  213. module.excludeShallow.push('config');
  214. });
  215. config.requirejs.compile_temp.options.modules = requireModules;
  216. // load plugins
  217. grunt.loadNpmTasks('grunt-s3');
  218. grunt.loadNpmTasks('grunt-zip');
  219. grunt.loadNpmTasks('grunt-ngmin');
  220. grunt.loadNpmTasks('grunt-contrib-copy');
  221. grunt.loadNpmTasks('grunt-contrib-less');
  222. grunt.loadNpmTasks('grunt-git-describe');
  223. grunt.loadNpmTasks('grunt-contrib-clean');
  224. grunt.loadNpmTasks('grunt-contrib-jshint');
  225. grunt.loadNpmTasks('grunt-contrib-cssmin');
  226. grunt.loadNpmTasks('grunt-contrib-uglify');
  227. grunt.loadNpmTasks('grunt-string-replace');
  228. grunt.loadNpmTasks('grunt-contrib-htmlmin');
  229. grunt.loadNpmTasks('grunt-contrib-requirejs');
  230. // Project configuration.
  231. grunt.initConfig(config);
  232. // Default task.
  233. grunt.registerTask('default', ['jshint:source','less:src']);
  234. grunt.registerTask('build', [
  235. 'jshint:source',
  236. 'clean:on_start',
  237. 'htmlmin',
  238. 'less:dist',
  239. 'cssmin',
  240. 'copy:everthing_left_in_src',
  241. 'ngmin',
  242. 'requirejs:compile_temp',
  243. 'clean:temp',
  244. 'write_revision_to_dest', // runs git-describe and replace:config
  245. 'uglify:dest'
  246. ]);
  247. grunt.registerTask('distribute', [
  248. 'load_s3_config',
  249. 'build',
  250. 'zip:dist',
  251. 's3:dist',
  252. 'clean:temp'
  253. ]);
  254. grunt.registerTask('load_s3_config', function () {
  255. var config = grunt.file.readJSON('.aws-config.json');
  256. grunt.config('s3.options', {
  257. key: config.key,
  258. secret: config.secret,
  259. bucket: 'download.elasticsearch.org',
  260. access: 'private'
  261. });
  262. });
  263. grunt.registerTask('write_revision_to_dest', function() {
  264. grunt.event.once('git-describe', function (desc) {
  265. grunt.config('string-replace.config', {
  266. src: '<%= destDir %>/app/components/require.config.js',
  267. dest: '<%= destDir %>/app/components/require.config.js',
  268. options: {
  269. replacements: [
  270. {
  271. pattern: /(?:^|\/\/)(.*)@REV@/,
  272. replacement: '$1'+desc.object
  273. }
  274. ]
  275. }
  276. });
  277. grunt.task.run('string-replace:config');
  278. });
  279. grunt.task.run('git-describe');
  280. });
  281. };