Gruntfile.js 11.8 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
      minify: {
        options: {
181
          cleancss: true
182
183
184
        },
        files: {
          'dist/css/<%= pkg.name %>.min.css': 'dist/css/<%= pkg.name %>.css',
Mark Otto's avatar
Mark Otto committed
185
          'dist/css/<%= pkg.name %>-theme.min.css': 'dist/css/<%= pkg.name %>-theme.css'
186
187
188
189
        }
      }
    },

Bas Bosman's avatar
Bas Bosman committed
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
    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: {
207
        src: 'docs/assets/css/_src/docs.css'
Bas Bosman's avatar
Bas Bosman committed
208
209
210
211
212
213
214
215
216
      },
      examples: {
        expand: true,
        cwd: 'docs/examples/',
        src: ['**/*.css'],
        dest: 'docs/examples/'
      }
    },

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

XhmikosR's avatar
XhmikosR committed
237
    cssmin: {
XhmikosR's avatar
XhmikosR committed
238
      options: {
239
        compatibility: 'ie8',
Mark Otto's avatar
Mark Otto committed
240
        keepSpecialComments: '*'
XhmikosR's avatar
XhmikosR committed
241
242
      },
      docs: {
XhmikosR's avatar
XhmikosR committed
243
        src: [
244
245
          'docs/assets/css/_src/docs.css',
          'docs/assets/css/_src/pygments-manni.css'
XhmikosR's avatar
XhmikosR committed
246
        ],
247
        dest: 'docs/assets/css/docs.min.css'
XhmikosR's avatar
XhmikosR committed
248
249
250
      }
    },

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

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

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

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

311
312
313
314
    jekyll: {
      docs: {}
    },

315
316
317
318
319
320
321
322
323
324
325
326
    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: {
327
328
          'docs/_includes/customizer-variables.html': 'docs/_jade/customizer-variables.jade',
          'docs/_includes/nav/customize.html': 'docs/_jade/customizer-nav.jade'
329
330
331
332
        }
      }
    },

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

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

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

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

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


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

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

404
  // Test task.
405
406
407
  var testSubtasks = [];
  // Skip core tests if running a different subset of the test suite
  if (!process.env.TWBS_TEST || process.env.TWBS_TEST === 'core') {
408
    testSubtasks = testSubtasks.concat(['dist-css', 'csslint', 'jshint', 'jscs', 'qunit', 'build-customizer-html']);
409
410
411
412
413
  }
  // Skip HTML validation if running a different subset of the test suite
  if (!process.env.TWBS_TEST || process.env.TWBS_TEST === 'validate-html') {
    testSubtasks.push('validate-html');
  }
414
  // Only run Sauce Labs tests if there's a Sauce access key
Chris Rebert's avatar
Chris Rebert committed
415
  if (typeof process.env.SAUCE_ACCESS_KEY !== 'undefined' &&
416
      // Skip Sauce if running a different subset of the test suite
Chris Rebert's avatar
Chris Rebert committed
417
      (!process.env.TWBS_TEST || process.env.TWBS_TEST === 'sauce-js-unit')) {
418
419
    testSubtasks.push('connect');
    testSubtasks.push('saucelabs-qunit');
420
421
  }
  grunt.registerTask('test', testSubtasks);
422

423
424
  // JS distribution task.
  grunt.registerTask('dist-js', ['concat', 'uglify']);
425

426
  // CSS distribution task.
427
  grunt.registerTask('less-compile', ['less:compileCore', 'less:compileTheme']);
Mark Otto's avatar
Mark Otto committed
428
  grunt.registerTask('dist-css', ['less-compile', 'autoprefixer', 'usebanner', 'csscomb', 'less:minify', 'cssmin']);
429

Mark Otto's avatar
derp    
Mark Otto committed
430
  // Docs distribution task.
431
  grunt.registerTask('dist-docs', 'copy:docs');
Mark Otto's avatar
Mark Otto committed
432

433
  // Full distribution task.
434
  grunt.registerTask('dist', ['clean', 'dist-css', 'copy:fonts', 'dist-js', 'dist-docs']);
435

436
  // Default task.
437
  grunt.registerTask('default', ['test', 'dist', 'build-glyphicons-data', 'build-customizer']);
438

439
440
441
  // 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!
442
  grunt.registerTask('change-version-number', 'sed');
443

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

446
  // task for building customizer
447
448
  grunt.registerTask('build-customizer', ['build-customizer-html', 'build-raw-files']);
  grunt.registerTask('build-customizer-html', 'jade');
449
450
  grunt.registerTask('build-raw-files', 'Add scripts/less files to customizer.', function () {
    var banner = grunt.template.process('<%= banner %>');
451
    generateRawFiles(grunt, banner);
452
  });
Chris Rebert's avatar
Chris Rebert committed
453
454

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