Gruntfile.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*!
  2. * Bootstrap's Gruntfile
  3. * https://getbootstrap.com
  4. * Copyright 2013-2016 The Bootstrap Authors
  5. * Copyright 2013-2016 Twitter, Inc.
  6. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
  7. */
  8. module.exports = function (grunt) {
  9. 'use strict';
  10. // Force use of Unix newlines
  11. grunt.util.linefeed = '\n';
  12. RegExp.quote = function (string) {
  13. return string.replace(/[-\\^$*+?.()|[\]{}]/g, '\\$&');
  14. };
  15. var fs = require('fs');
  16. var path = require('path');
  17. var isTravis = require('is-travis');
  18. var configBridge = grunt.file.readJSON('./grunt/configBridge.json', { encoding: 'utf8' });
  19. Object.keys(configBridge.paths).forEach(function (key) {
  20. configBridge.paths[key].forEach(function (val, i, arr) {
  21. arr[i] = path.join('./docs', val);
  22. });
  23. });
  24. // Project configuration.
  25. grunt.initConfig({
  26. // Metadata.
  27. pkg: grunt.file.readJSON('package.json'),
  28. banner: '/*!\n' +
  29. ' * Bootstrap v<%= pkg.version %> (<%= pkg.homepage %>)\n' +
  30. ' * Copyright 2011-<%= grunt.template.today("yyyy") %> <%= pkg.author %>\n' +
  31. ' * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)\n' +
  32. ' */\n',
  33. jqueryCheck: 'if (typeof jQuery === \'undefined\') {\n' +
  34. ' throw new Error(\'Bootstrap\\\'s JavaScript requires jQuery\')\n' +
  35. '}\n',
  36. jqueryVersionCheck: '+function ($) {\n' +
  37. ' var version = $.fn.jquery.split(\' \')[0].split(\'.\')\n' +
  38. ' if ((version[0] < 2 && version[1] < 9) || (version[0] == 1 && version[1] == 9 && version[2] < 1) || (version[0] >= 4)) {\n' +
  39. ' throw new Error(\'Bootstrap\\\'s JavaScript requires at least jQuery v1.9.1 but less than v4.0.0\')\n' +
  40. ' }\n' +
  41. '}(jQuery);\n\n',
  42. // Task configuration.
  43. clean: {
  44. dist: 'dist',
  45. docs: 'docs/dist'
  46. },
  47. // JS build configuration
  48. babel: {
  49. dev: {
  50. options: {
  51. sourceMap: true
  52. },
  53. files: {
  54. 'js/dist/util.js' : 'js/src/util.js',
  55. 'js/dist/alert.js' : 'js/src/alert.js',
  56. 'js/dist/button.js' : 'js/src/button.js',
  57. 'js/dist/carousel.js' : 'js/src/carousel.js',
  58. 'js/dist/collapse.js' : 'js/src/collapse.js',
  59. 'js/dist/dropdown.js' : 'js/src/dropdown.js',
  60. 'js/dist/modal.js' : 'js/src/modal.js',
  61. 'js/dist/scrollspy.js' : 'js/src/scrollspy.js',
  62. 'js/dist/tab.js' : 'js/src/tab.js',
  63. 'js/dist/tooltip.js' : 'js/src/tooltip.js',
  64. 'js/dist/popover.js' : 'js/src/popover.js'
  65. }
  66. },
  67. dist: {
  68. options: {
  69. extends: '../../js/.babelrc'
  70. },
  71. files: {
  72. '<%= concat.bootstrap.dest %>' : '<%= concat.bootstrap.dest %>'
  73. }
  74. }
  75. },
  76. stamp: {
  77. options: {
  78. banner: '<%= banner %>\n<%= jqueryCheck %>\n<%= jqueryVersionCheck %>\n+function () {\n',
  79. footer: '\n}();'
  80. },
  81. bootstrap: {
  82. files: {
  83. src: '<%= concat.bootstrap.dest %>'
  84. }
  85. }
  86. },
  87. concat: {
  88. options: {
  89. // Custom function to remove all export and import statements
  90. process: function (src) {
  91. return src.replace(/^(export|import).*/gm, '');
  92. }
  93. },
  94. bootstrap: {
  95. src: [
  96. 'js/src/util.js',
  97. 'js/src/alert.js',
  98. 'js/src/button.js',
  99. 'js/src/carousel.js',
  100. 'js/src/collapse.js',
  101. 'js/src/dropdown.js',
  102. 'js/src/modal.js',
  103. 'js/src/scrollspy.js',
  104. 'js/src/tab.js',
  105. 'js/src/tooltip.js',
  106. 'js/src/popover.js'
  107. ],
  108. dest: 'dist/js/<%= pkg.name %>.js'
  109. }
  110. },
  111. uglify: {
  112. options: {
  113. compress: {
  114. warnings: false
  115. },
  116. mangle: true,
  117. preserveComments: /^!|@preserve|@license|@cc_on/i
  118. },
  119. core: {
  120. src: '<%= concat.bootstrap.dest %>',
  121. dest: 'dist/js/<%= pkg.name %>.min.js'
  122. },
  123. docsJs: {
  124. src: configBridge.paths.docsJs,
  125. dest: 'docs/assets/js/docs.min.js'
  126. }
  127. },
  128. qunit: {
  129. options: {
  130. inject: 'js/tests/unit/phantom.js'
  131. },
  132. files: 'js/tests/index.html'
  133. },
  134. // CSS build configuration
  135. scsslint: {
  136. options: {
  137. bundleExec: true,
  138. config: 'scss/.scss-lint.yml',
  139. reporterOutput: null
  140. },
  141. core: {
  142. src: ['scss/*.scss', '!scss/_normalize.scss']
  143. },
  144. docs: {
  145. src: ['docs/assets/scss/*.scss', '!docs/assets/scss/docs.scss']
  146. }
  147. },
  148. cssmin: {
  149. options: {
  150. compatibility: 'ie9,-properties.zeroUnits',
  151. sourceMap: true,
  152. // sourceMapInlineSources: true,
  153. advanced: false
  154. },
  155. core: {
  156. files: [
  157. {
  158. expand: true,
  159. cwd: 'dist/css',
  160. src: ['*.css', '!*.min.css'],
  161. dest: 'dist/css',
  162. ext: '.min.css'
  163. }
  164. ]
  165. },
  166. docs: {
  167. files: [
  168. {
  169. expand: true,
  170. cwd: 'docs/assets/css',
  171. src: ['*.css', '!*.min.css'],
  172. dest: 'docs/assets/css',
  173. ext: '.min.css'
  174. }
  175. ]
  176. }
  177. },
  178. copy: {
  179. docs: {
  180. expand: true,
  181. cwd: 'dist/',
  182. src: [
  183. '**/*'
  184. ],
  185. dest: 'docs/dist/'
  186. }
  187. },
  188. connect: {
  189. server: {
  190. options: {
  191. port: 3000,
  192. base: '.'
  193. }
  194. }
  195. },
  196. jekyll: {
  197. options: {
  198. bundleExec: true,
  199. config: '_config.yml',
  200. incremental: false
  201. },
  202. docs: {},
  203. github: {
  204. options: {
  205. raw: 'github: true'
  206. }
  207. }
  208. },
  209. htmllint: {
  210. options: {
  211. ignore: [
  212. '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”.',
  213. 'Attribute “autocomplete” not allowed on element “button” at this point.',
  214. 'Consider using the “h1” element as a top-level heading only (all “h1” elements are treated as top-level headings by many screen readers and other tools).',
  215. 'Element “div” not allowed as child of element “progress” in this context. (Suppressing further errors from this subtree.)',
  216. 'Element “img” is missing required attribute “src”.',
  217. 'The “color” input type is not supported in all browsers. Please be sure to test, and consider using a polyfill.',
  218. 'The “date” input type is not supported in all browsers. Please be sure to test, and consider using a polyfill.',
  219. 'The “datetime” input type is not supported in all browsers. Please be sure to test, and consider using a polyfill.',
  220. 'The “datetime-local” input type is not supported in all browsers. Please be sure to test, and consider using a polyfill.',
  221. 'The “month” input type is not supported in all browsers. Please be sure to test, and consider using a polyfill.',
  222. 'The “time” input type is not supported in all browsers. Please be sure to test, and consider using a polyfill.',
  223. 'The “week” input type is not supported in all browsers. Please be sure to test, and consider using a polyfill.'
  224. ]
  225. },
  226. src: ['_gh_pages/**/*.html', 'js/tests/visual/*.html']
  227. },
  228. watch: {
  229. src: {
  230. files: '<%= concat.bootstrap.src %>',
  231. tasks: ['babel:dev']
  232. },
  233. sass: {
  234. files: 'scss/**/*.scss',
  235. tasks: ['dist-css', 'docs']
  236. },
  237. docs: {
  238. files: 'docs/assets/scss/**/*.scss',
  239. tasks: ['dist-css', 'docs']
  240. }
  241. },
  242. 'saucelabs-qunit': {
  243. all: {
  244. options: {
  245. build: process.env.TRAVIS_JOB_ID,
  246. concurrency: 10,
  247. maxRetries: 3,
  248. maxPollRetries: 4,
  249. urls: ['http://127.0.0.1:3000/js/tests/index.html?hidepassed'],
  250. browsers: grunt.file.readYAML('grunt/sauce_browsers.yml')
  251. }
  252. }
  253. },
  254. exec: {
  255. postcss: {
  256. command: 'npm run postcss'
  257. },
  258. 'postcss-docs': {
  259. command: 'npm run postcss-docs'
  260. },
  261. htmlhint: {
  262. command: 'npm run htmlhint'
  263. },
  264. 'upload-preview': {
  265. command: './grunt/upload-preview.sh'
  266. }
  267. },
  268. buildcontrol: {
  269. options: {
  270. dir: '_gh_pages',
  271. commit: true,
  272. push: true,
  273. message: 'Built %sourceName% from commit %sourceCommit% on branch %sourceBranch%'
  274. },
  275. pages: {
  276. options: {
  277. remote: 'git@github.com:twbs/derpstrap.git',
  278. branch: 'gh-pages'
  279. }
  280. }
  281. },
  282. compress: {
  283. main: {
  284. options: {
  285. archive: 'bootstrap-<%= pkg.version %>-dist.zip',
  286. mode: 'zip',
  287. level: 9,
  288. pretty: true
  289. },
  290. files: [
  291. {
  292. expand: true,
  293. cwd: 'dist/',
  294. src: ['**'],
  295. dest: 'bootstrap-<%= pkg.version %>-dist'
  296. }
  297. ]
  298. }
  299. }
  300. });
  301. // These plugins provide necessary tasks.
  302. require('load-grunt-tasks')(grunt, { scope: 'devDependencies',
  303. // Exclude Sass compilers. We choose the one to load later on.
  304. pattern: ['grunt-*', '!grunt-sass', '!grunt-contrib-sass'] });
  305. require('time-grunt')(grunt);
  306. // Docs HTML validation task
  307. grunt.registerTask('validate-html', ['jekyll:docs', 'htmllint', 'exec:htmlhint']);
  308. var runSubset = function (subset) {
  309. return !process.env.TWBS_TEST || process.env.TWBS_TEST === subset;
  310. };
  311. var isUndefOrNonZero = function (val) {
  312. return val === undefined || val !== '0';
  313. };
  314. // Test task.
  315. var testSubtasks = [];
  316. // Skip core tests if running a different subset of the test suite
  317. if (runSubset('core') &&
  318. // Skip core tests if this is a Savage build
  319. process.env.TRAVIS_REPO_SLUG !== 'twbs-savage/bootstrap') {
  320. testSubtasks = testSubtasks.concat(['dist-css', 'dist-js', 'test-scss', 'qunit', 'docs']);
  321. }
  322. // Skip HTML validation if running a different subset of the test suite
  323. if (runSubset('validate-html') &&
  324. isTravis &&
  325. // Skip HTML5 validator when [skip validator] is in the commit message
  326. isUndefOrNonZero(process.env.TWBS_DO_VALIDATOR)) {
  327. testSubtasks.push('validate-html');
  328. }
  329. // Only run Sauce Labs tests if there's a Sauce access key
  330. if (typeof process.env.SAUCE_ACCESS_KEY !== 'undefined' &&
  331. // Skip Sauce if running a different subset of the test suite
  332. runSubset('sauce-js-unit')) {
  333. testSubtasks = testSubtasks.concat(['dist', 'docs-css', 'docs-js', 'clean:docs', 'copy:docs', 'exec:upload-preview']);
  334. // Skip Sauce on Travis when [skip sauce] is in the commit message
  335. if (isUndefOrNonZero(process.env.TWBS_DO_SAUCE)) {
  336. testSubtasks.push('connect');
  337. testSubtasks.push('saucelabs-qunit');
  338. }
  339. }
  340. grunt.registerTask('test', testSubtasks);
  341. // JS distribution task.
  342. grunt.registerTask('dist-js', ['babel:dev', 'concat', 'babel:dist', 'stamp', 'uglify:core']);
  343. grunt.registerTask('test-scss', ['scsslint:core']);
  344. // CSS distribution task.
  345. // Supported Compilers: sass (Ruby) and libsass.
  346. (function (sassCompilerName) {
  347. require('./grunt/bs-sass-compile/' + sassCompilerName + '.js')(grunt);
  348. })(process.env.TWBS_SASS || 'libsass');
  349. // grunt.registerTask('sass-compile', ['sass:core', 'sass:extras', 'sass:docs']);
  350. grunt.registerTask('sass-compile', ['sass:core', 'sass:extras', 'sass:docs']);
  351. grunt.registerTask('dist-css', ['sass-compile', 'exec:postcss', 'cssmin:core', 'cssmin:docs']);
  352. // Full distribution task.
  353. grunt.registerTask('dist', ['clean:dist', 'dist-css', 'dist-js']);
  354. // Default task.
  355. grunt.registerTask('default', ['clean:dist', 'test']);
  356. // Docs task.
  357. grunt.registerTask('docs-css', ['cssmin:docs', 'exec:postcss-docs']);
  358. grunt.registerTask('lint-docs-css', ['scsslint:docs']);
  359. grunt.registerTask('docs-js', ['uglify:docsJs']);
  360. grunt.registerTask('docs', ['lint-docs-css', 'docs-css', 'docs-js', 'clean:docs', 'copy:docs']);
  361. grunt.registerTask('docs-github', ['jekyll:github']);
  362. grunt.registerTask('prep-release', ['dist', 'docs', 'docs-github', 'compress']);
  363. // Publish to GitHub
  364. grunt.registerTask('publish', ['buildcontrol:pages']);
  365. };