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.

634 lines
15 KiB

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