Gruntfile.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /*!
  2. * Bootstrap's Gruntfile
  3. * http://getbootstrap.com
  4. * Copyright 2013-2015 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 getLessVarsData = function () {
  20. var filePath = path.join(__dirname, 'less/variables.less');
  21. var fileContent = fs.readFileSync(filePath, { encoding: 'utf8' });
  22. var parser = new BsLessdocParser(fileContent);
  23. return { sections: parser.parseFile() };
  24. };
  25. var generateRawFiles = require('./grunt/bs-raw-files-generator.js');
  26. var generateCommonJSModule = require('./grunt/bs-commonjs-generator.js');
  27. var configBridge = grunt.file.readJSON('./grunt/configBridge.json', { encoding: 'utf8' });
  28. Object.keys(configBridge.paths).forEach(function (key) {
  29. configBridge.paths[key].forEach(function (val, i, arr) {
  30. arr[i] = path.join('./docs/assets', val);
  31. });
  32. });
  33. // Project configuration.
  34. grunt.initConfig({
  35. // Metadata.
  36. pkg: grunt.file.readJSON('package.json'),
  37. banner: '/*!\n' +
  38. ' * Bootstrap v<%= pkg.version %> (<%= pkg.homepage %>)\n' +
  39. ' * Copyright 2011-<%= grunt.template.today("yyyy") %> <%= pkg.author %>\n' +
  40. ' * Licensed under the <%= pkg.license %> license\n' +
  41. ' */\n',
  42. jqueryCheck: configBridge.config.jqueryCheck.join('\n'),
  43. jqueryVersionCheck: configBridge.config.jqueryVersionCheck.join('\n'),
  44. // Task configuration.
  45. clean: {
  46. dist: 'dist',
  47. docs: 'docs/dist'
  48. },
  49. jshint: {
  50. options: {
  51. jshintrc: 'js/.jshintrc'
  52. },
  53. grunt: {
  54. options: {
  55. jshintrc: 'grunt/.jshintrc'
  56. },
  57. src: ['Gruntfile.js', 'package.js', 'grunt/*.js']
  58. },
  59. core: {
  60. src: 'js/*.js'
  61. },
  62. test: {
  63. options: {
  64. jshintrc: 'js/tests/unit/.jshintrc'
  65. },
  66. src: 'js/tests/unit/*.js'
  67. },
  68. assets: {
  69. src: ['docs/assets/js/src/*.js', 'docs/assets/js/*.js', '!docs/assets/js/*.min.js']
  70. }
  71. },
  72. jscs: {
  73. options: {
  74. config: 'js/.jscsrc'
  75. },
  76. grunt: {
  77. src: '<%= jshint.grunt.src %>'
  78. },
  79. core: {
  80. src: '<%= jshint.core.src %>'
  81. },
  82. test: {
  83. src: '<%= jshint.test.src %>'
  84. },
  85. assets: {
  86. options: {
  87. requireCamelCaseOrUpperCaseIdentifiers: null
  88. },
  89. src: '<%= jshint.assets.src %>'
  90. }
  91. },
  92. concat: {
  93. options: {
  94. banner: '<%= banner %>\n<%= jqueryCheck %>\n<%= jqueryVersionCheck %>',
  95. stripBanners: false
  96. },
  97. bootstrap: {
  98. src: [
  99. 'js/transition.js',
  100. 'js/alert.js',
  101. 'js/button.js',
  102. 'js/carousel.js',
  103. 'js/collapse.js',
  104. 'js/dropdown.js',
  105. 'js/modal.js',
  106. 'js/tooltip.js',
  107. 'js/popover.js',
  108. 'js/scrollspy.js',
  109. 'js/tab.js',
  110. 'js/affix.js'
  111. ],
  112. dest: 'dist/js/<%= pkg.name %>.js'
  113. }
  114. },
  115. uglify: {
  116. options: {
  117. compress: {
  118. warnings: false
  119. },
  120. mangle: true,
  121. preserveComments: 'some'
  122. },
  123. core: {
  124. src: '<%= concat.bootstrap.dest %>',
  125. dest: 'dist/js/<%= pkg.name %>.min.js'
  126. },
  127. customize: {
  128. src: configBridge.paths.customizerJs,
  129. dest: 'docs/assets/js/customize.min.js'
  130. },
  131. docsJs: {
  132. src: configBridge.paths.docsJs,
  133. dest: 'docs/assets/js/docs.min.js'
  134. }
  135. },
  136. qunit: {
  137. options: {
  138. inject: 'js/tests/unit/phantom.js'
  139. },
  140. files: 'js/tests/index.html'
  141. },
  142. less: {
  143. compileCore: {
  144. options: {
  145. strictMath: true,
  146. sourceMap: true,
  147. outputSourceFiles: true,
  148. sourceMapURL: '<%= pkg.name %>.css.map',
  149. sourceMapFilename: 'dist/css/<%= pkg.name %>.css.map'
  150. },
  151. src: 'less/bootstrap.less',
  152. dest: 'dist/css/<%= pkg.name %>.css'
  153. },
  154. compileTheme: {
  155. options: {
  156. strictMath: true,
  157. sourceMap: true,
  158. outputSourceFiles: true,
  159. sourceMapURL: '<%= pkg.name %>-theme.css.map',
  160. sourceMapFilename: 'dist/css/<%= pkg.name %>-theme.css.map'
  161. },
  162. src: 'less/theme.less',
  163. dest: 'dist/css/<%= pkg.name %>-theme.css'
  164. }
  165. },
  166. autoprefixer: {
  167. options: {
  168. browsers: configBridge.config.autoprefixerBrowsers
  169. },
  170. core: {
  171. options: {
  172. map: true
  173. },
  174. src: 'dist/css/<%= pkg.name %>.css'
  175. },
  176. theme: {
  177. options: {
  178. map: true
  179. },
  180. src: 'dist/css/<%= pkg.name %>-theme.css'
  181. },
  182. docs: {
  183. src: ['docs/assets/css/src/docs.css']
  184. },
  185. examples: {
  186. expand: true,
  187. cwd: 'docs/examples/',
  188. src: ['**/*.css'],
  189. dest: 'docs/examples/'
  190. }
  191. },
  192. csslint: {
  193. options: {
  194. csslintrc: 'less/.csslintrc'
  195. },
  196. dist: [
  197. 'dist/css/bootstrap.css',
  198. 'dist/css/bootstrap-theme.css'
  199. ],
  200. examples: [
  201. 'docs/examples/**/*.css'
  202. ],
  203. docs: {
  204. options: {
  205. ids: false,
  206. 'overqualified-elements': false
  207. },
  208. src: 'docs/assets/css/src/docs.css'
  209. }
  210. },
  211. cssmin: {
  212. options: {
  213. // TODO: disable `zeroUnits` optimization once clean-css 3.2 is released
  214. // and then simplify the fix for https://github.com/twbs/bootstrap/issues/14837 accordingly
  215. compatibility: 'ie8',
  216. keepSpecialComments: '*',
  217. sourceMap: true,
  218. advanced: false
  219. },
  220. minifyCore: {
  221. src: 'dist/css/<%= pkg.name %>.css',
  222. dest: 'dist/css/<%= pkg.name %>.min.css'
  223. },
  224. minifyTheme: {
  225. src: 'dist/css/<%= pkg.name %>-theme.css',
  226. dest: 'dist/css/<%= pkg.name %>-theme.min.css'
  227. },
  228. docs: {
  229. src: [
  230. 'docs/assets/css/ie10-viewport-bug-workaround.css',
  231. 'docs/assets/css/src/pygments-manni.css',
  232. 'docs/assets/css/src/docs.css'
  233. ],
  234. dest: 'docs/assets/css/docs.min.css'
  235. }
  236. },
  237. csscomb: {
  238. options: {
  239. config: 'less/.csscomb.json'
  240. },
  241. dist: {
  242. expand: true,
  243. cwd: 'dist/css/',
  244. src: ['*.css', '!*.min.css'],
  245. dest: 'dist/css/'
  246. },
  247. examples: {
  248. expand: true,
  249. cwd: 'docs/examples/',
  250. src: '**/*.css',
  251. dest: 'docs/examples/'
  252. },
  253. docs: {
  254. src: 'docs/assets/css/src/docs.css',
  255. dest: 'docs/assets/css/src/docs.css'
  256. }
  257. },
  258. copy: {
  259. fonts: {
  260. expand: true,
  261. src: 'fonts/*',
  262. dest: 'dist/'
  263. },
  264. docs: {
  265. expand: true,
  266. cwd: 'dist/',
  267. src: [
  268. '**/*'
  269. ],
  270. dest: 'docs/dist/'
  271. }
  272. },
  273. connect: {
  274. server: {
  275. options: {
  276. port: 3000,
  277. base: '.'
  278. }
  279. }
  280. },
  281. jekyll: {
  282. options: {
  283. config: '_config.yml'
  284. },
  285. docs: {},
  286. github: {
  287. options: {
  288. raw: 'github: true'
  289. }
  290. }
  291. },
  292. htmlmin: {
  293. dist: {
  294. options: {
  295. collapseWhitespace: true,
  296. conservativeCollapse: true,
  297. minifyCSS: true,
  298. minifyJS: true,
  299. removeAttributeQuotes: true,
  300. removeComments: true
  301. },
  302. expand: true,
  303. cwd: '_gh_pages',
  304. dest: '_gh_pages',
  305. src: [
  306. '**/*.html',
  307. '!examples/**/*.html'
  308. ]
  309. }
  310. },
  311. jade: {
  312. options: {
  313. pretty: true,
  314. data: getLessVarsData
  315. },
  316. customizerVars: {
  317. src: 'docs/_jade/customizer-variables.jade',
  318. dest: 'docs/_includes/customizer-variables.html'
  319. },
  320. customizerNav: {
  321. src: 'docs/_jade/customizer-nav.jade',
  322. dest: 'docs/_includes/nav/customize.html'
  323. }
  324. },
  325. htmllint: {
  326. options: {
  327. ignore: [
  328. 'Attribute "autocomplete" not allowed on element "button" at this point.',
  329. 'Attribute "autocomplete" is only allowed when the input type is "color", "date", "datetime", "datetime-local", "email", "month", "number", "password", "range", "search", "tel", "text", "time", "url", or "week".',
  330. 'Element "img" is missing required attribute "src".'
  331. ]
  332. },
  333. src: '_gh_pages/**/*.html'
  334. },
  335. watch: {
  336. src: {
  337. files: '<%= jshint.core.src %>',
  338. tasks: ['jshint:core', 'qunit', 'concat']
  339. },
  340. test: {
  341. files: '<%= jshint.test.src %>',
  342. tasks: ['jshint:test', 'qunit']
  343. },
  344. less: {
  345. files: 'less/**/*.less',
  346. tasks: 'less'
  347. }
  348. },
  349. sed: {
  350. versionNumber: {
  351. pattern: (function () {
  352. var old = grunt.option('oldver');
  353. return old ? RegExp.quote(old) : old;
  354. })(),
  355. replacement: grunt.option('newver'),
  356. exclude: [
  357. 'dist/fonts',
  358. 'docs/assets',
  359. 'fonts',
  360. 'js/tests/vendor',
  361. 'node_modules',
  362. 'test-infra'
  363. ],
  364. recursive: true
  365. }
  366. },
  367. 'saucelabs-qunit': {
  368. all: {
  369. options: {
  370. build: process.env.TRAVIS_JOB_ID,
  371. throttled: 10,
  372. maxRetries: 3,
  373. maxPollRetries: 4,
  374. urls: ['http://127.0.0.1:3000/js/tests/index.html?hidepassed'],
  375. browsers: grunt.file.readYAML('grunt/sauce_browsers.yml')
  376. }
  377. }
  378. },
  379. exec: {
  380. npmUpdate: {
  381. command: 'npm update'
  382. }
  383. },
  384. compress: {
  385. main: {
  386. options: {
  387. archive: 'bootstrap-<%= pkg.version %>-dist.zip',
  388. mode: 'zip',
  389. level: 9,
  390. pretty: true
  391. },
  392. files: [
  393. {
  394. expand: true,
  395. cwd: 'dist/',
  396. src: ['**'],
  397. dest: 'bootstrap-<%= pkg.version %>-dist'
  398. }
  399. ]
  400. }
  401. }
  402. });
  403. // These plugins provide necessary tasks.
  404. require('load-grunt-tasks')(grunt, { scope: 'devDependencies' });
  405. require('time-grunt')(grunt);
  406. // Docs HTML validation task
  407. grunt.registerTask('validate-html', ['jekyll:docs', 'htmllint']);
  408. var runSubset = function (subset) {
  409. return !process.env.TWBS_TEST || process.env.TWBS_TEST === subset;
  410. };
  411. var isUndefOrNonZero = function (val) {
  412. return val === undefined || val !== '0';
  413. };
  414. // Test task.
  415. var testSubtasks = [];
  416. // Skip core tests if running a different subset of the test suite
  417. if (runSubset('core') &&
  418. // Skip core tests if this is a Savage build
  419. process.env.TRAVIS_REPO_SLUG !== 'twbs-savage/bootstrap') {
  420. testSubtasks = testSubtasks.concat(['dist-css', 'dist-js', 'csslint:dist', 'test-js', 'docs']);
  421. }
  422. // Skip HTML validation if running a different subset of the test suite
  423. if (runSubset('validate-html') &&
  424. // Skip HTML5 validator on Travis when [skip validator] is in the commit message
  425. isUndefOrNonZero(process.env.TWBS_DO_VALIDATOR)) {
  426. testSubtasks.push('validate-html');
  427. }
  428. // Only run Sauce Labs tests if there's a Sauce access key
  429. if (typeof process.env.SAUCE_ACCESS_KEY !== 'undefined' &&
  430. // Skip Sauce if running a different subset of the test suite
  431. runSubset('sauce-js-unit') &&
  432. // Skip Sauce on Travis when [skip sauce] is in the commit message
  433. isUndefOrNonZero(process.env.TWBS_DO_SAUCE)) {
  434. testSubtasks.push('connect');
  435. testSubtasks.push('saucelabs-qunit');
  436. }
  437. grunt.registerTask('test', testSubtasks);
  438. grunt.registerTask('test-js', ['jshint:core', 'jshint:test', 'jshint:grunt', 'jscs:core', 'jscs:test', 'jscs:grunt', 'qunit']);
  439. // JS distribution task.
  440. grunt.registerTask('dist-js', ['concat', 'uglify:core', 'commonjs']);
  441. // CSS distribution task.
  442. grunt.registerTask('less-compile', ['less:compileCore', 'less:compileTheme']);
  443. grunt.registerTask('dist-css', ['less-compile', 'autoprefixer:core', 'autoprefixer:theme', 'csscomb:dist', 'cssmin:minifyCore', 'cssmin:minifyTheme']);
  444. // Full distribution task.
  445. grunt.registerTask('dist', ['clean:dist', 'dist-css', 'copy:fonts', 'dist-js']);
  446. // Default task.
  447. grunt.registerTask('default', ['clean:dist', 'copy:fonts', 'test']);
  448. // Version numbering task.
  449. // grunt change-version-number --oldver=A.B.C --newver=X.Y.Z
  450. // This can be overzealous, so its changes should always be manually reviewed!
  451. grunt.registerTask('change-version-number', 'sed');
  452. grunt.registerTask('build-glyphicons-data', function () { generateGlyphiconsData.call(this, grunt); });
  453. // task for building customizer
  454. grunt.registerTask('build-customizer', ['build-customizer-html', 'build-raw-files']);
  455. grunt.registerTask('build-customizer-html', 'jade');
  456. grunt.registerTask('build-raw-files', 'Add scripts/less files to customizer.', function () {
  457. var banner = grunt.template.process('<%= banner %>');
  458. generateRawFiles(grunt, banner);
  459. });
  460. grunt.registerTask('commonjs', 'Generate CommonJS entrypoint module in dist dir.', function () {
  461. var srcFiles = grunt.config.get('concat.bootstrap.src');
  462. var destFilepath = 'dist/js/npm.js';
  463. generateCommonJSModule(grunt, srcFiles, destFilepath);
  464. });
  465. // Docs task.
  466. grunt.registerTask('docs-css', ['autoprefixer:docs', 'autoprefixer:examples', 'csscomb:docs', 'csscomb:examples', 'cssmin:docs']);
  467. grunt.registerTask('lint-docs-css', ['csslint:docs', 'csslint:examples']);
  468. grunt.registerTask('docs-js', ['uglify:docsJs', 'uglify:customize']);
  469. grunt.registerTask('lint-docs-js', ['jshint:assets', 'jscs:assets']);
  470. grunt.registerTask('docs', ['docs-css', 'lint-docs-css', 'docs-js', 'lint-docs-js', 'clean:docs', 'copy:docs', 'build-glyphicons-data', 'build-customizer']);
  471. grunt.registerTask('prep-release', ['dist', 'docs', 'jekyll:github', 'htmlmin', 'compress']);
  472. // Task for updating the cached npm packages used by the Travis build (which are controlled by test-infra/npm-shrinkwrap.json).
  473. // This task should be run and the updated file should be committed whenever Bootstrap's dependencies change.
  474. grunt.registerTask('update-shrinkwrap', ['exec:npmUpdate', '_update-shrinkwrap']);
  475. grunt.registerTask('_update-shrinkwrap', function () {
  476. var done = this.async();
  477. npmShrinkwrap({ dev: true, dirname: __dirname }, function (err) {
  478. if (err) {
  479. grunt.fail.warn(err);
  480. }
  481. var dest = 'test-infra/npm-shrinkwrap.json';
  482. fs.renameSync('npm-shrinkwrap.json', dest);
  483. grunt.log.writeln('File ' + dest.cyan + ' updated.');
  484. done();
  485. });
  486. });
  487. };