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.

604 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 assets only for minified version
  274. {
  275. expand : true,
  276. cwd : 'src/',
  277. src : [
  278. 'images/*',
  279. 'fonts/*'
  280. ],
  281. dest : 'docs/build/minified'
  282. },
  283. // copy assets only for packaged version
  284. {
  285. expand : true,
  286. cwd : 'src/',
  287. src : [
  288. 'images/*',
  289. 'fonts/*'
  290. ],
  291. dest : 'docs/build/packaged'
  292. }
  293. ]
  294. },
  295. srcToBuild: {
  296. files: [
  297. // exact copy for less
  298. {
  299. expand : true,
  300. cwd : 'src/',
  301. src : [
  302. '**/*'
  303. ],
  304. dest : 'build/less'
  305. },
  306. // copy everything but less files for uncompressed release
  307. {
  308. expand : true,
  309. cwd : 'src/',
  310. src : [
  311. '**/*.js',
  312. 'images/*',
  313. 'fonts/*'
  314. ],
  315. dest : 'build/uncompressed'
  316. },
  317. // copy assets only for minified version
  318. {
  319. expand : true,
  320. cwd : 'src/',
  321. src : [
  322. 'images/*',
  323. 'fonts/*'
  324. ],
  325. dest : 'build/minified'
  326. },
  327. // copy assets only for packaged version
  328. {
  329. expand : true,
  330. cwd : 'src/',
  331. src : [
  332. 'images/*',
  333. 'fonts/*'
  334. ],
  335. dest : 'build/packaged'
  336. }
  337. ]
  338. },
  339. // create new rtl assets
  340. buildToRTL: {
  341. files: [
  342. {
  343. expand : true,
  344. cwd : 'build/',
  345. src : [
  346. '**'
  347. ],
  348. dest : 'rtl'
  349. }
  350. ]
  351. },
  352. // make library available in docs
  353. buildToDocs: {
  354. files: [
  355. {
  356. expand : true,
  357. cwd : 'build/',
  358. src : [
  359. '**'
  360. ],
  361. dest : 'docs/build/'
  362. }
  363. ]
  364. },
  365. // copy spec files to docs
  366. specToDocs: {
  367. files: [
  368. {
  369. expand : true,
  370. cwd : 'spec',
  371. src : [
  372. '**'
  373. ],
  374. dest : 'docs/spec/'
  375. }
  376. ]
  377. },
  378. // copy spec files to docs
  379. examplesToDocs: {
  380. files: [
  381. {
  382. expand : true,
  383. cwd : 'build/examples',
  384. src : [
  385. '**'
  386. ],
  387. dest : 'docs/examples/'
  388. }
  389. ]
  390. }
  391. },
  392. compress: {
  393. options: {
  394. archive: 'docs/build/semantic.zip'
  395. },
  396. everything: {
  397. files: [
  398. {
  399. expand : true,
  400. cwd : 'build/',
  401. src : [
  402. '**'
  403. ]
  404. }
  405. ]
  406. }
  407. },
  408. concat: {
  409. options: {
  410. },
  411. concatenateCSS: {
  412. src: ['build/uncompressed/**/*.css'],
  413. dest: 'build/packaged/css/semantic.css'
  414. },
  415. concatenateJS: {
  416. src: ['build/uncompressed/**/*.js'],
  417. dest: 'build/packaged/javascript/semantic.js'
  418. },
  419. },
  420. cssmin: {
  421. // copy minified css to minified release
  422. minifyCSS: {
  423. expand : true,
  424. cwd : 'build/uncompressed',
  425. src : [
  426. '**/*.css'
  427. ],
  428. dest : 'build/minified',
  429. ext : '.min.css'
  430. },
  431. // add comment banner to css release
  432. addBanner: {
  433. options : {
  434. banner : '' +
  435. '/*\n' +
  436. '* # <%= package.title %>\n' +
  437. '* Version: <%= package.version %>\n' +
  438. '* http://github.com/jlukic/semantic-ui\n' +
  439. '*\n' +
  440. '*\n' +
  441. '* Copyright <%= grunt.template.today("yyyy") %> Contributors\n' +
  442. '* Released under the MIT license\n' +
  443. '* http://opensource.org/licenses/MIT\n' +
  444. '*\n' +
  445. '* Released: <%= grunt.template.today("mm/dd/yyyy") %>\n' +
  446. '*/\n'
  447. },
  448. files: {
  449. 'build/packaged/css/semantic.min.css': [
  450. 'build/uncompressed/**/*.css'
  451. ]
  452. }
  453. }
  454. },
  455. uglify: {
  456. minifyJS: {
  457. expand : true,
  458. cwd : 'build/uncompressed',
  459. src : [
  460. '**/*.js'
  461. ],
  462. dest : 'build/minified',
  463. ext : '.min.js',
  464. banner : '' +
  465. '/*' +
  466. '* # <%= package.title %>\n' +
  467. '* Version: <%= package.version %>\n' +
  468. '* http://github.com/jlukic/semantic-ui\n' +
  469. '*\n' +
  470. '*\n' +
  471. '* Copyright <%= grunt.template.today("yyyy") %> Contributors\n' +
  472. '* Released under the MIT license\n' +
  473. '* http://opensource.org/licenses/MIT\n' +
  474. '*\n' +
  475. '* Release Date: <%= grunt.template.today("mm/dd/yyyy") %>\n' +
  476. '*/\n'
  477. },
  478. buildReleaseJS: {
  479. options: {
  480. mangle : true,
  481. compress : true,
  482. banner : '' +
  483. '/*' +
  484. '* # <%= package.title %>\n' +
  485. '* Version: <%= package.version %>\n' +
  486. '* http://github.com/jlukic/semantic-ui\n' +
  487. '*\n' +
  488. '*\n' +
  489. '* Copyright <%= grunt.template.today("yyyy") %> Contributors\n' +
  490. '* Released under the MIT license\n' +
  491. '* http://opensource.org/licenses/MIT\n' +
  492. '*\n' +
  493. '* Release Date: <%= grunt.template.today("mm/dd/yyyy") %>\n' +
  494. '*/\n'
  495. },
  496. files: {
  497. 'build/packaged/javascript/semantic.min.js': [
  498. 'build/uncompressed/**/*.js'
  499. ]
  500. }
  501. }
  502. }
  503. };
  504. grunt.loadNpmTasks('grunt-contrib-clean');
  505. grunt.loadNpmTasks('grunt-contrib-compress');
  506. grunt.loadNpmTasks('grunt-contrib-concat');
  507. grunt.loadNpmTasks('grunt-contrib-copy');
  508. grunt.loadNpmTasks('grunt-contrib-cssmin');
  509. grunt.loadNpmTasks('grunt-contrib-less');
  510. grunt.loadNpmTasks('grunt-contrib-uglify');
  511. grunt.loadNpmTasks('grunt-contrib-watch');
  512. grunt.loadNpmTasks('grunt-autoprefixer');
  513. grunt.loadNpmTasks('grunt-docco-multi');
  514. grunt.loadNpmTasks('grunt-cssjanus');
  515. grunt.loadNpmTasks('grunt-clear');
  516. grunt.loadNpmTasks('grunt-karma');
  517. grunt.initConfig(config);
  518. grunt.registerTask('default', defaultTasks);
  519. grunt.registerTask('build', buildTasks);
  520. grunt.registerTask('spec', specTasks);
  521. grunt.registerTask('test', testTasks);
  522. // compiles only changed less files <https://npmjs.org/package/grunt-contrib-watch>
  523. grunt.event.on('watch', setWatchFiles);
  524. };