You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

304 lines
7.3 KiB

  1. module.exports = function(grunt) {
  2. var
  3. site = grunt.file.readJSON('grunt.config'),
  4. // shortcut
  5. paths = site.paths,
  6. defaultTasks = [
  7. // run grunt watch
  8. 'watch'
  9. ],
  10. watchTasks = [
  11. // compiles less
  12. 'less:build',
  13. // copy js
  14. 'copy:file',
  15. // auto prefix outputted file
  16. 'autoprefixer:prefixFile'
  17. ],
  18. resetTasks = [
  19. // clean build directory
  20. 'clean:output'
  21. ],
  22. buildTasks = [
  23. // clean build directory
  24. 'clean:output',
  25. // compiles less definitions
  26. 'less:buildAll',
  27. // copy assets
  28. 'copy:uncompressedAssets',
  29. 'copy:minifiedAssets',
  30. 'copy:packagedAssets',
  31. // copy javascript definitions
  32. 'copy:javascript',
  33. // auto prefix all outputted files
  34. 'autoprefixer:prefixOutput',
  35. // creates minified js of each output file
  36. 'uglify:minifyOutput',
  37. // creates minified css of each output file
  38. 'cssmin:minifyOutput',
  39. // create concatenated css release
  40. 'concat:createCSSPackage',
  41. // create concatenated js release
  42. 'concat:createJSPackage'
  43. ],
  44. setWatchFiles = function(action, filePath) {
  45. var
  46. outputPath = filePath.replace(paths.source.definitions, paths.output.uncompressed + 'definitions/')
  47. ;
  48. // build less and prefix
  49. if(filePath.search('.less') !== -1) {
  50. outputPath = outputPath.replace('less', 'css');
  51. grunt.config('less.build.src', filePath);
  52. grunt.config('less.build.dest', outputPath);
  53. grunt.config('autoprefixer.prefixFile.src', outputPath);
  54. }
  55. // copy just the one js file
  56. else if(filePath.search('.js') !== -1) {
  57. grunt.config('copy.file.src', filePath);
  58. grunt.config('copy.file.dest', outputPath);
  59. }
  60. // do nothing
  61. else {
  62. grunt.config('less.build.src', 'non/existant/path');
  63. grunt.config('less.build.dest', 'non/existant/path');
  64. grunt.config('autoprefixer.prefixFile.src', 'non/existant/path');
  65. }
  66. },
  67. // this allows filenames with multiple extensions to be preserved
  68. preserveFileExtensions = function(folder, filename) {
  69. return folder + filename.substring(0, filename.lastIndexOf('.') ) + '.css';
  70. },
  71. preserveMinFileExtensions = function(folder, filename) {
  72. return folder + filename.substring(0, filename.lastIndexOf('.') ) + '.min.css';
  73. },
  74. config
  75. ;
  76. config = {
  77. site : site,
  78. paths : site.paths,
  79. /*******************************
  80. Watch
  81. *******************************/
  82. // watches for changes in a source folder
  83. watch: {
  84. options: {
  85. spawn: false
  86. },
  87. src: {
  88. files: [
  89. paths.source.definitions + '**/*.less',
  90. paths.source.definitions + '**/*.variables',
  91. paths.source.definitions + '**/*.overrides',
  92. paths.source.definitions + '**/*.js'
  93. ],
  94. tasks : watchTasks
  95. }
  96. },
  97. /*******************************
  98. Test
  99. *******************************/
  100. // Clear terminal output
  101. clear: {
  102. terminal: {}
  103. },
  104. // Configured at run time
  105. copy: {
  106. file: {},
  107. uncompressedAssets: {
  108. expand: true,
  109. cwd: paths.source.themes,
  110. src : [
  111. [
  112. '**/assets/**/*'
  113. ]
  114. ],
  115. dest : paths.output.uncompressed + '/themes'
  116. },
  117. minifiedAssets: {
  118. expand: true,
  119. cwd: paths.source.themes,
  120. src : [
  121. [
  122. '**/assets/**/*'
  123. ]
  124. ],
  125. dest : paths.output.minified + '/themes'
  126. },
  127. packagedAssets: {
  128. expand: true,
  129. cwd: paths.source.themes,
  130. src : [
  131. [
  132. '**/assets/**/*'
  133. ]
  134. ],
  135. dest : paths.output.packaged + '/themes'
  136. },
  137. javascript: {
  138. expand : true,
  139. cwd : paths.source.definitions,
  140. src : ['**/*.js'],
  141. dest : paths.output.uncompressed + '/definitions/'
  142. }
  143. },
  144. less: {
  145. options: {
  146. paths : ['src'],
  147. compress : false,
  148. optimization : 2
  149. },
  150. build: {
  151. rename : preserveFileExtensions
  152. },
  153. buildAll: {
  154. expand : true,
  155. cwd : paths.source.definitions,
  156. src : ['**/*.less'],
  157. dest : paths.output.uncompressed + '/definitions/',
  158. rename : preserveFileExtensions
  159. }
  160. },
  161. // Clean a folder
  162. clean: {
  163. options: {
  164. force: true
  165. },
  166. output : [
  167. paths.output.uncompressed,
  168. paths.output.minified,
  169. paths.output.packaged
  170. ]
  171. },
  172. // Prefix
  173. autoprefixer: {
  174. options: {
  175. browsers: site.support
  176. },
  177. prefixOutput: {
  178. expand : true,
  179. cwd : paths.output.uncompressed,
  180. dest : paths.output.uncompressed,
  181. src : [
  182. '**/*.css'
  183. ]
  184. },
  185. prefixFile: {
  186. src : paths.output.uncompressed + '**/*.css'
  187. }
  188. },
  189. // Minify
  190. cssmin: {
  191. options : {
  192. keepSpecialComments: 0,
  193. report: 'min',
  194. banner : '' +
  195. '/*\n' +
  196. '* # Semantic UI ' +
  197. '* http://github.com/semantic-org/semantic-ui\n' +
  198. '*\n' +
  199. '* Copyright <%= grunt.template.today("yyyy") %> \n' +
  200. '* Built: <%= grunt.template.today("mm/dd/yyyy") %>\n' +
  201. '*/\n'
  202. },
  203. minifyOutput: {
  204. expand : true,
  205. cwd : paths.output.uncompressed,
  206. src : [
  207. '**/*.css'
  208. ],
  209. dest : paths.output.minified,
  210. rename : preserveMinFileExtensions
  211. }
  212. },
  213. // Minify JS
  214. uglify: {
  215. minifyOutput: {
  216. expand : true,
  217. cwd : paths.output.uncompressed,
  218. src : [
  219. '**/*.js'
  220. ],
  221. dest : paths.output.minified,
  222. ext : '.min.js',
  223. banner : '' +
  224. '/*' +
  225. '* # Semantic UI\n' +
  226. '* http://github.com/semantic-org/semantic-ui\n' +
  227. '*\n' +
  228. '* Copyright <%= grunt.template.today("yyyy") %> Contributors\n' +
  229. '*\n' +
  230. '* Build Date: <%= grunt.template.today("mm/dd/yyyy") %>\n' +
  231. '*/\n'
  232. }
  233. },
  234. concat: {
  235. options: {
  236. },
  237. createCSSPackage: {
  238. src: [ paths.output.minified + '**/*.css'],
  239. dest: paths.output.packaged + 'definitions/css/semantic.css'
  240. },
  241. createJSPackage: {
  242. src: [ paths.output.minified + '**/*.js'],
  243. dest: paths.output.packaged + 'definitions/js/semantic.js'
  244. }
  245. }
  246. };
  247. // filesys & terminal
  248. grunt.loadNpmTasks('grunt-contrib-clean');
  249. grunt.loadNpmTasks('grunt-contrib-watch');
  250. grunt.loadNpmTasks('grunt-contrib-copy');
  251. grunt.loadNpmTasks('grunt-clear');
  252. grunt.loadNpmTasks('grunt-contrib-concat');
  253. // css
  254. grunt.loadNpmTasks('grunt-contrib-cssmin');
  255. grunt.loadNpmTasks('grunt-contrib-less');
  256. grunt.loadNpmTasks('grunt-autoprefixer');
  257. // javascript
  258. grunt.loadNpmTasks('grunt-contrib-uglify');
  259. grunt.initConfig(config);
  260. grunt.registerTask('default', defaultTasks);
  261. grunt.registerTask('build', buildTasks);
  262. grunt.registerTask('reset', resetTasks);
  263. // compiles only changed less files <https://npmjs.org/package/grunt-contrib-watch>
  264. grunt.event.on('watch', setWatchFiles);
  265. };