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.

600 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: ['last 2 version', 'ie 9']
  161. },
  162. prefixBuild: {
  163. src : 'build/**/*.css'
  164. },
  165. prefixDocs: {
  166. src : 'docs/build/**/*.css'
  167. }
  168. },
  169. clean: {
  170. options: {
  171. force: true
  172. },
  173. build : [
  174. 'build/less',
  175. 'build/minified',
  176. 'build/packaged',
  177. 'build/uncompressed'
  178. ],
  179. release : [
  180. 'docs/build',
  181. 'docs',
  182. 'rtl'
  183. ]
  184. },
  185. docco: {
  186. generate: {
  187. options: {
  188. css : 'spec/assets/docco.css',
  189. output : 'spec/docs/'
  190. },
  191. files: [
  192. {
  193. expand : true,
  194. cwd : 'spec/',
  195. src : [
  196. '**.commented.js'
  197. ]
  198. }
  199. ]
  200. }
  201. },
  202. cssjanus: {
  203. rtl: {
  204. expand : true,
  205. cwd : 'build/',
  206. src : [
  207. '**/*.less',
  208. '**/*.css',
  209. ],
  210. dest : 'rtl'
  211. },
  212. },
  213. less: {
  214. options: {
  215. paths : ['src'],
  216. compress : false,
  217. optimization : 2
  218. },
  219. // optimized for watch, src is built on watch task using callbacks
  220. buildDocsCSS: {
  221. src : 'src',
  222. dest : 'docs/build/uncompressed/',
  223. rename : preserveFileExtensions
  224. },
  225. buildTestCSS: {
  226. expand : true,
  227. cwd : 'src',
  228. src : [
  229. '**/*.less'
  230. ],
  231. dest : 'docs/build/uncompressed/',
  232. rename: preserveFileExtensions
  233. },
  234. buildCSS: {
  235. expand : true,
  236. cwd : 'src',
  237. src : [
  238. '**/*.less'
  239. ],
  240. dest : 'build/uncompressed/',
  241. rename: preserveFileExtensions
  242. }
  243. },
  244. copy: {
  245. srcToDocs: {
  246. files: [
  247. // exact copy for less
  248. {
  249. expand : true,
  250. cwd : 'src/**/*.less',
  251. src : [
  252. '**/*'
  253. ],
  254. dest : 'docs/build/less'
  255. },
  256. // copy everything but less files for uncompressed release
  257. {
  258. expand : true,
  259. cwd : 'src/',
  260. src : [
  261. '**/*.js',
  262. 'images/*',
  263. 'fonts/*'
  264. ],
  265. dest : 'docs/build/uncompressed'
  266. },
  267. // copy everything but less for minified release
  268. {
  269. expand : true,
  270. cwd : 'src/',
  271. src : [
  272. '**/*.js',
  273. 'images/*',
  274. 'fonts/*'
  275. ],
  276. dest : 'docs/build/minified'
  277. },
  278. // copy assets only for packaged version
  279. {
  280. expand : true,
  281. cwd : 'src/',
  282. src : [
  283. 'images/*',
  284. 'fonts/*'
  285. ],
  286. dest : 'docs/build/packaged'
  287. }
  288. ]
  289. },
  290. srcToBuild: {
  291. files: [
  292. // exact copy for less
  293. {
  294. expand : true,
  295. cwd : 'src/',
  296. src : [
  297. '**/*'
  298. ],
  299. dest : 'build/less'
  300. },
  301. // copy everything but less files for uncompressed release
  302. {
  303. expand : true,
  304. cwd : 'src/',
  305. src : [
  306. '**/*.js',
  307. 'images/*',
  308. 'fonts/*'
  309. ],
  310. dest : 'build/uncompressed'
  311. },
  312. // copy everything but less for minified release
  313. {
  314. expand : true,
  315. cwd : 'src/',
  316. src : [
  317. '**/*.js',
  318. 'images/*',
  319. 'fonts/*'
  320. ],
  321. dest : 'build/minified'
  322. },
  323. // copy assets only for packaged version
  324. {
  325. expand : true,
  326. cwd : 'src/',
  327. src : [
  328. 'images/*',
  329. 'fonts/*'
  330. ],
  331. dest : 'build/packaged'
  332. }
  333. ]
  334. },
  335. // create new rtl assets
  336. buildToRTL: {
  337. files: [
  338. {
  339. expand : true,
  340. cwd : 'build/',
  341. src : [
  342. '**'
  343. ],
  344. dest : 'rtl'
  345. }
  346. ]
  347. },
  348. // make library available in docs
  349. buildToDocs: {
  350. files: [
  351. {
  352. expand : true,
  353. cwd : 'build/',
  354. src : [
  355. '**'
  356. ],
  357. dest : 'docs/build/'
  358. }
  359. ]
  360. },
  361. // copy spec files to docs
  362. specToDocs: {
  363. files: [
  364. {
  365. expand : true,
  366. cwd : 'spec',
  367. src : [
  368. '**'
  369. ],
  370. dest : 'docs/spec/'
  371. }
  372. ]
  373. },
  374. // copy spec files to docs
  375. examplesToDocs: {
  376. files: [
  377. {
  378. expand : true,
  379. cwd : 'build/examples',
  380. src : [
  381. '**'
  382. ],
  383. dest : 'docs/examples/'
  384. }
  385. ]
  386. }
  387. },
  388. compress: {
  389. options: {
  390. archive: 'docs/build/semantic.zip'
  391. },
  392. everything: {
  393. files: [
  394. {
  395. expand : true,
  396. cwd : 'build/',
  397. src : [
  398. '**'
  399. ]
  400. }
  401. ]
  402. }
  403. },
  404. concat: {
  405. options: {
  406. },
  407. concatenateCSS: {
  408. src: ['build/uncompressed/**/*.css'],
  409. dest: 'build/packaged/css/semantic.css'
  410. },
  411. concatenateJS: {
  412. src: ['build/uncompressed/**/*.js'],
  413. dest: 'build/packaged/javascript/semantic.js'
  414. },
  415. },
  416. cssmin: {
  417. // copy minified css to minified release
  418. minifyCSS: {
  419. expand : true,
  420. cwd : 'build/uncompressed',
  421. src : [
  422. '**/*.css'
  423. ],
  424. dest : 'build/minified',
  425. ext : '.min.css'
  426. },
  427. // add comment banner to css release
  428. addBanner: {
  429. options : {
  430. banner : '' +
  431. '/*\n' +
  432. '* # <%= package.title %>\n' +
  433. '* Version: <%= package.version %>\n' +
  434. '* http://github.com/jlukic/semantic-ui\n' +
  435. '*\n' +
  436. '*\n' +
  437. '* Copyright <%= grunt.template.today("yyyy") %> Contributors\n' +
  438. '* Released under the MIT license\n' +
  439. '* http://opensource.org/licenses/MIT\n' +
  440. '*\n' +
  441. '* Released: <%= grunt.template.today("mm/dd/yyyy") %>\n' +
  442. '*/\n'
  443. },
  444. files: {
  445. 'build/packaged/css/semantic.min.css': [
  446. 'build/uncompressed/**/*.css'
  447. ]
  448. }
  449. }
  450. },
  451. uglify: {
  452. minifyJS: {
  453. expand : true,
  454. cwd : 'build/uncompressed',
  455. src : [
  456. '**/*.js'
  457. ],
  458. dest : 'build/minified',
  459. ext : '.min.js',
  460. banner : '' +
  461. '/*' +
  462. '* # <%= package.title %>\n' +
  463. '* Version: <%= package.version %>\n' +
  464. '* http://github.com/jlukic/semantic-ui\n' +
  465. '*\n' +
  466. '*\n' +
  467. '* Copyright <%= grunt.template.today("yyyy") %> Contributors\n' +
  468. '* Released under the MIT license\n' +
  469. '* http://opensource.org/licenses/MIT\n' +
  470. '*\n' +
  471. '* Release Date: <%= grunt.template.today("mm/dd/yyyy") %>\n' +
  472. '*/\n'
  473. },
  474. buildReleaseJS: {
  475. options: {
  476. mangle : true,
  477. compress : true,
  478. banner : '' +
  479. '/*' +
  480. '* # <%= package.title %>\n' +
  481. '* Version: <%= package.version %>\n' +
  482. '* http://github.com/jlukic/semantic-ui\n' +
  483. '*\n' +
  484. '*\n' +
  485. '* Copyright <%= grunt.template.today("yyyy") %> Contributors\n' +
  486. '* Released under the MIT license\n' +
  487. '* http://opensource.org/licenses/MIT\n' +
  488. '*\n' +
  489. '* Release Date: <%= grunt.template.today("mm/dd/yyyy") %>\n' +
  490. '*/\n'
  491. },
  492. files: {
  493. 'build/packaged/javascript/semantic.min.js': [
  494. 'build/uncompressed/**/*.js'
  495. ]
  496. }
  497. }
  498. }
  499. };
  500. grunt.loadNpmTasks('grunt-contrib-clean');
  501. grunt.loadNpmTasks('grunt-contrib-compress');
  502. grunt.loadNpmTasks('grunt-contrib-concat');
  503. grunt.loadNpmTasks('grunt-contrib-copy');
  504. grunt.loadNpmTasks('grunt-contrib-cssmin');
  505. grunt.loadNpmTasks('grunt-contrib-less');
  506. grunt.loadNpmTasks('grunt-contrib-uglify');
  507. grunt.loadNpmTasks('grunt-contrib-watch');
  508. grunt.loadNpmTasks('grunt-autoprefixer');
  509. grunt.loadNpmTasks('grunt-docco-multi');
  510. grunt.loadNpmTasks('grunt-cssjanus');
  511. grunt.loadNpmTasks('grunt-clear');
  512. grunt.loadNpmTasks('grunt-karma');
  513. grunt.initConfig(config);
  514. grunt.registerTask('default', defaultTasks);
  515. grunt.registerTask('build', buildTasks);
  516. grunt.registerTask('spec', specTasks);
  517. grunt.registerTask('test', testTasks);
  518. // compiles only changed less files <https://npmjs.org/package/grunt-contrib-watch>
  519. grunt.event.on('watch', setWatchFiles);
  520. };