Gruntfile.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. advanced: false
  218. },
  219. minifyCore: {
  220. src: 'dist/css/<%= pkg.name %>.css',
  221. dest: 'dist/css/<%= pkg.name %>.min.css'
  222. },
  223. minifyTheme: {
  224. src: 'dist/css/<%= pkg.name %>-theme.css',
  225. dest: 'dist/css/<%= pkg.name %>-theme.min.css'
  226. },
  227. docs: {
  228. src: [
  229. 'docs/assets/css/src/pygments-manni.css',
  230. 'docs/assets/css/src/docs.css'
  231. ],
  232. dest: 'docs/assets/css/docs.min.css'
  233. }
  234. },
  235. csscomb: {
  236. options: {
  237. config: 'less/.csscomb.json'
  238. },
  239. dist: {
  240. expand: true,
  241. cwd: 'dist/css/',
  242. src: ['*.css', '!*.min.css'],
  243. dest: 'dist/css/'
  244. },
  245. examples: {
  246. expand: true,
  247. cwd: 'docs/examples/',
  248. src: '**/*.css',
  249. dest: 'docs/examples/'
  250. },
  251. docs: {
  252. src: 'docs/assets/css/src/docs.css',
  253. dest: 'docs/assets/css/src/docs.css'
  254. }
  255. },
  256. copy: {
  257. fonts: {
  258. expand: true,
  259. src: 'fonts/*',
  260. dest: 'dist/'
  261. },
  262. docs: {
  263. expand: true,
  264. cwd: 'dist/',
  265. src: [
  266. '**/*'
  267. ],
  268. dest: 'docs/dist/'
  269. }
  270. },
  271. connect: {
  272. server: {
  273. options: {
  274. port: 3000,
  275. base: '.'
  276. }
  277. }
  278. },
  279. jekyll: {
  280. options: {
  281. config: '_config.yml'
  282. },
  283. docs: {},
  284. github: {
  285. options: {
  286. raw: 'github: true'
  287. }
  288. }
  289. },
  290. htmlmin: {
  291. dist: {
  292. options: {
  293. collapseWhitespace: true,
  294. conservativeCollapse: true,
  295. minifyCSS: true,
  296. minifyJS: true,
  297. removeAttributeQuotes: true,
  298. removeComments: true
  299. },
  300. expand: true,
  301. cwd: '_gh_pages',
  302. dest: '_gh_pages',
  303. src: [
  304. '**/*.html',
  305. '!examples/**/*.html'
  306. ]
  307. }
  308. },
  309. jade: {
  310. options: {
  311. pretty: true,
  312. data: getLessVarsData
  313. },
  314. customizerVars: {
  315. src: 'docs/_jade/customizer-variables.jade',
  316. dest: 'docs/_includes/customizer-variables.html'
  317. },
  318. customizerNav: {
  319. src: 'docs/_jade/customizer-nav.jade',
  320. dest: 'docs/_includes/nav/customize.html'
  321. }
  322. },
  323. htmllint: {
  324. options: {
  325. ignore: [
  326. 'Attribute "autocomplete" not allowed on element "button" at this point.',
  327. 'Attribute "autocomplete" not allowed on element "input" at this point.',
  328. 'Element "img" is missing required attribute "src".'
  329. ]
  330. },
  331. src: '_gh_pages/**/*.html'
  332. },
  333. watch: {
  334. src: {
  335. files: '<%= jshint.core.src %>',
  336. tasks: ['jshint:core', 'qunit', 'concat']
  337. },
  338. test: {
  339. files: '<%= jshint.test.src %>',
  340. tasks: ['jshint:test', 'qunit']
  341. },
  342. less: {
  343. files: 'less/**/*.less',
  344. tasks: 'less'
  345. }
  346. },
  347. sed: {
  348. versionNumber: {
  349. pattern: (function () {
  350. var old = grunt.option('oldver');
  351. return old ? RegExp.quote(old) : old;
  352. })(),
  353. replacement: grunt.option('newver'),
  354. exclude: [
  355. 'dist/fonts',
  356. 'docs/assets',
  357. 'fonts',
  358. 'js/tests/vendor',
  359. 'node_modules',
  360. 'test-infra'
  361. ],
  362. recursive: true
  363. }
  364. },
  365. 'saucelabs-qunit': {
  366. all: {
  367. options: {
  368. build: process.env.TRAVIS_JOB_ID,
  369. throttled: 10,
  370. maxRetries: 3,
  371. maxPollRetries: 4,
  372. urls: ['http://127.0.0.1:3000/js/tests/index.html?hidepassed'],
  373. browsers: grunt.file.readYAML('grunt/sauce_browsers.yml')
  374. }
  375. }
  376. },
  377. exec: {
  378. npmUpdate: {
  379. command: 'npm update'
  380. }
  381. },
  382. compress: {
  383. main: {
  384. options: {
  385. archive: 'bootstrap-<%= pkg.version %>-dist.zip',
  386. mode: 'zip',
  387. level: 9,
  388. pretty: true
  389. },
  390. files: [
  391. {
  392. expand: true,
  393. cwd: 'dist/',
  394. src: ['**'],
  395. dest: 'bootstrap-<%= pkg.version %>-dist'
  396. }
  397. ]
  398. }
  399. }
  400. });
  401. // These plugins provide necessary tasks.
  402. require('load-grunt-tasks')(grunt, { scope: 'devDependencies' });
  403. require('time-grunt')(grunt);
  404. // Docs HTML validation task
  405. grunt.registerTask('validate-html', ['jekyll:docs', 'htmllint']);
  406. var runSubset = function (subset) {
  407. return !process.env.TWBS_TEST || process.env.TWBS_TEST === subset;
  408. };
  409. var isUndefOrNonZero = function (val) {
  410. return val === undefined || val !== '0';
  411. };
  412. // Test task.
  413. var testSubtasks = [];
  414. // Skip core tests if running a different subset of the test suite
  415. if (runSubset('core') &&
  416. // Skip core tests if this is a Savage build
  417. process.env.TRAVIS_REPO_SLUG !== 'twbs-savage/bootstrap') {
  418. testSubtasks = testSubtasks.concat(['dist-css', 'dist-js', 'csslint:dist', 'test-js', 'docs']);
  419. }
  420. // Skip HTML validation if running a different subset of the test suite
  421. if (runSubset('validate-html') &&
  422. // Skip HTML5 validator on Travis when [skip validator] is in the commit message
  423. isUndefOrNonZero(process.env.TWBS_DO_VALIDATOR)) {
  424. testSubtasks.push('validate-html');
  425. }
  426. // Only run Sauce Labs tests if there's a Sauce access key
  427. if (typeof process.env.SAUCE_ACCESS_KEY !== 'undefined' &&
  428. // Skip Sauce if running a different subset of the test suite
  429. runSubset('sauce-js-unit') &&
  430. // Skip Sauce on Travis when [skip sauce] is in the commit message
  431. isUndefOrNonZero(process.env.TWBS_DO_SAUCE)) {
  432. testSubtasks.push('connect');
  433. testSubtasks.push('saucelabs-qunit');
  434. }
  435. grunt.registerTask('test', testSubtasks);
  436. grunt.registerTask('test-js', ['jshint:core', 'jshint:test', 'jshint:grunt', 'jscs:core', 'jscs:test', 'jscs:grunt', 'qunit']);
  437. // JS distribution task.
  438. grunt.registerTask('dist-js', ['concat', 'uglify:core', 'commonjs']);
  439. // CSS distribution task.
  440. grunt.registerTask('less-compile', ['less:compileCore', 'less:compileTheme']);
  441. grunt.registerTask('dist-css', ['less-compile', 'autoprefixer:core', 'autoprefixer:theme', 'csscomb:dist', 'cssmin:minifyCore', 'cssmin:minifyTheme']);
  442. // Full distribution task.
  443. grunt.registerTask('dist', ['clean:dist', 'dist-css', 'copy:fonts', 'dist-js']);
  444. // Default task.
  445. grunt.registerTask('default', ['clean:dist', 'copy:fonts', 'test']);
  446. // Version numbering task.
  447. // grunt change-version-number --oldver=A.B.C --newver=X.Y.Z
  448. // This can be overzealous, so its changes should always be manually reviewed!
  449. grunt.registerTask('change-version-number', 'sed');
  450. grunt.registerTask('build-glyphicons-data', function () { generateGlyphiconsData.call(this, grunt); });
  451. // task for building customizer
  452. grunt.registerTask('build-customizer', ['build-customizer-html', 'build-raw-files']);
  453. grunt.registerTask('build-customizer-html', 'jade');
  454. grunt.registerTask('build-raw-files', 'Add scripts/less files to customizer.', function () {
  455. var banner = grunt.template.process('<%= banner %>');
  456. generateRawFiles(grunt, banner);
  457. });
  458. grunt.registerTask('commonjs', 'Generate CommonJS entrypoint module in dist dir.', function () {
  459. var srcFiles = grunt.config.get('concat.bootstrap.src');
  460. var destFilepath = 'dist/js/npm.js';
  461. generateCommonJSModule(grunt, srcFiles, destFilepath);
  462. });
  463. // Docs task.
  464. grunt.registerTask('docs-css', ['autoprefixer:docs', 'autoprefixer:examples', 'csscomb:docs', 'csscomb:examples', 'cssmin:docs']);
  465. grunt.registerTask('lint-docs-css', ['csslint:docs', 'csslint:examples']);
  466. grunt.registerTask('docs-js', ['uglify:docsJs', 'uglify:customize']);
  467. grunt.registerTask('lint-docs-js', ['jshint:assets', 'jscs:assets']);
  468. grunt.registerTask('docs', ['docs-css', 'lint-docs-css', 'docs-js', 'lint-docs-js', 'clean:docs', 'copy:docs', 'build-glyphicons-data', 'build-customizer']);
  469. grunt.registerTask('prep-release', ['dist', 'docs', 'jekyll:github', 'htmlmin', 'compress']);
  470. // Task for updating the cached npm packages used by the Travis build (which are controlled by test-infra/npm-shrinkwrap.json).
  471. // This task should be run and the updated file should be committed whenever Bootstrap's dependencies change.
  472. grunt.registerTask('update-shrinkwrap', ['exec:npmUpdate', '_update-shrinkwrap']);
  473. grunt.registerTask('_update-shrinkwrap', function () {
  474. var done = this.async();
  475. npmShrinkwrap({ dev: true, dirname: __dirname }, function (err) {
  476. if (err) {
  477. grunt.fail.warn(err);
  478. }
  479. var dest = 'test-infra/npm-shrinkwrap.json';
  480. fs.renameSync('npm-shrinkwrap.json', dest);
  481. grunt.log.writeln('File ' + dest.cyan + ' updated.');
  482. done();
  483. });
  484. });
  485. };