Gruntfile.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*!
  2. * Bootstrap's Gruntfile
  3. * http://getbootstrap.com
  4. * Copyright 2013-2014 Twitter, Inc.
  5. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
  6. */
  7. module.exports = function (grunt) {
  8. 'use strict';
  9. // Force use of Unix newlines
  10. grunt.util.linefeed = '\n';
  11. RegExp.quote = function (string) {
  12. return string.replace(/[-\\^$*+?.()|[\]{}]/g, '\\$&');
  13. };
  14. var fs = require('fs');
  15. var path = require('path');
  16. var npmShrinkwrap = require('npm-shrinkwrap');
  17. var generateGlyphiconsData = require('./grunt/bs-glyphicons-data-generator.js');
  18. var BsLessdocParser = require('./grunt/bs-lessdoc-parser.js');
  19. var generateRawFiles = require('./grunt/bs-raw-files-generator.js');
  20. // Project configuration.
  21. grunt.initConfig({
  22. // Metadata.
  23. pkg: grunt.file.readJSON('package.json'),
  24. banner: '/*!\n' +
  25. ' * Bootstrap v<%= pkg.version %> (<%= pkg.homepage %>)\n' +
  26. ' * Copyright 2011-<%= grunt.template.today("yyyy") %> <%= pkg.author %>\n' +
  27. ' * Licensed under <%= pkg.license.type %> (<%= pkg.license.url %>)\n' +
  28. ' */\n',
  29. // NOTE: This jqueryCheck code is duplicated in customizer.js; if making changes here, be sure to update the other copy too.
  30. jqueryCheck: 'if (typeof jQuery === \'undefined\') { throw new Error(\'Bootstrap\\\'s JavaScript requires jQuery\') }\n\n',
  31. // Task configuration.
  32. clean: {
  33. dist: ['dist', 'docs/dist']
  34. },
  35. jshint: {
  36. options: {
  37. jshintrc: 'js/.jshintrc'
  38. },
  39. grunt: {
  40. options: {
  41. jshintrc: 'grunt/.jshintrc'
  42. },
  43. src: ['Gruntfile.js', 'grunt/*.js']
  44. },
  45. src: {
  46. src: 'js/*.js'
  47. },
  48. test: {
  49. options: {
  50. jshintrc: 'js/tests/unit/.jshintrc'
  51. },
  52. src: 'js/tests/unit/*.js'
  53. },
  54. assets: {
  55. src: ['docs/assets/js/_src/*.js', 'docs/assets/js/*.js', '!docs/assets/js/*.min.js']
  56. }
  57. },
  58. jscs: {
  59. options: {
  60. config: 'js/.jscsrc'
  61. },
  62. grunt: {
  63. src: '<%= jshint.grunt.src %>'
  64. },
  65. src: {
  66. src: '<%= jshint.src.src %>'
  67. },
  68. test: {
  69. src: '<%= jshint.test.src %>'
  70. },
  71. assets: {
  72. options: {
  73. requireCamelCaseOrUpperCaseIdentifiers: null
  74. },
  75. src: '<%= jshint.assets.src %>'
  76. }
  77. },
  78. concat: {
  79. options: {
  80. banner: '<%= banner %>\n<%= jqueryCheck %>',
  81. stripBanners: false
  82. },
  83. bootstrap: {
  84. src: [
  85. 'js/transition.js',
  86. 'js/alert.js',
  87. 'js/button.js',
  88. 'js/carousel.js',
  89. 'js/collapse.js',
  90. 'js/dropdown.js',
  91. 'js/modal.js',
  92. 'js/tooltip.js',
  93. 'js/popover.js',
  94. 'js/scrollspy.js',
  95. 'js/tab.js',
  96. 'js/affix.js'
  97. ],
  98. dest: 'dist/js/<%= pkg.name %>.js'
  99. }
  100. },
  101. uglify: {
  102. options: {
  103. preserveComments: 'some'
  104. },
  105. bootstrap: {
  106. src: '<%= concat.bootstrap.dest %>',
  107. dest: 'dist/js/<%= pkg.name %>.min.js'
  108. },
  109. customize: {
  110. src: [
  111. 'docs/assets/js/_vendor/less.min.js',
  112. 'docs/assets/js/_vendor/jszip.min.js',
  113. 'docs/assets/js/_vendor/uglify.min.js',
  114. 'docs/assets/js/_vendor/blob.js',
  115. 'docs/assets/js/_vendor/filesaver.js',
  116. 'docs/assets/js/raw-files.min.js',
  117. 'docs/assets/js/_src/customizer.js'
  118. ],
  119. dest: 'docs/assets/js/customize.min.js'
  120. },
  121. docsJs: {
  122. src: [
  123. 'docs/assets/js/_vendor/holder.js',
  124. 'docs/assets/js/_vendor/ZeroClipboard.min.js',
  125. 'docs/assets/js/_src/application.js'
  126. ],
  127. dest: 'docs/assets/js/docs.min.js'
  128. }
  129. },
  130. qunit: {
  131. options: {
  132. inject: 'js/tests/unit/phantom.js'
  133. },
  134. files: 'js/tests/index.html'
  135. },
  136. less: {
  137. compileCore: {
  138. options: {
  139. strictMath: true,
  140. sourceMap: true,
  141. outputSourceFiles: true,
  142. sourceMapURL: '<%= pkg.name %>.css.map',
  143. sourceMapFilename: 'dist/css/<%= pkg.name %>.css.map'
  144. },
  145. files: {
  146. 'dist/css/<%= pkg.name %>.css': 'less/bootstrap.less'
  147. }
  148. },
  149. compileTheme: {
  150. options: {
  151. strictMath: true,
  152. sourceMap: true,
  153. outputSourceFiles: true,
  154. sourceMapURL: '<%= pkg.name %>-theme.css.map',
  155. sourceMapFilename: 'dist/css/<%= pkg.name %>-theme.css.map'
  156. },
  157. files: {
  158. 'dist/css/<%= pkg.name %>-theme.css': 'less/theme.less'
  159. }
  160. }
  161. },
  162. autoprefixer: {
  163. options: {
  164. browsers: [
  165. 'Android 2.3',
  166. 'Android >= 4',
  167. 'Chrome >= 20',
  168. 'Firefox >= 24', // Firefox 24 is the latest ESR
  169. 'Explorer >= 8',
  170. 'iOS >= 6',
  171. 'Opera >= 12',
  172. 'Safari >= 6'
  173. ]
  174. },
  175. core: {
  176. options: {
  177. map: true
  178. },
  179. src: 'dist/css/<%= pkg.name %>.css'
  180. },
  181. theme: {
  182. options: {
  183. map: true
  184. },
  185. src: 'dist/css/<%= pkg.name %>-theme.css'
  186. },
  187. docs: {
  188. src: 'docs/assets/css/_src/docs.css'
  189. },
  190. examples: {
  191. expand: true,
  192. cwd: 'docs/examples/',
  193. src: ['**/*.css'],
  194. dest: 'docs/examples/'
  195. }
  196. },
  197. csslint: {
  198. options: {
  199. csslintrc: 'less/.csslintrc'
  200. },
  201. src: [
  202. 'dist/css/bootstrap.css',
  203. 'dist/css/bootstrap-theme.css'
  204. ],
  205. examples: [
  206. 'docs/examples/**/*.css'
  207. ],
  208. docs: {
  209. options: {
  210. ids: false,
  211. 'overqualified-elements': false
  212. },
  213. src: 'docs/assets/css/_src/docs.css'
  214. }
  215. },
  216. cssmin: {
  217. options: {
  218. compatibility: 'ie8',
  219. keepSpecialComments: '*',
  220. noAdvanced: true
  221. },
  222. core: {
  223. files: {
  224. 'dist/css/<%= pkg.name %>.min.css': 'dist/css/<%= pkg.name %>.css',
  225. 'dist/css/<%= pkg.name %>-theme.min.css': 'dist/css/<%= pkg.name %>-theme.css'
  226. }
  227. },
  228. docs: {
  229. src: [
  230. 'docs/assets/css/_src/docs.css',
  231. 'docs/assets/css/_src/pygments-manni.css'
  232. ],
  233. dest: 'docs/assets/css/docs.min.css'
  234. }
  235. },
  236. usebanner: {
  237. options: {
  238. position: 'top',
  239. banner: '<%= banner %>'
  240. },
  241. files: {
  242. src: 'dist/css/*.css'
  243. }
  244. },
  245. csscomb: {
  246. options: {
  247. config: 'less/.csscomb.json'
  248. },
  249. dist: {
  250. expand: true,
  251. cwd: 'dist/css/',
  252. src: ['*.css', '!*.min.css'],
  253. dest: 'dist/css/'
  254. },
  255. examples: {
  256. expand: true,
  257. cwd: 'docs/examples/',
  258. src: '**/*.css',
  259. dest: 'docs/examples/'
  260. },
  261. docs: {
  262. files: {
  263. 'docs/assets/css/_src/docs.css': 'docs/assets/css/_src/docs.css'
  264. }
  265. }
  266. },
  267. copy: {
  268. fonts: {
  269. expand: true,
  270. src: 'fonts/*',
  271. dest: 'dist/'
  272. },
  273. docs: {
  274. expand: true,
  275. cwd: './dist',
  276. src: [
  277. '{css,js}/*.min.*',
  278. 'css/*.map',
  279. 'fonts/*'
  280. ],
  281. dest: 'docs/dist'
  282. }
  283. },
  284. connect: {
  285. server: {
  286. options: {
  287. port: 3000,
  288. base: '.'
  289. }
  290. }
  291. },
  292. jekyll: {
  293. docs: {}
  294. },
  295. jade: {
  296. compile: {
  297. options: {
  298. pretty: true,
  299. data: function () {
  300. var filePath = path.join(__dirname, 'less/variables.less');
  301. var fileContent = fs.readFileSync(filePath, { encoding: 'utf8' });
  302. var parser = new BsLessdocParser(fileContent);
  303. return { sections: parser.parseFile() };
  304. }
  305. },
  306. files: {
  307. 'docs/_includes/customizer-variables.html': 'docs/_jade/customizer-variables.jade',
  308. 'docs/_includes/nav/customize.html': 'docs/_jade/customizer-nav.jade'
  309. }
  310. }
  311. },
  312. validation: {
  313. options: {
  314. charset: 'utf-8',
  315. doctype: 'HTML5',
  316. failHard: true,
  317. reset: true,
  318. relaxerror: [
  319. 'Bad value X-UA-Compatible for attribute http-equiv on element meta.',
  320. 'Element img is missing required attribute src.'
  321. ]
  322. },
  323. files: {
  324. src: '_gh_pages/**/*.html'
  325. }
  326. },
  327. watch: {
  328. src: {
  329. files: '<%= jshint.src.src %>',
  330. tasks: ['jshint:src', 'qunit']
  331. },
  332. test: {
  333. files: '<%= jshint.test.src %>',
  334. tasks: ['jshint:test', 'qunit']
  335. },
  336. less: {
  337. files: 'less/*.less',
  338. tasks: 'less'
  339. }
  340. },
  341. sed: {
  342. versionNumber: {
  343. pattern: (function () {
  344. var old = grunt.option('oldver');
  345. return old ? RegExp.quote(old) : old;
  346. })(),
  347. replacement: grunt.option('newver'),
  348. recursive: true
  349. }
  350. },
  351. 'saucelabs-qunit': {
  352. all: {
  353. options: {
  354. build: process.env.TRAVIS_JOB_ID,
  355. concurrency: 10,
  356. maxRetries: 3,
  357. urls: ['http://127.0.0.1:3000/js/tests/index.html'],
  358. browsers: grunt.file.readYAML('grunt/sauce_browsers.yml')
  359. }
  360. }
  361. },
  362. exec: {
  363. npmUpdate: {
  364. command: 'npm update'
  365. }
  366. }
  367. });
  368. // These plugins provide necessary tasks.
  369. require('load-grunt-tasks')(grunt, { scope: 'devDependencies' });
  370. require('time-grunt')(grunt);
  371. // Docs HTML validation task
  372. grunt.registerTask('validate-html', ['jekyll', 'validation']);
  373. var runSubset = function (subset) {
  374. return !process.env.TWBS_TEST || process.env.TWBS_TEST === subset;
  375. };
  376. var isUndefOrNonZero = function (val) {
  377. return val === undefined || val !== '0';
  378. };
  379. // Test task.
  380. var testSubtasks = [];
  381. // Skip core tests if running a different subset of the test suite
  382. if (runSubset('core')) {
  383. testSubtasks = testSubtasks.concat(['dist-css', 'csslint', 'jshint', 'jscs', 'qunit', 'build-customizer-html']);
  384. }
  385. // Skip HTML validation if running a different subset of the test suite
  386. if (runSubset('validate-html') &&
  387. // Skip HTML5 validator on Travis when [skip validator] is in the commit message
  388. isUndefOrNonZero(process.env.TWBS_DO_VALIDATOR)) {
  389. testSubtasks.push('validate-html');
  390. }
  391. // Only run Sauce Labs tests if there's a Sauce access key
  392. if (typeof process.env.SAUCE_ACCESS_KEY !== 'undefined' &&
  393. // Skip Sauce if running a different subset of the test suite
  394. runSubset('sauce-js-unit') &&
  395. // Skip Sauce on Travis when [skip sauce] is in the commit message
  396. isUndefOrNonZero(process.env.TWBS_DO_SAUCE)) {
  397. testSubtasks.push('connect');
  398. testSubtasks.push('saucelabs-qunit');
  399. }
  400. grunt.registerTask('test', testSubtasks);
  401. // JS distribution task.
  402. grunt.registerTask('dist-js', ['concat', 'uglify']);
  403. // CSS distribution task.
  404. grunt.registerTask('less-compile', ['less:compileCore', 'less:compileTheme']);
  405. grunt.registerTask('dist-css', ['less-compile', 'autoprefixer', 'usebanner', 'csscomb', 'cssmin']);
  406. // Docs distribution task.
  407. grunt.registerTask('dist-docs', 'copy:docs');
  408. // Full distribution task.
  409. grunt.registerTask('dist', ['clean', 'dist-css', 'copy:fonts', 'dist-js', 'dist-docs']);
  410. // Default task.
  411. grunt.registerTask('default', ['test', 'dist', 'build-glyphicons-data', 'build-customizer']);
  412. // Version numbering task.
  413. // grunt change-version-number --oldver=A.B.C --newver=X.Y.Z
  414. // This can be overzealous, so its changes should always be manually reviewed!
  415. grunt.registerTask('change-version-number', 'sed');
  416. grunt.registerTask('build-glyphicons-data', function () { generateGlyphiconsData.call(this, grunt); });
  417. // task for building customizer
  418. grunt.registerTask('build-customizer', ['build-customizer-html', 'build-raw-files']);
  419. grunt.registerTask('build-customizer-html', 'jade');
  420. grunt.registerTask('build-raw-files', 'Add scripts/less files to customizer.', function () {
  421. var banner = grunt.template.process('<%= banner %>');
  422. generateRawFiles(grunt, banner);
  423. });
  424. // Task for updating the cached npm packages used by the Travis build (which are controlled by test-infra/npm-shrinkwrap.json).
  425. // This task should be run and the updated file should be committed whenever Bootstrap's dependencies change.
  426. grunt.registerTask('update-shrinkwrap', ['exec:npmUpdate', '_update-shrinkwrap']);
  427. grunt.registerTask('_update-shrinkwrap', function () {
  428. var done = this.async();
  429. npmShrinkwrap({ dev: true, dirname: __dirname }, function (err) {
  430. if (err) {
  431. grunt.fail.warn(err)
  432. }
  433. var dest = 'test-infra/npm-shrinkwrap.json';
  434. fs.renameSync('npm-shrinkwrap.json', dest);
  435. grunt.log.writeln('File ' + dest.cyan + ' updated.');
  436. done();
  437. });
  438. });
  439. };