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.

571 lines
13 KiB

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