Gruntfile.js 12.6 KB
Newer Older
Chris Rebert's avatar
Chris Rebert committed
1
2
3
4
5
6
/*!
 * Bootstrap's Gruntfile
 * http://getbootstrap.com
 * Copyright 2013-2014 Twitter, Inc.
 * 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
22
  var generateGlyphiconsData = require('./grunt/bs-glyphicons-data-generator.js');
  var BsLessdocParser = require('./grunt/bs-lessdoc-parser.js');
23
  var generateRawFiles = require('./grunt/bs-raw-files-generator.js');
24

25
26
27
28
29
  // Project configuration.
  grunt.initConfig({

    // Metadata.
    pkg: grunt.file.readJSON('package.json'),
30
    banner: '/*!\n' +
XhmikosR's avatar
XhmikosR committed
31
32
            ' * Bootstrap v<%= pkg.version %> (<%= pkg.homepage %>)\n' +
            ' * Copyright 2011-<%= grunt.template.today("yyyy") %> <%= pkg.author %>\n' +
33
            ' * Licensed under <%= pkg.license.type %> (<%= pkg.license.url %>)\n' +
XhmikosR's avatar
XhmikosR committed
34
            ' */\n',
35
    // NOTE: This jqueryCheck code is duplicated in customizer.js; if making changes here, be sure to update the other copy too.
36
    jqueryCheck: 'if (typeof jQuery === \'undefined\') { throw new Error(\'Bootstrap\\\'s JavaScript requires jQuery\') }\n\n',
37
38
39

    // Task configuration.
    clean: {
Chris Rebert's avatar
Chris Rebert committed
40
      dist: ['dist', 'docs/dist']
41
42
43
44
45
46
    },

    jshint: {
      options: {
        jshintrc: 'js/.jshintrc'
      },
Chris Rebert's avatar
Chris Rebert committed
47
      grunt: {
48
        options: {
49
          jshintrc: 'grunt/.jshintrc'
50
        },
Zlatan Vasović's avatar
Zlatan Vasović committed
51
        src: ['Gruntfile.js', 'grunt/*.js']
52
53
      },
      src: {
54
        src: 'js/*.js'
55
56
      },
      test: {
XhmikosR's avatar
XhmikosR committed
57
58
59
        options: {
          jshintrc: 'js/tests/unit/.jshintrc'
        },
60
        src: 'js/tests/unit/*.js'
61
62
      },
      assets: {
63
        src: ['docs/assets/js/_src/*.js', 'docs/assets/js/*.js', '!docs/assets/js/*.min.js']
64
65
      }
    },
66

Chris Rebert's avatar
Chris Rebert committed
67
68
    jscs: {
      options: {
XhmikosR's avatar
XhmikosR committed
69
        config: 'js/.jscsrc'
Chris Rebert's avatar
Chris Rebert committed
70
      },
Chris Rebert's avatar
Chris Rebert committed
71
      grunt: {
72
        src: '<%= jshint.grunt.src %>'
Chris Rebert's avatar
Chris Rebert committed
73
74
      },
      src: {
75
        src: '<%= jshint.src.src %>'
Chris Rebert's avatar
Chris Rebert committed
76
77
      },
      test: {
78
        src: '<%= jshint.test.src %>'
79
80
      },
      assets: {
81
82
83
        options: {
          requireCamelCaseOrUpperCaseIdentifiers: null
        },
84
        src: '<%= jshint.assets.src %>'
Chris Rebert's avatar
Chris Rebert committed
85
86
87
      }
    },

88
89
    concat: {
      options: {
Zlatan Vasović's avatar
Zlatan Vasović committed
90
        banner: '<%= banner %>\n<%= jqueryCheck %>',
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
        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'
      }
    },
111

112
    uglify: {
113
114
115
      options: {
        preserveComments: 'some'
      },
116
      bootstrap: {
117
        src: '<%= concat.bootstrap.dest %>',
118
        dest: 'dist/js/<%= pkg.name %>.min.js'
XhmikosR's avatar
XhmikosR committed
119
120
121
      },
      customize: {
        src: [
122
123
124
125
126
          'docs/assets/js/_vendor/less.min.js',
          'docs/assets/js/_vendor/jszip.min.js',
          'docs/assets/js/_vendor/uglify.min.js',
          'docs/assets/js/_vendor/blob.js',
          'docs/assets/js/_vendor/filesaver.js',
127
          'docs/assets/js/raw-files.min.js',
128
          'docs/assets/js/_src/customizer.js'
XhmikosR's avatar
XhmikosR committed
129
        ],
130
        dest: 'docs/assets/js/customize.min.js'
131
132
133
      },
      docsJs: {
        src: [
134
          'docs/assets/js/_vendor/holder.js',
XhmikosR's avatar
XhmikosR committed
135
          'docs/assets/js/_vendor/ZeroClipboard.min.js',
136
          'docs/assets/js/_src/application.js'
137
138
        ],
        dest: 'docs/assets/js/docs.min.js'
139
140
141
      }
    },

Mark Otto's avatar
Mark Otto committed
142
143
144
145
146
147
148
    qunit: {
      options: {
        inject: 'js/tests/unit/phantom.js'
      },
      files: 'js/tests/index.html'
    },

149
    less: {
150
151
      compileCore: {
        options: {
152
          strictMath: true,
153
154
155
156
157
158
159
160
161
162
163
          sourceMap: true,
          outputSourceFiles: true,
          sourceMapURL: '<%= pkg.name %>.css.map',
          sourceMapFilename: 'dist/css/<%= pkg.name %>.css.map'
        },
        files: {
          'dist/css/<%= pkg.name %>.css': 'less/bootstrap.less'
        }
      },
      compileTheme: {
        options: {
164
          strictMath: true,
165
166
167
168
169
          sourceMap: true,
          outputSourceFiles: true,
          sourceMapURL: '<%= pkg.name %>-theme.css.map',
          sourceMapFilename: 'dist/css/<%= pkg.name %>-theme.css.map'
        },
170
171
172
        files: {
          'dist/css/<%= pkg.name %>-theme.css': 'less/theme.less'
        }
173
174
175
      }
    },

Bas Bosman's avatar
Bas Bosman committed
176
177
    autoprefixer: {
      options: {
Bas Bosman's avatar
Bas Bosman committed
178
179
180
181
182
183
184
185
186
187
        browsers: [
          'Android 2.3',
          'Android >= 4',
          'Chrome >= 20',
          'Firefox >= 24', // Firefox 24 is the latest ESR
          'Explorer >= 8',
          'iOS >= 6',
          'Opera >= 12',
          'Safari >= 6'
        ]
Bas Bosman's avatar
Bas Bosman committed
188
189
190
191
192
193
194
195
196
197
198
199
200
201
      },
      core: {
        options: {
          map: true
        },
        src: 'dist/css/<%= pkg.name %>.css'
      },
      theme: {
        options: {
          map: true
        },
        src: 'dist/css/<%= pkg.name %>-theme.css'
      },
      docs: {
202
        src: 'docs/assets/css/_src/docs.css'
Bas Bosman's avatar
Bas Bosman committed
203
204
205
206
207
208
209
210
211
      },
      examples: {
        expand: true,
        cwd: 'docs/examples/',
        src: ['**/*.css'],
        dest: 'docs/examples/'
      }
    },

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

XhmikosR's avatar
XhmikosR committed
232
    cssmin: {
XhmikosR's avatar
XhmikosR committed
233
      options: {
234
        compatibility: 'ie8',
235
236
        keepSpecialComments: '*',
        noAdvanced: true
XhmikosR's avatar
XhmikosR committed
237
      },
238
239
240
      core: {
        files: {
          'dist/css/<%= pkg.name %>.min.css': 'dist/css/<%= pkg.name %>.css',
XhmikosR's avatar
XhmikosR committed
241
          'dist/css/<%= pkg.name %>-theme.min.css': 'dist/css/<%= pkg.name %>-theme.css'
242
243
        }
      },
XhmikosR's avatar
XhmikosR committed
244
      docs: {
XhmikosR's avatar
XhmikosR committed
245
        src: [
246
247
          'docs/assets/css/_src/docs.css',
          'docs/assets/css/_src/pygments-manni.css'
XhmikosR's avatar
XhmikosR committed
248
        ],
249
        dest: 'docs/assets/css/docs.min.css'
XhmikosR's avatar
XhmikosR committed
250
251
252
      }
    },

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

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

Mark Otto's avatar
Mark Otto committed
286
287
288
    copy: {
      fonts: {
        expand: true,
289
        src: 'fonts/*',
Mark Otto's avatar
Mark Otto committed
290
        dest: 'dist/'
291
      },
Mark Otto's avatar
Mark Otto committed
292
      docs: {
293
        expand: true,
Mark Otto's avatar
Mark Otto committed
294
295
296
        cwd: './dist',
        src: [
          '{css,js}/*.min.*',
Chris Rebert's avatar
Chris Rebert committed
297
          'css/*.map',
Mark Otto's avatar
Mark Otto committed
298
299
300
          'fonts/*'
        ],
        dest: 'docs/dist'
Mark Otto's avatar
Mark Otto committed
301
302
303
      }
    },

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

313
314
315
316
    jekyll: {
      docs: {}
    },

317
318
319
320
321
322
    jade: {
      compile: {
        options: {
          pretty: true,
          data: function () {
            var filePath = path.join(__dirname, 'less/variables.less');
XhmikosR's avatar
XhmikosR committed
323
            var fileContent = fs.readFileSync(filePath, { encoding: 'utf8' });
324
            var parser = new BsLessdocParser(fileContent);
XhmikosR's avatar
XhmikosR committed
325
            return { sections: parser.parseFile() };
326
327
328
          }
        },
        files: {
329
330
          'docs/_includes/customizer-variables.html': 'docs/_jade/customizer-variables.jade',
          'docs/_includes/nav/customize.html': 'docs/_jade/customizer-nav.jade'
331
332
333
334
        }
      }
    },

335
336
    validation: {
      options: {
337
338
        charset: 'utf-8',
        doctype: 'HTML5',
339
        failHard: true,
340
341
        reset: true,
        relaxerror: [
XhmikosR's avatar
XhmikosR committed
342
343
          'Bad value X-UA-Compatible for attribute http-equiv on element meta.',
          'Element img is missing required attribute src.'
344
        ]
345
346
      },
      files: {
347
        src: '_gh_pages/**/*.html'
348
349
350
      }
    },

351
352
353
354
355
356
357
358
359
    watch: {
      src: {
        files: '<%= jshint.src.src %>',
        tasks: ['jshint:src', 'qunit']
      },
      test: {
        files: '<%= jshint.test.src %>',
        tasks: ['jshint:test', 'qunit']
      },
360
      less: {
361
        files: 'less/*.less',
362
        tasks: 'less'
363
      }
364
365
366
367
368
    },

    sed: {
      versionNumber: {
        pattern: (function () {
Chris Rebert's avatar
Chris Rebert committed
369
370
          var old = grunt.option('oldver');
          return old ? RegExp.quote(old) : old;
371
372
373
374
        })(),
        replacement: grunt.option('newver'),
        recursive: true
      }
375
376
377
378
379
380
    },

    'saucelabs-qunit': {
      all: {
        options: {
          build: process.env.TRAVIS_JOB_ID,
Chris Rebert's avatar
Chris Rebert committed
381
          concurrency: 10,
382
          urls: ['http://127.0.0.1:3000/js/tests/index.html'],
383
          browsers: grunt.file.readYAML('grunt/sauce_browsers.yml')
384
385
        }
      }
Chris Rebert's avatar
Chris Rebert committed
386
387
388
389
390
    },

    exec: {
      npmUpdate: {
        command: 'npm update'
Mark Otto's avatar
Mark Otto committed
391
      }
392
393
    }
  });
394
395


396
  // These plugins provide necessary tasks.
XhmikosR's avatar
XhmikosR committed
397
  require('load-grunt-tasks')(grunt, { scope: 'devDependencies' });
XhmikosR's avatar
XhmikosR committed
398
  require('time-grunt')(grunt);
399

400
  // Docs HTML validation task
401
  grunt.registerTask('validate-html', ['jekyll', 'validation']);
402

403
404
405
  var runSubset = function (subset) {
    return !process.env.TWBS_TEST || process.env.TWBS_TEST === subset;
  };
406
407
408
  var isUndefOrNonZero = function (val) {
    return val === undefined || val !== '0';
  };
409

410
  // Test task.
411
412
  var testSubtasks = [];
  // Skip core tests if running a different subset of the test suite
413
  if (runSubset('core')) {
414
    testSubtasks = testSubtasks.concat(['dist-css', 'csslint', 'jshint', 'jscs', 'qunit', 'build-customizer-html']);
415
416
  }
  // Skip HTML validation if running a different subset of the test suite
417
418
419
  if (runSubset('validate-html') &&
      // Skip HTML5 validator on Travis when [skip validator] is in the commit message
      isUndefOrNonZero(process.env.TWBS_DO_VALIDATOR)) {
420
421
    testSubtasks.push('validate-html');
  }
422
  // Only run Sauce Labs tests if there's a Sauce access key
Chris Rebert's avatar
Chris Rebert committed
423
  if (typeof process.env.SAUCE_ACCESS_KEY !== 'undefined' &&
424
      // Skip Sauce if running a different subset of the test suite
425
426
427
      runSubset('sauce-js-unit') &&
      // Skip Sauce on Travis when [skip sauce] is in the commit message
      isUndefOrNonZero(process.env.TWBS_DO_SAUCE)) {
428
429
    testSubtasks.push('connect');
    testSubtasks.push('saucelabs-qunit');
430
431
  }
  grunt.registerTask('test', testSubtasks);
432

433
434
  // JS distribution task.
  grunt.registerTask('dist-js', ['concat', 'uglify']);
435

436
  // CSS distribution task.
437
  grunt.registerTask('less-compile', ['less:compileCore', 'less:compileTheme']);
438
  grunt.registerTask('dist-css', ['less-compile', 'autoprefixer', 'usebanner', 'csscomb', 'cssmin']);
439

Mark Otto's avatar
derp    
Mark Otto committed
440
  // Docs distribution task.
441
  grunt.registerTask('dist-docs', 'copy:docs');
Mark Otto's avatar
Mark Otto committed
442

443
  // Full distribution task.
444
  grunt.registerTask('dist', ['clean', 'dist-css', 'copy:fonts', 'dist-js', 'dist-docs']);
445

446
  // Default task.
447
  grunt.registerTask('default', ['test', 'dist', 'build-glyphicons-data', 'build-customizer']);
448

449
450
451
  // 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!
452
  grunt.registerTask('change-version-number', 'sed');
453

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

456
  // task for building customizer
457
458
  grunt.registerTask('build-customizer', ['build-customizer-html', 'build-raw-files']);
  grunt.registerTask('build-customizer-html', 'jade');
459
460
  grunt.registerTask('build-raw-files', 'Add scripts/less files to customizer.', function () {
    var banner = grunt.template.process('<%= banner %>');
461
    generateRawFiles(grunt, banner);
462
  });
Chris Rebert's avatar
Chris Rebert committed
463

464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
  // 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) {
        grunt.fail.warn(err)
      }
      var dest = 'test-infra/npm-shrinkwrap.json';
      fs.renameSync('npm-shrinkwrap.json', dest);
      grunt.log.writeln('File ' + dest.cyan + ' updated.');
      done();
    });
  });
479
};