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. 'opera 12.1',
  164. 'safari 6',
  165. 'ie 9',
  166. 'bb 10',
  167. 'android 4'
  168. ]
  169. },
  170. prefixBuild: {
  171. src : 'build/**/*.css'
  172. },
  173. prefixDocs: {
  174. src : 'docs/build/**/*.css'
  175. }
  176. },
  177. clean: {
  178. options: {
  179. force: true
  180. },
  181. build : [
  182. 'build/less',
  183. 'build/minified',
  184. 'build/packaged',
  185. 'build/uncompressed'
  186. ],
  187. release : [
  188. 'docs/build',
  189. 'docs',
  190. 'rtl'
  191. ]
  192. },
  193. docco: {
  194. generate: {
  195. options: {
  196. css : 'spec/assets/docco.css',
  197. output : 'spec/docs/'
  198. },
  199. files: [
  200. {
  201. expand : true,
  202. cwd : 'spec/',
  203. src : [
  204. '**.commented.js'
  205. ]
  206. }
  207. ]
  208. }
  209. },
  210. cssjanus: {
  211. rtl: {
  212. expand : true,
  213. cwd : 'build/',
  214. src : [
  215. '**/*.less',
  216. '**/*.css',
  217. ],
  218. dest : 'rtl'
  219. },
  220. },
  221. less: {
  222. options: {
  223. paths : ['src'],
  224. compress : false,
  225. optimization : 2
  226. },
  227. // optimized for watch, src is built on watch task using callbacks
  228. buildDocsCSS: {
  229. src : 'src',
  230. dest : 'docs/build/uncompressed/',
  231. rename : preserveFileExtensions
  232. },
  233. buildTestCSS: {
  234. expand : true,
  235. cwd : 'src',
  236. src : [
  237. '**/*.less'
  238. ],
  239. dest : 'docs/build/uncompressed/',
  240. rename: preserveFileExtensions
  241. },
  242. buildCSS: {
  243. expand : true,
  244. cwd : 'src',
  245. src : [
  246. '**/*.less'
  247. ],
  248. dest : 'build/uncompressed/',
  249. rename: preserveFileExtensions
  250. }
  251. },
  252. copy: {
  253. srcToDocs: {
  254. files: [
  255. // exact copy for less
  256. {
  257. expand : true,
  258. cwd : 'src/**/*.less',
  259. src : [
  260. '**/*'
  261. ],
  262. dest : 'docs/build/less'
  263. },
  264. // copy everything but less files for uncompressed release
  265. {
  266. expand : true,
  267. cwd : 'src/',
  268. src : [
  269. '**/*.js',
  270. 'images/*',
  271. 'fonts/*'
  272. ],
  273. dest : 'docs/build/uncompressed'
  274. },
  275. // copy assets only for minified version
  276. {
  277. expand : true,
  278. cwd : 'src/',
  279. src : [
  280. 'images/*',
  281. 'fonts/*'
  282. ],
  283. dest : 'docs/build/minified'
  284. },
  285. // copy assets only for packaged version
  286. {
  287. expand : true,
  288. cwd : 'src/',
  289. src : [
  290. 'images/*',
  291. 'fonts/*'
  292. ],
  293. dest : 'docs/build/packaged'
  294. }
  295. ]
  296. },
  297. srcToBuild: {
  298. files: [
  299. // exact copy for less
  300. {
  301. expand : true,
  302. cwd : 'src/',
  303. src : [
  304. '**/*'
  305. ],
  306. dest : 'build/less'
  307. },
  308. // copy everything but less files for uncompressed release
  309. {
  310. expand : true,
  311. cwd : 'src/',
  312. src : [
  313. '**/*.js',
  314. 'images/*',
  315. 'fonts/*'
  316. ],
  317. dest : 'build/uncompressed'
  318. },
  319. // copy assets only for minified version
  320. {
  321. expand : true,
  322. cwd : 'src/',
  323. src : [
  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. };