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.

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