Gruntfile.js 12.1 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
21
  var generateGlyphiconsData = require('./grunt/bs-glyphicons-data-generator.js');
  var BsLessdocParser = require('./grunt/bs-lessdoc-parser.js');
22
  var generateRawFiles = require('./grunt/bs-raw-files-generator.js');
Mark Otto's avatar
Mark Otto committed
23
  var updateShrinkwrap = require('./grunt/shrinkwrap.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
    jqueryCheck: 'if (typeof jQuery === \'undefined\') { throw new Error(\'Bootstrap\\\'s JavaScript requires jQuery\') }\n\n',
36
37
38

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

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

Chris Rebert's avatar
Chris Rebert committed
63
64
    jscs: {
      options: {
XhmikosR's avatar
XhmikosR committed
65
        config: 'js/.jscsrc'
Chris Rebert's avatar
Chris Rebert committed
66
      },
Chris Rebert's avatar
Chris Rebert committed
67
      grunt: {
XhmikosR's avatar
XhmikosR committed
68
        options: {
XhmikosR's avatar
XhmikosR committed
69
70
          requireCamelCaseOrUpperCaseIdentifiers: null,
          requireParenthesesAroundIIFE: true
XhmikosR's avatar
XhmikosR committed
71
        },
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
113
    uglify: {
      bootstrap: {
114
        options: {
115
          banner: '<%= banner %>'
116
        },
117
        src: '<%= concat.bootstrap.dest %>',
118
        dest: 'dist/js/<%= pkg.name %>.min.js'
XhmikosR's avatar
XhmikosR committed
119
120
      },
      customize: {
121
        options: {
122
          preserveComments: 'some'
123
        },
XhmikosR's avatar
XhmikosR committed
124
        src: [
125
126
127
128
129
          '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',
130
          'docs/assets/js/raw-files.min.js',
131
          'docs/assets/js/_src/customizer.js'
XhmikosR's avatar
XhmikosR committed
132
        ],
133
        dest: 'docs/assets/js/customize.min.js'
134
135
136
      },
      docsJs: {
        options: {
137
          preserveComments: 'some'
138
139
        },
        src: [
140
141
          'docs/assets/js/_vendor/holder.js',
          'docs/assets/js/_src/application.js'
142
143
        ],
        dest: 'docs/assets/js/docs.min.js'
144
145
146
      }
    },

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

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

Bas Bosman's avatar
Bas Bosman committed
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
    autoprefixer: {
      options: {
        browsers: ['last 2 versions', 'ie 8', 'ie 9', 'android 2.3', 'android 4', 'opera 12']
      },
      core: {
        options: {
          map: true
        },
        src: 'dist/css/<%= pkg.name %>.css'
      },
      theme: {
        options: {
          map: true
        },
        src: 'dist/css/<%= pkg.name %>-theme.css'
      },
      docs: {
198
        src: '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
212
213
214
215
216
217
218
219
220
    csslint: {
      options: {
        csslintrc: 'less/.csslintrc'
      },
      src: [
        '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
        compatibility: 'ie8',
Mark Otto's avatar
Mark Otto committed
231
        keepSpecialComments: '*'
XhmikosR's avatar
XhmikosR committed
232
      },
233
234
235
236
237
238
      core: {
        files: {
          'dist/css/<%= pkg.name %>.min.css': 'dist/css/<%= pkg.name %>.css',
          'dist/css/<%= pkg.name %>-theme.min.css': 'dist/css/<%= pkg.name %>-theme.css',
        }
      },
XhmikosR's avatar
XhmikosR committed
239
      docs: {
XhmikosR's avatar
XhmikosR committed
240
        src: [
241
242
          'docs/assets/css/_src/docs.css',
          'docs/assets/css/_src/pygments-manni.css'
XhmikosR's avatar
XhmikosR committed
243
        ],
244
        dest: 'docs/assets/css/docs.min.css'
XhmikosR's avatar
XhmikosR committed
245
246
247
      }
    },

248
    usebanner: {
XhmikosR's avatar
XhmikosR committed
249
250
251
252
253
254
      options: {
        position: 'top',
        banner: '<%= banner %>'
      },
      files: {
        src: 'dist/css/*.css'
255
256
257
      }
    },

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

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

299
300
301
302
303
    connect: {
      server: {
        options: {
          port: 3000,
          base: '.'
304
        }
305
306
307
      }
    },

308
309
310
311
    jekyll: {
      docs: {}
    },

312
313
314
315
316
317
318
319
320
321
322
323
    jade: {
      compile: {
        options: {
          pretty: true,
          data: 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()};
          }
        },
        files: {
324
325
          'docs/_includes/customizer-variables.html': 'docs/_jade/customizer-variables.jade',
          'docs/_includes/nav/customize.html': 'docs/_jade/customizer-nav.jade'
326
327
328
329
        }
      }
    },

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

346
347
348
349
350
351
352
353
354
    watch: {
      src: {
        files: '<%= jshint.src.src %>',
        tasks: ['jshint:src', 'qunit']
      },
      test: {
        files: '<%= jshint.test.src %>',
        tasks: ['jshint:test', 'qunit']
      },
355
      less: {
356
        files: 'less/*.less',
357
        tasks: 'less'
358
      }
359
360
361
362
363
    },

    sed: {
      versionNumber: {
        pattern: (function () {
Chris Rebert's avatar
Chris Rebert committed
364
365
          var old = grunt.option('oldver');
          return old ? RegExp.quote(old) : old;
366
367
368
369
        })(),
        replacement: grunt.option('newver'),
        recursive: true
      }
370
371
372
373
374
375
    },

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

    exec: {
      npmUpdate: {
        command: 'npm update'
      },
Mark Otto's avatar
Mark Otto committed
387
388
389
      npmShrinkWrap: {
        command: 'npm shrinkwrap --dev'
      }
390
391
    }
  });
392
393


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

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

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

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

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

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

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

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

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

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

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

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

  // Task for updating the npm packages used by the Travis build.
Mark Otto's avatar
Mark Otto committed
463
464
  grunt.registerTask('update-shrinkwrap', ['exec:npmUpdate', 'exec:npmShrinkWrap', '_update-shrinkwrap']);
  grunt.registerTask('_update-shrinkwrap', function () { updateShrinkwrap.call(this, grunt); });
465
};