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.

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