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.

575 lines
13 KiB

11 years ago
11 years ago
  1. module.exports = function(grunt) {
  2. var
  3. defaultTasks = [
  4. // run grunt watch
  5. 'watch'
  6. ],
  7. watchTasks = [
  8. // compiles less to docs
  9. 'less:buildDocsCSS',
  10. // copies assets and js over to docs
  11. 'copy:srcToDocs',
  12. // copies examples over to docs
  13. 'copy:examplesToDocs'
  14. ],
  15. testWatchTasks = [
  16. 'clear',
  17. 'karma:watch:run'
  18. ],
  19. testTasks = [
  20. // compiles less to docs so phantomjs can read
  21. 'less:buildTestCSS',
  22. // test components
  23. 'karma:travis'
  24. ],
  25. specTasks = [
  26. // generate code docs
  27. 'docco:generate',
  28. // copies spec files over to docs
  29. 'copy:specToDocs'
  30. ],
  31. buildTasks = [
  32. // clean build directory
  33. 'clean:build',
  34. // compiles less
  35. 'less:buildCSS',
  36. // copies assets and js over to build dir
  37. 'copy:srcToBuild',
  38. // creates minified css of each file
  39. 'cssmin:minifyCSS',
  40. // creates custom license in header
  41. 'cssmin:addBanner',
  42. // create concatenated css release
  43. 'concat:concatenateCSS',
  44. // create concatenated js release
  45. 'concat:concatenateJS',
  46. // creates minified js of each file
  47. 'uglify:minifyJS',
  48. // creates release js of all together
  49. 'uglify:buildReleaseJS',
  50. // creates minified css of each file
  51. 'cssmin:minifyCSS',
  52. // creates custom license in header
  53. 'cssmin:addBanner',
  54. // cleans previous generated release
  55. 'clean:release',
  56. // creates release zip
  57. 'compress:everything',
  58. // copies assets to rtl
  59. 'copy:buildToRTL',
  60. // create rtl release
  61. 'cssjanus:rtl',
  62. // copies examples over to docs
  63. 'copy:examplesToDocs',
  64. // copies files over to docs
  65. 'copy:buildToDocs'
  66. ],
  67. setWatchTests = function(action, filePath) {
  68. var
  69. karmaFiles = grunt.config('karma.watch.files'),
  70. isJavascript = (filePath.search('.js') !== -1),
  71. isModule = (filePath.search('modules/') !== -1),
  72. isSpec = (filePath.search('.spec') !== -1),
  73. specFile = (isSpec)
  74. ? filePath
  75. : filePath
  76. .replace('src/', 'test/')
  77. .replace('.js', '.spec.js')
  78. ;
  79. if(isJavascript && (isSpec || isModule) ) {
  80. karmaFiles.pop();
  81. karmaFiles.push(specFile);
  82. }
  83. },
  84. setWatchFiles = function(action, filePath) {
  85. var
  86. buildPath = filePath.replace('src/', 'docs/build/uncompressed/').replace('less', 'css')
  87. ;
  88. if(filePath.search('.less') !== -1) {
  89. grunt.config('less.buildDocsCSS.src', filePath);
  90. grunt.config('less.buildDocsCSS.dest', buildPath);
  91. }
  92. else {
  93. grunt.config('less.buildDocsCSS.src', 'non/existant/path');
  94. grunt.config('less.buildDocsCSS.dest', 'non/existant/path');
  95. }
  96. },
  97. // this allows filenames with multiple extensions to be preserved
  98. preserveFileExtensions = function(folder, filename) {
  99. return folder + filename.substring(0, filename.lastIndexOf('.') ) + '.css';
  100. },
  101. config
  102. ;
  103. config = {
  104. package : grunt.file.readJSON('package.json'),
  105. /*******************************
  106. Watch
  107. *******************************/
  108. // watches for changes in a source folder
  109. watch: {
  110. options: {
  111. spawn: false
  112. },
  113. scripts: {
  114. files: [
  115. 'test/**/*.js',
  116. 'src/**/*.js'
  117. ],
  118. tasks : testWatchTasks
  119. },
  120. src: {
  121. files: [
  122. 'build/examples/**/*',
  123. 'src/**/*.less',
  124. 'src/**/*.js'
  125. ],
  126. tasks : watchTasks
  127. }
  128. },
  129. /*******************************
  130. Test
  131. *******************************/
  132. clear: {
  133. terminal: {
  134. }
  135. },
  136. karma: {
  137. watch: {
  138. configFile : 'karma.conf.js',
  139. background : true
  140. },
  141. travis: {
  142. configFile : 'karma.conf.js',
  143. singleRun : true
  144. }
  145. },
  146. /*******************************
  147. Build
  148. *******************************/
  149. clean: {
  150. options: {
  151. force: true
  152. },
  153. build : [
  154. 'build/less',
  155. 'build/minified',
  156. 'build/packaged',
  157. 'build/uncompressed'
  158. ],
  159. release : [
  160. 'docs/build',
  161. 'docs',
  162. 'rtl'
  163. ]
  164. },
  165. docco: {
  166. generate: {
  167. options: {
  168. css : 'spec/assets/docco.css',
  169. output : 'spec/docs/'
  170. },
  171. files: [
  172. {
  173. expand : true,
  174. cwd : 'spec/',
  175. src : [
  176. '**.commented.js'
  177. ]
  178. }
  179. ]
  180. }
  181. },
  182. cssjanus: {
  183. rtl: {
  184. expand : true,
  185. cwd : 'build/',
  186. src : [
  187. '**/*.less',
  188. '**/*.css',
  189. ],
  190. dest : 'rtl'
  191. },
  192. },
  193. less: {
  194. options: {
  195. paths : ['src'],
  196. compress : false,
  197. optimization : 2
  198. },
  199. // optimized for watch, src is built on watch task using callbacks
  200. buildDocsCSS: {
  201. src : 'src',
  202. dest : 'docs/build/uncompressed/',
  203. rename : preserveFileExtensions
  204. },
  205. buildTestCSS: {
  206. expand : true,
  207. cwd : 'src',
  208. src : [
  209. '**/*.less'
  210. ],
  211. dest : 'docs/build/uncompressed/',
  212. rename: preserveFileExtensions
  213. },
  214. buildCSS: {
  215. expand : true,
  216. cwd : 'src',
  217. src : [
  218. '**/*.less'
  219. ],
  220. dest : 'build/uncompressed/',
  221. rename: preserveFileExtensions
  222. }
  223. },
  224. copy: {
  225. srcToDocs: {
  226. files: [
  227. // exact copy for less
  228. {
  229. expand : true,
  230. cwd : 'src/**/*.less',
  231. src : [
  232. '**/*'
  233. ],
  234. dest : 'docs/build/less'
  235. },
  236. // copy everything but less files for uncompressed release
  237. {
  238. expand : true,
  239. cwd : 'src/',
  240. src : [
  241. '**/*.js',
  242. 'images/*',
  243. 'fonts/*'
  244. ],
  245. dest : 'docs/build/uncompressed'
  246. },
  247. // copy everything but less for minified release
  248. {
  249. expand : true,
  250. cwd : 'src/',
  251. src : [
  252. '**/*.js',
  253. 'images/*',
  254. 'fonts/*'
  255. ],
  256. dest : 'docs/build/minified'
  257. },
  258. // copy assets only for packaged version
  259. {
  260. expand : true,
  261. cwd : 'src/',
  262. src : [
  263. 'images/*',
  264. 'fonts/*'
  265. ],
  266. dest : 'docs/build/packaged'
  267. }
  268. ]
  269. },
  270. srcToBuild: {
  271. files: [
  272. // exact copy for less
  273. {
  274. expand : true,
  275. cwd : 'src/',
  276. src : [
  277. '**/*'
  278. ],
  279. dest : 'build/less'
  280. },
  281. // copy everything but less files for uncompressed release
  282. {
  283. expand : true,
  284. cwd : 'src/',
  285. src : [
  286. '**/*.js',
  287. 'images/*',
  288. 'fonts/*'
  289. ],
  290. dest : 'build/uncompressed'
  291. },
  292. // copy everything but less for minified release
  293. {
  294. expand : true,
  295. cwd : 'src/',
  296. src : [
  297. '**/*.js',
  298. 'images/*',
  299. 'fonts/*'
  300. ],
  301. dest : 'build/minified'
  302. },
  303. // copy assets only for packaged version
  304. {
  305. expand : true,
  306. cwd : 'src/',
  307. src : [
  308. 'images/*',
  309. 'fonts/*'
  310. ],
  311. dest : 'build/packaged'
  312. }
  313. ]
  314. },
  315. // create new rtl assets
  316. buildToRTL: {
  317. files: [
  318. {
  319. expand : true,
  320. cwd : 'build/',
  321. src : [
  322. '**'
  323. ],
  324. dest : 'rtl'
  325. }
  326. ]
  327. },
  328. // make library available in docs
  329. buildToDocs: {
  330. files: [
  331. {
  332. expand : true,
  333. cwd : 'build/',
  334. src : [
  335. '**'
  336. ],
  337. dest : 'docs/build/'
  338. }
  339. ]
  340. },
  341. // copy spec files to docs
  342. specToDocs: {
  343. files: [
  344. {
  345. expand : true,
  346. cwd : 'spec',
  347. src : [
  348. '**'
  349. ],
  350. dest : 'docs/spec/'
  351. }
  352. ]
  353. },
  354. // copy spec files to docs
  355. examplesToDocs: {
  356. files: [
  357. {
  358. expand : true,
  359. cwd : 'build/examples',
  360. src : [
  361. '**'
  362. ],
  363. dest : 'docs/examples/'
  364. }
  365. ]
  366. }
  367. },
  368. compress: {
  369. options: {
  370. archive: 'docs/build/semantic.zip'
  371. },
  372. everything: {
  373. files: [
  374. {
  375. expand : true,
  376. cwd : 'build/',
  377. src : [
  378. '**'
  379. ]
  380. }
  381. ]
  382. }
  383. },
  384. concat: {
  385. options: {
  386. },
  387. concatenateCSS: {
  388. src: ['build/uncompressed/**/*.css'],
  389. dest: 'build/packaged/css/semantic.css'
  390. },
  391. concatenateJS: {
  392. src: ['build/uncompressed/**/*.js'],
  393. dest: 'build/packaged/javascript/semantic.js'
  394. },
  395. },
  396. cssmin: {
  397. // copy minified css to minified release
  398. minifyCSS: {
  399. expand : true,
  400. cwd : 'build/uncompressed',
  401. src : [
  402. '**/*.css'
  403. ],
  404. dest : 'build/minified',
  405. ext : '.min.css'
  406. },
  407. // add comment banner to css release
  408. addBanner: {
  409. options : {
  410. banner : '' +
  411. '/*\n' +
  412. '* # <%= package.title %>\n' +
  413. '* Version: <%= package.version %>\n' +
  414. '* http://github.com/jlukic/semantic-ui\n' +
  415. '*\n' +
  416. '*\n' +
  417. '* Copyright <%= grunt.template.today("yyyy") %> Contributors\n' +
  418. '* Released under the MIT license\n' +
  419. '* http://opensource.org/licenses/MIT\n' +
  420. '*\n' +
  421. '* Released: <%= grunt.template.today("mm/dd/yyyy") %>\n' +
  422. '*/\n'
  423. },
  424. files: {
  425. 'build/packaged/css/semantic.min.css': [
  426. 'build/uncompressed/**/*.css'
  427. ]
  428. }
  429. }
  430. },
  431. uglify: {
  432. minifyJS: {
  433. expand : true,
  434. cwd : 'build/uncompressed',
  435. src : [
  436. '**/*.js'
  437. ],
  438. dest : 'build/minified',
  439. ext : '.min.js',
  440. banner : '' +
  441. '/*' +
  442. '* # <%= package.title %>\n' +
  443. '* Version: <%= package.version %>\n' +
  444. '* http://github.com/jlukic/semantic-ui\n' +
  445. '*\n' +
  446. '*\n' +
  447. '* Copyright <%= grunt.template.today("yyyy") %> Contributors\n' +
  448. '* Released under the MIT license\n' +
  449. '* http://opensource.org/licenses/MIT\n' +
  450. '*\n' +
  451. '* Release Date: <%= grunt.template.today("mm/dd/yyyy") %>\n' +
  452. '*/\n'
  453. },
  454. buildReleaseJS: {
  455. options: {
  456. mangle : true,
  457. compress : true,
  458. banner : '' +
  459. '/*' +
  460. '* # <%= package.title %>\n' +
  461. '* Version: <%= package.version %>\n' +
  462. '* http://github.com/jlukic/semantic-ui\n' +
  463. '*\n' +
  464. '*\n' +
  465. '* Copyright <%= grunt.template.today("yyyy") %> Contributors\n' +
  466. '* Released under the MIT license\n' +
  467. '* http://opensource.org/licenses/MIT\n' +
  468. '*\n' +
  469. '* Release Date: <%= grunt.template.today("mm/dd/yyyy") %>\n' +
  470. '*/\n'
  471. },
  472. files: {
  473. 'build/packaged/javascript/semantic.min.js': [
  474. 'build/uncompressed/**/*.js'
  475. ]
  476. }
  477. }
  478. }
  479. };
  480. grunt.loadNpmTasks('grunt-contrib-clean');
  481. grunt.loadNpmTasks('grunt-contrib-compress');
  482. grunt.loadNpmTasks('grunt-contrib-concat');
  483. grunt.loadNpmTasks('grunt-contrib-copy');
  484. grunt.loadNpmTasks('grunt-contrib-cssmin');
  485. grunt.loadNpmTasks('grunt-contrib-less');
  486. grunt.loadNpmTasks('grunt-contrib-uglify');
  487. grunt.loadNpmTasks('grunt-contrib-watch');
  488. grunt.loadNpmTasks('grunt-docco-multi');
  489. grunt.loadNpmTasks('grunt-cssjanus');
  490. grunt.loadNpmTasks('grunt-clear');
  491. grunt.loadNpmTasks('grunt-karma');
  492. grunt.initConfig(config);
  493. grunt.registerTask('default', defaultTasks);
  494. grunt.registerTask('build', buildTasks);
  495. grunt.registerTask('spec', specTasks);
  496. grunt.registerTask('test', testTasks);
  497. // compiles only changed less files <https://npmjs.org/package/grunt-contrib-watch>
  498. grunt.event.on('watch', setWatchFiles);
  499. };