Gruntfile.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /*!
  2. * Bootstrap's Gruntfile
  3. * http://getbootstrap.com
  4. * Copyright 2013-2016 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 generateGlyphiconsData = require('./grunt/bs-glyphicons-data-generator.js');
  17. var BsLessdocParser = require('./grunt/bs-lessdoc-parser.js');
  18. var getLessVarsData = function () {
  19. var filePath = path.join(__dirname, 'less/variables.less');
  20. var fileContent = fs.readFileSync(filePath, { encoding: 'utf8' });
  21. var parser = new BsLessdocParser(fileContent);
  22. return { sections: parser.parseFile() };
  23. };
  24. var generateRawFiles = require('./grunt/bs-raw-files-generator.js');
  25. var generateCommonJSModule = require('./grunt/bs-commonjs-generator.js');
  26. var configBridge = grunt.file.readJSON('./grunt/configBridge.json', { encoding: 'utf8' });
  27. Object.keys(configBridge.paths).forEach(function (key) {
  28. configBridge.paths[key].forEach(function (val, i, arr) {
  29. arr[i] = path.join('./docs/assets', val);
  30. });
  31. });
  32. // Project configuration.
  33. grunt.initConfig({
  34. // Metadata.
  35. pkg: grunt.file.readJSON('package.json'),
  36. banner: '/*!\n' +
  37. ' * Bootstrap v<%= pkg.version %> (<%= pkg.homepage %>)\n' +
  38. ' * Copyright 2011-<%= grunt.template.today("yyyy") %> <%= pkg.author %>\n' +
  39. ' * Licensed under the <%= pkg.license %> license\n' +
  40. ' */\n',
  41. jqueryCheck: configBridge.config.jqueryCheck.join('\n'),
  42. jqueryVersionCheck: configBridge.config.jqueryVersionCheck.join('\n'),
  43. // Task configuration.
  44. clean: {
  45. dist: 'dist',
  46. docs: 'docs/dist'
  47. },
  48. jshint: {
  49. options: {
  50. jshintrc: 'js/.jshintrc'
  51. },
  52. grunt: {
  53. options: {
  54. jshintrc: 'grunt/.jshintrc'
  55. },
  56. src: ['Gruntfile.js', 'package.js', 'grunt/*.js']
  57. },
  58. core: {
  59. src: 'js/*.js'
  60. },
  61. test: {
  62. options: {
  63. jshintrc: 'js/tests/unit/.jshintrc'
  64. },
  65. src: 'js/tests/unit/*.js'
  66. },
  67. assets: {
  68. src: ['docs/assets/js/src/*.js', 'docs/assets/js/*.js', '!docs/assets/js/*.min.js']
  69. }
  70. },
  71. jscs: {
  72. options: {
  73. config: 'js/.jscsrc'
  74. },
  75. grunt: {
  76. src: '<%= jshint.grunt.src %>'
  77. },
  78. core: {
  79. src: '<%= jshint.core.src %>'
  80. },
  81. test: {
  82. src: '<%= jshint.test.src %>'
  83. },
  84. assets: {
  85. options: {
  86. requireCamelCaseOrUpperCaseIdentifiers: null
  87. },
  88. src: '<%= jshint.assets.src %>'
  89. }
  90. },
  91. concat: {
  92. options: {
  93. banner: '<%= banner %>\n<%= jqueryCheck %>\n<%= jqueryVersionCheck %>',
  94. stripBanners: false
  95. },
  96. bootstrap: {
  97. src: [
  98. 'js/transition.js',
  99. 'js/alert.js',
  100. 'js/button.js',
  101. 'js/carousel.js',
  102. 'js/collapse.js',
  103. 'js/dropdown.js',
  104. 'js/modal.js',
  105. 'js/tooltip.js',
  106. 'js/popover.js',
  107. 'js/scrollspy.js',
  108. 'js/tab.js',
  109. 'js/affix.js'
  110. ],
  111. dest: 'dist/js/<%= pkg.name %>.js'
  112. }
  113. },
  114. uglify: {
  115. options: {
  116. compress: {
  117. warnings: false
  118. },
  119. mangle: true,
  120. preserveComments: /^!|@preserve|@license|@cc_on/i
  121. },
  122. core: {
  123. src: '<%= concat.bootstrap.dest %>',
  124. dest: 'dist/js/<%= pkg.name %>.min.js'
  125. },
  126. customize: {
  127. src: configBridge.paths.customizerJs,
  128. dest: 'docs/assets/js/customize.min.js'
  129. },
  130. docsJs: {
  131. src: configBridge.paths.docsJs,
  132. dest: 'docs/assets/js/docs.min.js'
  133. }
  134. },
  135. qunit: {
  136. options: {
  137. inject: 'js/tests/unit/phantom.js'
  138. },
  139. files: 'js/tests/index.html'
  140. },
  141. less: {
  142. compileCore: {
  143. options: {
  144. strictMath: true,
  145. sourceMap: true,
  146. outputSourceFiles: true,
  147. sourceMapURL: '<%= pkg.name %>.css.map',
  148. sourceMapFilename: 'dist/css/<%= pkg.name %>.css.map'
  149. },
  150. src: 'less/bootstrap.less',
  151. dest: 'dist/css/<%= pkg.name %>.css'
  152. },
  153. compileTheme: {
  154. options: {
  155. strictMath: true,
  156. sourceMap: true,
  157. outputSourceFiles: true,
  158. sourceMapURL: '<%= pkg.name %>-theme.css.map',
  159. sourceMapFilename: 'dist/css/<%= pkg.name %>-theme.css.map'
  160. },
  161. src: 'less/theme.less',
  162. dest: 'dist/css/<%= pkg.name %>-theme.css'
  163. }
  164. },
  165. autoprefixer: {
  166. options: {
  167. browsers: configBridge.config.autoprefixerBrowsers
  168. },
  169. core: {
  170. options: {
  171. map: true
  172. },
  173. src: 'dist/css/<%= pkg.name %>.css'
  174. },
  175. theme: {
  176. options: {
  177. map: true
  178. },
  179. src: 'dist/css/<%= pkg.name %>-theme.css'
  180. },
  181. docs: {
  182. src: ['docs/assets/css/src/docs.css']
  183. },
  184. examples: {
  185. expand: true,
  186. cwd: 'docs/examples/',
  187. src: ['**/*.css'],
  188. dest: 'docs/examples/'
  189. }
  190. },
  191. csslint: {
  192. options: {
  193. csslintrc: 'less/.csslintrc'
  194. },
  195. dist: [
  196. 'dist/css/bootstrap.css',
  197. 'dist/css/bootstrap-theme.css'
  198. ],
  199. examples: [
  200. 'docs/examples/**/*.css'
  201. ],
  202. docs: {
  203. options: {
  204. ids: false,
  205. 'overqualified-elements': false
  206. },
  207. src: 'docs/assets/css/src/docs.css'
  208. }
  209. },
  210. cssmin: {
  211. options: {
  212. // TODO: disable `zeroUnits` optimization once clean-css 3.2 is released
  213. // and then simplify the fix for https://github.com/twbs/bootstrap/issues/14837 accordingly
  214. compatibility: 'ie8',
  215. keepSpecialComments: '*',
  216. sourceMap: true,
  217. sourceMapInlineSources: 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. bundleExec: true,
  284. config: '_config.yml',
  285. incremental: false
  286. },
  287. docs: {},
  288. github: {
  289. options: {
  290. raw: 'github: true'
  291. }
  292. }
  293. },
  294. htmlmin: {
  295. dist: {
  296. options: {
  297. collapseBooleanAttributes: true,
  298. collapseWhitespace: true,
  299. conservativeCollapse: true,
  300. decodeEntities: false,
  301. minifyCSS: {
  302. compatibility: 'ie8',
  303. keepSpecialComments: 0
  304. },
  305. minifyJS: true,
  306. minifyURLs: false,
  307. processConditionalComments: true,
  308. removeAttributeQuotes: true,
  309. removeComments: true,
  310. removeOptionalAttributes: true,
  311. removeOptionalTags: true,
  312. removeRedundantAttributes: true,
  313. removeScriptTypeAttributes: true,
  314. removeStyleLinkTypeAttributes: true,
  315. removeTagWhitespace: false,
  316. sortAttributes: true,
  317. sortClassName: true
  318. },
  319. expand: true,
  320. cwd: '_gh_pages',
  321. dest: '_gh_pages',
  322. src: [
  323. '**/*.html',
  324. '!examples/**/*.html'
  325. ]
  326. }
  327. },
  328. pug: {
  329. options: {
  330. pretty: true,
  331. data: getLessVarsData
  332. },
  333. customizerVars: {
  334. src: 'docs/_pug/customizer-variables.pug',
  335. dest: 'docs/_includes/customizer-variables.html'
  336. },
  337. customizerNav: {
  338. src: 'docs/_pug/customizer-nav.pug',
  339. dest: 'docs/_includes/nav/customize.html'
  340. }
  341. },
  342. htmllint: {
  343. options: {
  344. ignore: [
  345. 'Attribute "autocomplete" not allowed on element "button" at this point.',
  346. 'Attribute "autocomplete" is only allowed when the input type is "color", "date", "datetime", "datetime-local", "email", "hidden", "month", "number", "password", "range", "search", "tel", "text", "time", "url", or "week".',
  347. 'Element "img" is missing required attribute "src".'
  348. ]
  349. },
  350. src: '_gh_pages/**/*.html'
  351. },
  352. watch: {
  353. src: {
  354. files: '<%= jshint.core.src %>',
  355. tasks: ['jshint:core', 'qunit', 'concat']
  356. },
  357. test: {
  358. files: '<%= jshint.test.src %>',
  359. tasks: ['jshint:test', 'qunit']
  360. },
  361. less: {
  362. files: 'less/**/*.less',
  363. tasks: 'less'
  364. }
  365. },
  366. 'saucelabs-qunit': {
  367. all: {
  368. options: {
  369. build: process.env.TRAVIS_JOB_ID,
  370. throttled: 10,
  371. maxRetries: 3,
  372. maxPollRetries: 4,
  373. urls: ['http://127.0.0.1:3000/js/tests/index.html?hidepassed'],
  374. browsers: grunt.file.readYAML('grunt/sauce_browsers.yml')
  375. }
  376. }
  377. },
  378. exec: {
  379. npmUpdate: {
  380. command: 'npm update'
  381. }
  382. },
  383. compress: {
  384. main: {
  385. options: {
  386. archive: 'bootstrap-<%= pkg.version %>-dist.zip',
  387. mode: 'zip',
  388. level: 9,
  389. pretty: true
  390. },
  391. files: [
  392. {
  393. expand: true,
  394. cwd: 'dist/',
  395. src: ['**'],
  396. dest: 'bootstrap-<%= pkg.version %>-dist'
  397. }
  398. ]
  399. }
  400. }
  401. });
  402. // These plugins provide necessary tasks.
  403. require('load-grunt-tasks')(grunt, { scope: 'devDependencies' });
  404. require('time-grunt')(grunt);
  405. // Docs HTML validation task
  406. grunt.registerTask('validate-html', ['jekyll:docs', 'htmllint']);
  407. var runSubset = function (subset) {
  408. return !process.env.TWBS_TEST || process.env.TWBS_TEST === subset;
  409. };
  410. var isUndefOrNonZero = function (val) {
  411. return val === undefined || val !== '0';
  412. };
  413. // Test task.
  414. var testSubtasks = [];
  415. // Skip core tests if running a different subset of the test suite
  416. if (runSubset('core') &&
  417. // Skip core tests if this is a Savage build
  418. process.env.TRAVIS_REPO_SLUG !== 'twbs-savage/bootstrap') {
  419. testSubtasks = testSubtasks.concat(['dist-css', 'dist-js', 'csslint:dist', 'test-js', 'docs']);
  420. }
  421. // Skip HTML validation if running a different subset of the test suite
  422. if (runSubset('validate-html') &&
  423. // Skip HTML5 validator on Travis when [skip validator] is in the commit message
  424. isUndefOrNonZero(process.env.TWBS_DO_VALIDATOR)) {
  425. testSubtasks.push('validate-html');
  426. }
  427. // Only run Sauce Labs tests if there's a Sauce access key
  428. if (typeof process.env.SAUCE_ACCESS_KEY !== 'undefined' &&
  429. // Skip Sauce if running a different subset of the test suite
  430. runSubset('sauce-js-unit') &&
  431. // Skip Sauce on Travis when [skip sauce] is in the commit message
  432. isUndefOrNonZero(process.env.TWBS_DO_SAUCE)) {
  433. testSubtasks.push('connect');
  434. testSubtasks.push('saucelabs-qunit');
  435. }
  436. grunt.registerTask('test', testSubtasks);
  437. grunt.registerTask('test-js', ['jshint:core', 'jshint:test', 'jshint:grunt', 'jscs:core', 'jscs:test', 'jscs:grunt', 'qunit']);
  438. // JS distribution task.
  439. grunt.registerTask('dist-js', ['concat', 'uglify:core', 'commonjs']);
  440. // CSS distribution task.
  441. grunt.registerTask('less-compile', ['less:compileCore', 'less:compileTheme']);
  442. grunt.registerTask('dist-css', ['less-compile', 'autoprefixer:core', 'autoprefixer:theme', 'csscomb:dist', 'cssmin:minifyCore', 'cssmin:minifyTheme']);
  443. // Full distribution task.
  444. grunt.registerTask('dist', ['clean:dist', 'dist-css', 'copy:fonts', 'dist-js']);
  445. // Default task.
  446. grunt.registerTask('default', ['clean:dist', 'copy:fonts', 'test']);
  447. grunt.registerTask('build-glyphicons-data', function () { generateGlyphiconsData.call(this, grunt); });
  448. // task for building customizer
  449. grunt.registerTask('build-customizer', ['build-customizer-html', 'build-raw-files']);
  450. grunt.registerTask('build-customizer-html', 'pug');
  451. grunt.registerTask('build-raw-files', 'Add scripts/less files to customizer.', function () {
  452. var banner = grunt.template.process('<%= banner %>');
  453. generateRawFiles(grunt, banner);
  454. });
  455. grunt.registerTask('commonjs', 'Generate CommonJS entrypoint module in dist dir.', function () {
  456. var srcFiles = grunt.config.get('concat.bootstrap.src');
  457. var destFilepath = 'dist/js/npm.js';
  458. generateCommonJSModule(grunt, srcFiles, destFilepath);
  459. });
  460. // Docs task.
  461. grunt.registerTask('docs-css', ['autoprefixer:docs', 'autoprefixer:examples', 'csscomb:docs', 'csscomb:examples', 'cssmin:docs']);
  462. grunt.registerTask('lint-docs-css', ['csslint:docs', 'csslint:examples']);
  463. grunt.registerTask('docs-js', ['uglify:docsJs', 'uglify:customize']);
  464. grunt.registerTask('lint-docs-js', ['jshint:assets', 'jscs:assets']);
  465. grunt.registerTask('docs', ['docs-css', 'lint-docs-css', 'docs-js', 'lint-docs-js', 'clean:docs', 'copy:docs', 'build-glyphicons-data', 'build-customizer']);
  466. grunt.registerTask('docs-github', ['jekyll:github', 'htmlmin']);
  467. grunt.registerTask('prep-release', ['dist', 'docs', 'docs-github', 'compress']);
  468. };