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.

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