Gruntfile.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*!
  2. * Bootstrap's Gruntfile
  3. * https://getbootstrap.com/
  4. * Copyright 2013-2019 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. core: {
  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: true,
  117. mangle: true,
  118. ie8: true,
  119. output: {
  120. comments: /^!|@preserve|@license|@cc_on/i
  121. }
  122. },
  123. core: {
  124. src: '<%= concat.core.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. docs: {
  132. src: configBridge.paths.docsJs,
  133. dest: 'docs/assets/js/docs.min.js'
  134. }
  135. },
  136. less: {
  137. options: {
  138. ieCompat: true,
  139. strictMath: true,
  140. sourceMap: true,
  141. outputSourceFiles: true
  142. },
  143. core: {
  144. options: {
  145. sourceMapURL: '<%= pkg.name %>.css.map',
  146. sourceMapFilename: 'dist/css/<%= pkg.name %>.css.map'
  147. },
  148. src: 'less/bootstrap.less',
  149. dest: 'dist/css/<%= pkg.name %>.css'
  150. },
  151. theme: {
  152. options: {
  153. sourceMapURL: '<%= pkg.name %>-theme.css.map',
  154. sourceMapFilename: 'dist/css/<%= pkg.name %>-theme.css.map'
  155. },
  156. src: 'less/theme.less',
  157. dest: 'dist/css/<%= pkg.name %>-theme.css'
  158. },
  159. docs: {
  160. options: {
  161. sourceMapURL: 'docs.css.map',
  162. sourceMapFilename: 'docs/assets/css/docs.css.map'
  163. },
  164. src: 'docs/assets/less/docs.less',
  165. dest: 'docs/assets/css/docs.css'
  166. },
  167. docsIe: {
  168. options: {
  169. sourceMap: false
  170. },
  171. src: 'docs/assets/less/ie10-viewport-bug-workaround.less',
  172. dest: 'docs/assets/css/ie10-viewport-bug-workaround.css'
  173. }
  174. },
  175. postcss: {
  176. options: {
  177. map: {
  178. inline: false,
  179. sourcesContent: true
  180. },
  181. processors: [
  182. require('autoprefixer')(configBridge.config.autoprefixer)
  183. ]
  184. },
  185. core: {
  186. src: 'dist/css/<%= pkg.name %>.css'
  187. },
  188. theme: {
  189. src: 'dist/css/<%= pkg.name %>-theme.css'
  190. },
  191. docs: {
  192. src: 'docs/assets/css/docs.css'
  193. },
  194. examples: {
  195. options: {
  196. map: false
  197. },
  198. expand: true,
  199. cwd: 'docs/examples/',
  200. src: ['**/*.css'],
  201. dest: 'docs/examples/'
  202. }
  203. },
  204. stylelint: {
  205. options: {
  206. configFile: 'grunt/.stylelintrc',
  207. reportNeedlessDisables: false
  208. },
  209. dist: [
  210. 'less/**/*.less'
  211. ],
  212. docs: [
  213. 'docs/assets/less/**/*.less'
  214. ],
  215. examples: [
  216. 'docs/examples/**/*.css'
  217. ]
  218. },
  219. cssmin: {
  220. options: {
  221. compatibility: 'ie8',
  222. sourceMap: true,
  223. sourceMapInlineSources: true,
  224. level: {
  225. 1: {
  226. specialComments: 'all'
  227. }
  228. }
  229. },
  230. core: {
  231. src: 'dist/css/<%= pkg.name %>.css',
  232. dest: 'dist/css/<%= pkg.name %>.min.css'
  233. },
  234. theme: {
  235. src: 'dist/css/<%= pkg.name %>-theme.css',
  236. dest: 'dist/css/<%= pkg.name %>-theme.min.css'
  237. },
  238. docs: {
  239. src: 'docs/assets/css/docs.css',
  240. dest: 'docs/assets/css/docs.min.css'
  241. }
  242. },
  243. copy: {
  244. fonts: {
  245. expand: true,
  246. src: 'fonts/**',
  247. dest: 'dist/'
  248. },
  249. docs: {
  250. expand: true,
  251. cwd: 'dist/',
  252. src: [
  253. '**/*'
  254. ],
  255. dest: 'docs/dist/'
  256. }
  257. },
  258. connect: {
  259. server: {
  260. options: {
  261. port: 3000,
  262. base: '.'
  263. }
  264. }
  265. },
  266. jekyll: {
  267. options: {
  268. bundleExec: true,
  269. config: '_config.yml',
  270. incremental: false
  271. },
  272. docs: {},
  273. github: {
  274. options: {
  275. raw: 'github: true'
  276. }
  277. }
  278. },
  279. pug: {
  280. options: {
  281. pretty: true,
  282. data: getLessVarsData
  283. },
  284. customizerVars: {
  285. src: 'docs/_pug/customizer-variables.pug',
  286. dest: 'docs/_includes/customizer-variables.html'
  287. },
  288. customizerNav: {
  289. src: 'docs/_pug/customizer-nav.pug',
  290. dest: 'docs/_includes/nav/customize.html'
  291. }
  292. },
  293. htmllint: {
  294. options: {
  295. ignore: [
  296. 'Element "img" is missing required attribute "src".'
  297. ],
  298. noLangDetect: true
  299. },
  300. src: ['_gh_pages/**/*.html', 'js/tests/**/*.html']
  301. },
  302. watch: {
  303. src: {
  304. files: '<%= jshint.core.src %>',
  305. tasks: ['jshint:core', 'exec:karma', 'concat']
  306. },
  307. test: {
  308. files: '<%= jshint.test.src %>',
  309. tasks: ['jshint:test', 'exec:karma']
  310. },
  311. less: {
  312. files: 'less/**/*.less',
  313. tasks: ['less', 'copy']
  314. },
  315. docs: {
  316. files: 'docs/assets/less/**/*.less',
  317. tasks: ['less']
  318. }
  319. },
  320. exec: {
  321. browserstack: {
  322. command: 'cross-env BROWSER=true karma start grunt/karma.conf.js'
  323. },
  324. karma: {
  325. command: 'karma start grunt/karma.conf.js'
  326. }
  327. }
  328. });
  329. // These plugins provide necessary tasks.
  330. require('load-grunt-tasks')(grunt, { scope: 'devDependencies' });
  331. require('time-grunt')(grunt);
  332. // Docs HTML validation task
  333. grunt.registerTask('validate-html', ['jekyll:docs', 'htmllint']);
  334. var runSubset = function (subset) {
  335. return !process.env.TWBS_TEST || process.env.TWBS_TEST === subset;
  336. };
  337. var isUndefOrNonZero = function (val) {
  338. return typeof val === 'undefined' || val !== '0';
  339. };
  340. // Test task.
  341. var testSubtasks = [];
  342. // Skip core tests if running a different subset of the test suite
  343. if (runSubset('core')) {
  344. testSubtasks = testSubtasks.concat(['dist-css', 'dist-js', 'stylelint:dist', 'test-js', 'docs']);
  345. }
  346. // Skip HTML validation if running a different subset of the test suite
  347. if (runSubset('validate-html') &&
  348. // Skip HTML5 validator on Travis when [skip validator] is in the commit message
  349. isUndefOrNonZero(process.env.TWBS_DO_VALIDATOR)) {
  350. testSubtasks.push('validate-html');
  351. }
  352. // Only run BrowserStack tests if there's a BrowserStack access key
  353. if (typeof process.env.BROWSER_STACK_USERNAME !== 'undefined' &&
  354. // Skip BrowserStack if running a different subset of the test suite
  355. runSubset('browserstack') &&
  356. // Skip BrowserStack on Travis when [skip browserstack] is in the commit message
  357. isUndefOrNonZero(process.env.TWBS_DO_BROWSERSTACK)) {
  358. testSubtasks.push('exec:browserstack');
  359. }
  360. grunt.registerTask('test', testSubtasks);
  361. grunt.registerTask('test-js', ['jshint:core', 'jshint:test', 'jshint:grunt', 'jscs:core', 'jscs:test', 'jscs:grunt', 'exec:karma']);
  362. // JS distribution task.
  363. grunt.registerTask('dist-js', ['concat', 'uglify:core', 'commonjs']);
  364. // CSS distribution task.
  365. grunt.registerTask('dist-css', ['less:core', 'less:theme', 'postcss:core', 'postcss:theme', 'cssmin:core', 'cssmin:theme']);
  366. // Full distribution task.
  367. grunt.registerTask('dist', ['clean:dist', 'dist-css', 'copy:fonts', 'dist-js']);
  368. // Default task.
  369. grunt.registerTask('default', ['clean:dist', 'copy:fonts', 'test']);
  370. grunt.registerTask('build-glyphicons-data', function () {
  371. generateGlyphiconsData.call(this, grunt);
  372. });
  373. // task for building customizer
  374. grunt.registerTask('build-customizer', ['build-customizer-html', 'build-raw-files']);
  375. grunt.registerTask('build-customizer-html', 'pug');
  376. grunt.registerTask('build-raw-files', 'Add scripts/less files to customizer.', function () {
  377. var banner = grunt.template.process('<%= banner %>');
  378. generateRawFiles(grunt, banner);
  379. });
  380. grunt.registerTask('commonjs', 'Generate CommonJS entrypoint module in dist dir.', function () {
  381. var srcFiles = grunt.config.get('concat.core.src');
  382. var destFilepath = 'dist/js/npm.js';
  383. generateCommonJSModule(grunt, srcFiles, destFilepath);
  384. });
  385. // Docs task.
  386. grunt.registerTask('docs-css', ['less:docs', 'less:docsIe', 'postcss:docs', 'postcss:examples', 'cssmin:docs']);
  387. grunt.registerTask('lint-docs-css', ['stylelint:docs', 'stylelint:examples']);
  388. grunt.registerTask('docs-js', ['uglify:docs', 'uglify:customize']);
  389. grunt.registerTask('lint-docs-js', ['jshint:assets', 'jscs:assets']);
  390. grunt.registerTask('docs', ['docs-css', 'lint-docs-css', 'docs-js', 'lint-docs-js', 'clean:docs', 'copy:docs', 'build-glyphicons-data', 'build-customizer']);
  391. grunt.registerTask('prep-release', ['dist', 'docs', 'jekyll:github']);
  392. };