Gruntfile.js 14.6 KB
Newer Older
Chris Rebert's avatar
Chris Rebert committed
1
2
3
/*!
 * Bootstrap's Gruntfile
 * http://getbootstrap.com
Zlatan Vasović's avatar
Zlatan Vasović committed
4
 * Copyright 2013-2015 Twitter, Inc.
Chris Rebert's avatar
Chris Rebert committed
5
6
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
 */
7

Chris Rebert's avatar
Chris Rebert committed
8
module.exports = function (grunt) {
XhmikosR's avatar
XhmikosR committed
9
  'use strict';
10

11
12
13
  // Force use of Unix newlines
  grunt.util.linefeed = '\n';

Zlatan Vasović's avatar
Zlatan Vasović committed
14
  RegExp.quote = function (string) {
Chris Rebert's avatar
Chris Rebert committed
15
16
    return string.replace(/[-\\^$*+?.()|[\]{}]/g, '\\$&');
  };
17

Chris Rebert's avatar
Chris Rebert committed
18
  var fs = require('fs');
19
  var path = require('path');
20
  var npmShrinkwrap = require('npm-shrinkwrap');
21
  var generateGlyphiconsData = require('./grunt/bs-glyphicons-data-generator.js');
22
  var BsLessdocParser = require('./grunt/bs-lessdoc-parser.js');
23
24
25
26
27
28
  var getLessVarsData = function () {
    var filePath = path.join(__dirname, 'less/variables.less');
    var fileContent = fs.readFileSync(filePath, { encoding: 'utf8' });
    var parser = new BsLessdocParser(fileContent);
    return { sections: parser.parseFile() };
  };
29
  var generateRawFiles = require('./grunt/bs-raw-files-generator.js');
30
  var generateCommonJSModule = require('./grunt/bs-commonjs-generator.js');
31
32
33
34
35
36
37
  var configBridge = grunt.file.readJSON('./grunt/configBridge.json', { encoding: 'utf8' });

  Object.keys(configBridge.paths).forEach(function (key) {
    configBridge.paths[key].forEach(function (val, i, arr) {
      arr[i] = path.join('./docs/assets', val);
    });
  });
38

39
40
41
42
43
  // Project configuration.
  grunt.initConfig({

    // Metadata.
    pkg: grunt.file.readJSON('package.json'),
44
    banner: '/*!\n' +
XhmikosR's avatar
XhmikosR committed
45
46
            ' * Bootstrap v<%= pkg.version %> (<%= pkg.homepage %>)\n' +
            ' * Copyright 2011-<%= grunt.template.today("yyyy") %> <%= pkg.author %>\n' +
47
            ' * Licensed under <%= pkg.license.type %> (<%= pkg.license.url %>)\n' +
XhmikosR's avatar
XhmikosR committed
48
            ' */\n',
49
50
    jqueryCheck: configBridge.config.jqueryCheck.join('\n'),
    jqueryVersionCheck: configBridge.config.jqueryVersionCheck.join('\n'),
51
52
53

    // Task configuration.
    clean: {
54
55
      dist: 'dist',
      docs: 'docs/dist'
56
57
58
59
60
61
    },

    jshint: {
      options: {
        jshintrc: 'js/.jshintrc'
      },
Chris Rebert's avatar
Chris Rebert committed
62
      grunt: {
63
        options: {
64
          jshintrc: 'grunt/.jshintrc'
65
        },
Zlatan Vasović's avatar
Zlatan Vasović committed
66
        src: ['Gruntfile.js', 'grunt/*.js']
67
      },
68
      core: {
69
        src: 'js/*.js'
70
71
      },
      test: {
XhmikosR's avatar
XhmikosR committed
72
73
74
        options: {
          jshintrc: 'js/tests/unit/.jshintrc'
        },
75
        src: 'js/tests/unit/*.js'
76
77
      },
      assets: {
78
        src: ['docs/assets/js/src/*.js', 'docs/assets/js/*.js', '!docs/assets/js/*.min.js']
79
80
      }
    },
81

Chris Rebert's avatar
Chris Rebert committed
82
83
    jscs: {
      options: {
XhmikosR's avatar
XhmikosR committed
84
        config: 'js/.jscsrc'
Chris Rebert's avatar
Chris Rebert committed
85
      },
Chris Rebert's avatar
Chris Rebert committed
86
      grunt: {
87
        src: '<%= jshint.grunt.src %>'
Chris Rebert's avatar
Chris Rebert committed
88
      },
89
90
      core: {
        src: '<%= jshint.core.src %>'
Chris Rebert's avatar
Chris Rebert committed
91
92
      },
      test: {
93
        src: '<%= jshint.test.src %>'
94
95
      },
      assets: {
96
97
98
        options: {
          requireCamelCaseOrUpperCaseIdentifiers: null
        },
99
        src: '<%= jshint.assets.src %>'
Chris Rebert's avatar
Chris Rebert committed
100
101
102
      }
    },

103
104
    concat: {
      options: {
105
        banner: '<%= banner %>\n<%= jqueryCheck %>\n<%= jqueryVersionCheck %>',
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
        stripBanners: false
      },
      bootstrap: {
        src: [
          'js/transition.js',
          'js/alert.js',
          'js/button.js',
          'js/carousel.js',
          'js/collapse.js',
          'js/dropdown.js',
          'js/modal.js',
          'js/tooltip.js',
          'js/popover.js',
          'js/scrollspy.js',
          'js/tab.js',
          'js/affix.js'
        ],
        dest: 'dist/js/<%= pkg.name %>.js'
      }
    },
126

127
    uglify: {
128
      options: {
XhmikosR's avatar
XhmikosR committed
129
130
131
132
        compress: {
          warnings: false
        },
        mangle: true,
XhmikosR's avatar
XhmikosR committed
133
        preserveComments: 'some'
134
      },
135
      core: {
136
        src: '<%= concat.bootstrap.dest %>',
137
        dest: 'dist/js/<%= pkg.name %>.min.js'
XhmikosR's avatar
XhmikosR committed
138
139
      },
      customize: {
140
        src: configBridge.paths.customizerJs,
141
        dest: 'docs/assets/js/customize.min.js'
142
143
      },
      docsJs: {
144
        src: configBridge.paths.docsJs,
145
        dest: 'docs/assets/js/docs.min.js'
146
147
148
      }
    },

Mark Otto's avatar
Mark Otto committed
149
150
151
152
153
154
155
    qunit: {
      options: {
        inject: 'js/tests/unit/phantom.js'
      },
      files: 'js/tests/index.html'
    },

156
    less: {
157
158
      compileCore: {
        options: {
159
          strictMath: true,
160
161
162
163
164
          sourceMap: true,
          outputSourceFiles: true,
          sourceMapURL: '<%= pkg.name %>.css.map',
          sourceMapFilename: 'dist/css/<%= pkg.name %>.css.map'
        },
165
166
        src: 'less/bootstrap.less',
        dest: 'dist/css/<%= pkg.name %>.css'
167
168
169
      },
      compileTheme: {
        options: {
170
          strictMath: true,
171
172
173
174
175
          sourceMap: true,
          outputSourceFiles: true,
          sourceMapURL: '<%= pkg.name %>-theme.css.map',
          sourceMapFilename: 'dist/css/<%= pkg.name %>-theme.css.map'
        },
176
177
        src: 'less/theme.less',
        dest: 'dist/css/<%= pkg.name %>-theme.css'
178
179
180
      }
    },

Bas Bosman's avatar
Bas Bosman committed
181
182
    autoprefixer: {
      options: {
183
        browsers: configBridge.config.autoprefixerBrowsers
Bas Bosman's avatar
Bas Bosman committed
184
185
186
187
188
189
190
191
192
193
194
195
196
197
      },
      core: {
        options: {
          map: true
        },
        src: 'dist/css/<%= pkg.name %>.css'
      },
      theme: {
        options: {
          map: true
        },
        src: 'dist/css/<%= pkg.name %>-theme.css'
      },
      docs: {
XhmikosR's avatar
XhmikosR committed
198
        src: ['docs/assets/css/anchor.css', 'docs/assets/css/src/docs.css']
Bas Bosman's avatar
Bas Bosman committed
199
200
201
202
203
204
205
206
207
      },
      examples: {
        expand: true,
        cwd: 'docs/examples/',
        src: ['**/*.css'],
        dest: 'docs/examples/'
      }
    },

Mark Otto's avatar
Mark Otto committed
208
209
210
211
    csslint: {
      options: {
        csslintrc: 'less/.csslintrc'
      },
212
      dist: [
Mark Otto's avatar
Mark Otto committed
213
214
215
216
217
218
219
220
        'dist/css/bootstrap.css',
        'dist/css/bootstrap-theme.css'
      ],
      examples: [
        'docs/examples/**/*.css'
      ],
      docs: {
        options: {
221
          ids: false,
Mark Otto's avatar
Mark Otto committed
222
223
          'overqualified-elements': false
        },
224
        src: 'docs/assets/css/src/docs.css'
Mark Otto's avatar
Mark Otto committed
225
226
227
      }
    },

XhmikosR's avatar
XhmikosR committed
228
    cssmin: {
XhmikosR's avatar
XhmikosR committed
229
      options: {
230
231
        // TODO: disable `zeroUnits` optimization once clean-css 3.2 is released
        //    and then simplify the fix for https://github.com/twbs/bootstrap/issues/14837 accordingly
232
        compatibility: 'ie8',
233
        keepSpecialComments: '*',
234
        advanced: false
XhmikosR's avatar
XhmikosR committed
235
      },
236
237
238
239
240
241
242
      minifyCore: {
        src: 'dist/css/<%= pkg.name %>.css',
        dest: 'dist/css/<%= pkg.name %>.min.css'
      },
      minifyTheme: {
        src: 'dist/css/<%= pkg.name %>-theme.css',
        dest: 'dist/css/<%= pkg.name %>-theme.min.css'
243
      },
XhmikosR's avatar
XhmikosR committed
244
      docs: {
XhmikosR's avatar
XhmikosR committed
245
        src: [
XhmikosR's avatar
XhmikosR committed
246
247
248
249
          'docs/assets/css/src/pygments-manni.css',
          'docs/assets/css/src/anchor.css',
          'docs/assets/css/src/docs.css'

XhmikosR's avatar
XhmikosR committed
250
        ],
251
        dest: 'docs/assets/css/docs.min.css'
XhmikosR's avatar
XhmikosR committed
252
253
254
      }
    },

255
    usebanner: {
XhmikosR's avatar
XhmikosR committed
256
257
258
259
260
261
      options: {
        position: 'top',
        banner: '<%= banner %>'
      },
      files: {
        src: 'dist/css/*.css'
262
263
264
      }
    },

265
    csscomb: {
266
267
268
269
      options: {
        config: 'less/.csscomb.json'
      },
      dist: {
XhmikosR's avatar
XhmikosR committed
270
271
272
273
        expand: true,
        cwd: 'dist/css/',
        src: ['*.css', '!*.min.css'],
        dest: 'dist/css/'
274
275
      },
      examples: {
Chris Rebert's avatar
Chris Rebert committed
276
277
        expand: true,
        cwd: 'docs/examples/',
Zlatan Vasović's avatar
Zlatan Vasović committed
278
        src: '**/*.css',
Chris Rebert's avatar
Chris Rebert committed
279
        dest: 'docs/examples/'
XhmikosR's avatar
XhmikosR committed
280
281
      },
      docs: {
282
283
        src: 'docs/assets/css/src/docs.css',
        dest: 'docs/assets/css/src/docs.css'
284
285
286
      }
    },

Mark Otto's avatar
Mark Otto committed
287
288
    copy: {
      fonts: {
XhmikosR's avatar
XhmikosR committed
289
        expand: true,
290
        src: 'fonts/*',
Mark Otto's avatar
Mark Otto committed
291
        dest: 'dist/'
292
      },
Mark Otto's avatar
Mark Otto committed
293
      docs: {
XhmikosR's avatar
XhmikosR committed
294
295
296
297
298
299
        expand: true,
        cwd: 'dist/',
        src: [
          '**/*'
        ],
        dest: 'docs/dist/'
Mark Otto's avatar
Mark Otto committed
300
301
302
      }
    },

303
304
305
306
307
    connect: {
      server: {
        options: {
          port: 3000,
          base: '.'
308
        }
309
310
311
      }
    },

312
    jekyll: {
313
314
315
316
317
318
319
320
321
      options: {
        config: '_config.yml'
      },
      docs: {},
      github: {
        options: {
          raw: 'github: true'
        }
      }
322
323
    },

XhmikosR's avatar
XhmikosR committed
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
    htmlmin: {
      dist: {
        options: {
          collapseWhitespace: true,
          conservativeCollapse: true,
          minifyCSS: true,
          minifyJS: true,
          removeAttributeQuotes: true,
          removeComments: true
        },
        expand: true,
        cwd: '_gh_pages',
        dest: '_gh_pages',
        src: [
          '**/*.html',
          '!examples/**/*.html'
        ]
      }
    },

344
    jade: {
345
346
347
348
349
350
351
352
353
354
355
      options: {
        pretty: true,
        data: getLessVarsData
      },
      customizerVars: {
        src: 'docs/_jade/customizer-variables.jade',
        dest: 'docs/_includes/customizer-variables.html'
      },
      customizerNav: {
        src: 'docs/_jade/customizer-nav.jade',
        dest: 'docs/_includes/nav/customize.html'
356
357
358
      }
    },

359
    htmllint: {
360
      options: {
361
362
363
364
        ignore: [
          'Attribute "autocomplete" not allowed on element "button" at this point.',
          'Attribute "autocomplete" not allowed on element "input" at this point.',
          'Element "img" is missing required attribute "src".'
365
        ]
366
      },
367
      src: '_gh_pages/**/*.html'
368
369
    },

370
371
    watch: {
      src: {
372
        files: '<%= jshint.core.src %>',
Zach Leatherman's avatar
Zach Leatherman committed
373
        tasks: ['jshint:core', 'qunit', 'concat']
374
375
376
377
378
      },
      test: {
        files: '<%= jshint.test.src %>',
        tasks: ['jshint:test', 'qunit']
      },
379
      less: {
380
        files: 'less/**/*.less',
381
        tasks: 'less'
382
      }
383
384
385
386
387
    },

    sed: {
      versionNumber: {
        pattern: (function () {
Chris Rebert's avatar
Chris Rebert committed
388
389
          var old = grunt.option('oldver');
          return old ? RegExp.quote(old) : old;
390
391
392
393
        })(),
        replacement: grunt.option('newver'),
        recursive: true
      }
394
395
396
397
398
399
    },

    'saucelabs-qunit': {
      all: {
        options: {
          build: process.env.TRAVIS_JOB_ID,
400
          throttled: 10,
401
          maxRetries: 3,
402
          maxPollRetries: 4,
403
          urls: ['http://127.0.0.1:3000/js/tests/index.html?hidepassed'],
404
          browsers: grunt.file.readYAML('grunt/sauce_browsers.yml')
405
406
        }
      }
Chris Rebert's avatar
Chris Rebert committed
407
408
409
410
411
    },

    exec: {
      npmUpdate: {
        command: 'npm update'
Mark Otto's avatar
Mark Otto committed
412
      }
XhmikosR's avatar
XhmikosR committed
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
    },

    compress: {
      main: {
        options: {
          archive: 'bootstrap-<%= pkg.version %>-dist.zip',
          mode: 'zip',
          level: 9,
          pretty: true
        },
        files: [
          {
            expand: true,
            cwd: 'dist/',
            src: ['**'],
            dest: 'bootstrap-<%= pkg.version %>-dist'
          }
        ]
      }
432
    }
XhmikosR's avatar
XhmikosR committed
433

434
  });
435
436


437
  // These plugins provide necessary tasks.
XhmikosR's avatar
XhmikosR committed
438
  require('load-grunt-tasks')(grunt, { scope: 'devDependencies' });
XhmikosR's avatar
XhmikosR committed
439
  require('time-grunt')(grunt);
440

441
  // Docs HTML validation task
442
  grunt.registerTask('validate-html', ['jekyll:docs', 'htmllint']);
443

444
445
446
  var runSubset = function (subset) {
    return !process.env.TWBS_TEST || process.env.TWBS_TEST === subset;
  };
447
448
449
  var isUndefOrNonZero = function (val) {
    return val === undefined || val !== '0';
  };
450

451
  // Test task.
452
453
  var testSubtasks = [];
  // Skip core tests if running a different subset of the test suite
454
455
456
  if (runSubset('core') &&
      // Skip core tests if this is a Savage build
      process.env.TRAVIS_REPO_SLUG !== 'twbs-savage/bootstrap') {
Chris Rebert's avatar
Chris Rebert committed
457
    testSubtasks = testSubtasks.concat(['dist-css', 'dist-js', 'csslint:dist', 'test-js', 'docs']);
458
459
  }
  // Skip HTML validation if running a different subset of the test suite
460
461
462
  if (runSubset('validate-html') &&
      // Skip HTML5 validator on Travis when [skip validator] is in the commit message
      isUndefOrNonZero(process.env.TWBS_DO_VALIDATOR)) {
463
464
    testSubtasks.push('validate-html');
  }
465
  // Only run Sauce Labs tests if there's a Sauce access key
Chris Rebert's avatar
Chris Rebert committed
466
  if (typeof process.env.SAUCE_ACCESS_KEY !== 'undefined' &&
467
      // Skip Sauce if running a different subset of the test suite
468
469
470
      runSubset('sauce-js-unit') &&
      // Skip Sauce on Travis when [skip sauce] is in the commit message
      isUndefOrNonZero(process.env.TWBS_DO_SAUCE)) {
471
472
    testSubtasks.push('connect');
    testSubtasks.push('saucelabs-qunit');
473
474
  }
  grunt.registerTask('test', testSubtasks);
Chris Rebert's avatar
Chris Rebert committed
475
  grunt.registerTask('test-js', ['jshint:core', 'jshint:test', 'jshint:grunt', 'jscs:core', 'jscs:test', 'jscs:grunt', 'qunit']);
476

477
  // JS distribution task.
478
  grunt.registerTask('dist-js', ['concat', 'uglify:core', 'commonjs']);
479

480
  // CSS distribution task.
481
  grunt.registerTask('less-compile', ['less:compileCore', 'less:compileTheme']);
482
  grunt.registerTask('dist-css', ['less-compile', 'autoprefixer:core', 'autoprefixer:theme', 'usebanner', 'csscomb:dist', 'cssmin:minifyCore', 'cssmin:minifyTheme']);
Mark Otto's avatar
Mark Otto committed
483

484
  // Full distribution task.
485
  grunt.registerTask('dist', ['clean:dist', 'dist-css', 'copy:fonts', 'dist-js']);
486

487
  // Default task.
488
  grunt.registerTask('default', ['clean:dist', 'copy:fonts', 'test']);
489

490
491
492
  // Version numbering task.
  // grunt change-version-number --oldver=A.B.C --newver=X.Y.Z
  // This can be overzealous, so its changes should always be manually reviewed!
493
  grunt.registerTask('change-version-number', 'sed');
494

495
496
  grunt.registerTask('build-glyphicons-data', function () { generateGlyphiconsData.call(this, grunt); });

497
  // task for building customizer
498
499
  grunt.registerTask('build-customizer', ['build-customizer-html', 'build-raw-files']);
  grunt.registerTask('build-customizer-html', 'jade');
500
501
  grunt.registerTask('build-raw-files', 'Add scripts/less files to customizer.', function () {
    var banner = grunt.template.process('<%= banner %>');
502
    generateRawFiles(grunt, banner);
503
  });
Chris Rebert's avatar
Chris Rebert committed
504

505
  grunt.registerTask('commonjs', 'Generate CommonJS entrypoint module in dist dir.', function () {
506
507
508
    var srcFiles = grunt.config.get('concat.bootstrap.src');
    var destFilepath = 'dist/js/npm.js';
    generateCommonJSModule(grunt, srcFiles, destFilepath);
509
510
  });

511
512
513
514
515
  // Docs task.
  grunt.registerTask('docs-css', ['autoprefixer:docs', 'autoprefixer:examples', 'csscomb:docs', 'csscomb:examples', 'cssmin:docs']);
  grunt.registerTask('lint-docs-css', ['csslint:docs', 'csslint:examples']);
  grunt.registerTask('docs-js', ['uglify:docsJs', 'uglify:customize']);
  grunt.registerTask('lint-docs-js', ['jshint:assets', 'jscs:assets']);
516
  grunt.registerTask('docs', ['docs-css', 'lint-docs-css', 'docs-js', 'lint-docs-js', 'clean:docs', 'copy:docs', 'build-glyphicons-data', 'build-customizer']);
517

XhmikosR's avatar
XhmikosR committed
518
  grunt.registerTask('prep-release', ['jekyll:github', 'htmlmin', 'compress']);
519

520
521
522
523
524
525
526
  // Task for updating the cached npm packages used by the Travis build (which are controlled by test-infra/npm-shrinkwrap.json).
  // This task should be run and the updated file should be committed whenever Bootstrap's dependencies change.
  grunt.registerTask('update-shrinkwrap', ['exec:npmUpdate', '_update-shrinkwrap']);
  grunt.registerTask('_update-shrinkwrap', function () {
    var done = this.async();
    npmShrinkwrap({ dev: true, dirname: __dirname }, function (err) {
      if (err) {
527
        grunt.fail.warn(err);
528
529
530
531
532
533
534
      }
      var dest = 'test-infra/npm-shrinkwrap.json';
      fs.renameSync('npm-shrinkwrap.json', dest);
      grunt.log.writeln('File ' + dest.cyan + ' updated.');
      done();
    });
  });
535
};