Gruntfile.js 11.9 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 %>-rtl.min.css': 'dist/css/<%= pkg.name %>-rtl.css',
Mark Otto's avatar
Mark Otto committed
186
          'dist/css/<%= pkg.name %>-theme.min.css': 'dist/css/<%= pkg.name %>-theme.css'
187
188
189
190
        }
      }
    },

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

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

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

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

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

Mark Otto's avatar
Mark Otto committed
285
286
287
    copy: {
      fonts: {
        expand: true,
288
        src: 'fonts/*',
Mark Otto's avatar
Mark Otto committed
289
        dest: 'dist/'
290
      },
Mark Otto's avatar
Mark Otto committed
291
      docs: {
292
        expand: true,
Mark Otto's avatar
Mark Otto committed
293
294
295
        cwd: './dist',
        src: [
          '{css,js}/*.min.*',
Chris Rebert's avatar
Chris Rebert committed
296
          'css/*.map',
Mark Otto's avatar
Mark Otto committed
297
298
299
          'fonts/*'
        ],
        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
313
314
315
    jekyll: {
      docs: {}
    },

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

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

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

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

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

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


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

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

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

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

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

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

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

437
  // Default task.
Mark Otto's avatar
Mark Otto committed
438
  grunt.registerTask('default', ['test', 'dist', 'build-glyphicons-data', 'build-customizer', 'update-shrinkwrap']);
439

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

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

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

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