Gruntfile.js 11.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
22
23
  var generateGlyphiconsData = require('./grunt/bs-glyphicons-data-generator.js');
  var BsLessdocParser = require('./grunt/bs-lessdoc-parser.js');
  var generateRawFilesJs = require('./grunt/bs-raw-files-generator.js');
  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/application.js', 'docs/assets/js/customizer.js']
60
61
      }
    },
62

Chris Rebert's avatar
Chris Rebert committed
63
64
65
66
    jscs: {
      options: {
        config: 'js/.jscs.json',
      },
Chris Rebert's avatar
Chris Rebert committed
67
      grunt: {
Zlatan Vasović's avatar
Zlatan Vasović committed
68
        src: ['Gruntfile.js', 'grunt/*.js']
Chris Rebert's avatar
Chris Rebert committed
69
70
      },
      src: {
71
        src: 'js/*.js'
Chris Rebert's avatar
Chris Rebert committed
72
73
      },
      test: {
74
        src: 'js/tests/unit/*.js'
75
76
77
      },
      assets: {
        src: ['docs/assets/js/application.js', 'docs/assets/js/customizer.js']
Chris Rebert's avatar
Chris Rebert committed
78
79
80
      }
    },

XhmikosR's avatar
XhmikosR committed
81
82
    csslint: {
      options: {
83
        csslintrc: 'less/.csslintrc'
XhmikosR's avatar
XhmikosR committed
84
      },
85
86
87
      src: [
        'dist/css/bootstrap.css',
        'dist/css/bootstrap-theme.css',
88
89
        'docs/assets/css/docs.css',
        'docs/examples/**/*.css'
90
      ]
XhmikosR's avatar
XhmikosR committed
91
92
    },

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

117
    uglify: {
118
119
120
      options: {
        report: 'min'
      },
121
      bootstrap: {
122
        options: {
123
          banner: '<%= banner %>'
124
        },
125
        src: '<%= concat.bootstrap.dest %>',
126
        dest: 'dist/js/<%= pkg.name %>.min.js'
XhmikosR's avatar
XhmikosR committed
127
128
      },
      customize: {
129
        options: {
130
          preserveComments: 'some'
131
        },
XhmikosR's avatar
XhmikosR committed
132
        src: [
133
          'docs/assets/js/vendor/less.min.js',
XhmikosR's avatar
XhmikosR committed
134
          'docs/assets/js/vendor/jszip.min.js',
135
136
137
          'docs/assets/js/vendor/uglify.min.js',
          'docs/assets/js/vendor/blob.js',
          'docs/assets/js/vendor/filesaver.js',
138
          'docs/assets/js/raw-files.min.js',
139
          'docs/assets/js/customizer.js'
XhmikosR's avatar
XhmikosR committed
140
        ],
141
        dest: 'docs/assets/js/customize.min.js'
142
143
144
      },
      docsJs: {
        options: {
145
          preserveComments: 'some'
146
147
        },
        src: [
148
          'docs/assets/js/vendor/holder.js',
149
150
151
          'docs/assets/js/application.js'
        ],
        dest: 'docs/assets/js/docs.min.js'
152
153
154
      }
    },

155
    less: {
156
157
      compileCore: {
        options: {
158
          strictMath: true,
159
160
161
162
163
164
165
166
167
168
169
          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: {
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
178
        files: {
          'dist/css/<%= pkg.name %>-theme.css': 'less/theme.less'
        }
179
      },
180
181
182
183
184
185
186
187
188
189
      minify: {
        options: {
          cleancss: true,
          report: 'min'
        },
        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'
        }
      }
190
191
    },

XhmikosR's avatar
XhmikosR committed
192
193
194
195
    cssmin: {
      compress: {
        options: {
          keepSpecialComments: '*',
196
          noAdvanced: true, // turn advanced optimizations off until the issue is fixed in clean-css
XhmikosR's avatar
XhmikosR committed
197
          report: 'min',
XhmikosR's avatar
XhmikosR committed
198
          compatibility: 'ie8'
XhmikosR's avatar
XhmikosR committed
199
200
201
202
203
        },
        src: [
          'docs/assets/css/docs.css',
          'docs/assets/css/pygments-manni.css'
        ],
204
        dest: 'docs/assets/css/docs.min.css'
XhmikosR's avatar
XhmikosR committed
205
206
207
      }
    },

208
209
    usebanner: {
      dist: {
210
        options: {
211
212
          position: 'top',
          banner: '<%= banner %>'
213
        },
214
        files: {
215
216
          src: [
            'dist/css/<%= pkg.name %>.css',
217
            'dist/css/<%= pkg.name %>.min.css',
218
            'dist/css/<%= pkg.name %>-theme.css',
219
            'dist/css/<%= pkg.name %>-theme.min.css'
220
          ]
221
222
223
224
        }
      }
    },

225
    csscomb: {
226
227
228
229
      options: {
        config: 'less/.csscomb.json'
      },
      dist: {
230
        files: {
231
232
          'dist/css/<%= pkg.name %>.css': 'dist/css/<%= pkg.name %>.css',
          'dist/css/<%= pkg.name %>-theme.css': 'dist/css/<%= pkg.name %>-theme.css'
233
        }
234
235
      },
      examples: {
Chris Rebert's avatar
Chris Rebert committed
236
237
238
239
        expand: true,
        cwd: 'docs/examples/',
        src: ['**/*.css'],
        dest: 'docs/examples/'
240
241
242
      }
    },

Mark Otto's avatar
Mark Otto committed
243
244
245
    copy: {
      fonts: {
        expand: true,
246
        src: 'fonts/*',
Mark Otto's avatar
Mark Otto committed
247
        dest: 'dist/'
248
      },
Mark Otto's avatar
Mark Otto committed
249
      docs: {
250
        expand: true,
Mark Otto's avatar
Mark Otto committed
251
252
253
        cwd: './dist',
        src: [
          '{css,js}/*.min.*',
Chris Rebert's avatar
Chris Rebert committed
254
          'css/*.map',
Mark Otto's avatar
Mark Otto committed
255
256
257
          'fonts/*'
        ],
        dest: 'docs/dist'
Mark Otto's avatar
Mark Otto committed
258
259
260
      }
    },

261
262
263
264
    qunit: {
      options: {
        inject: 'js/tests/unit/phantom.js'
      },
265
      files: 'js/tests/index.html'
266
    },
267

268
269
270
271
272
    connect: {
      server: {
        options: {
          port: 3000,
          base: '.'
273
        }
274
275
276
      }
    },

277
278
279
280
    jekyll: {
      docs: {}
    },

281
282
283
284
285
286
287
288
289
290
291
292
    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: {
293
294
          'docs/_includes/customizer-variables.html': 'docs/jade/customizer-variables.jade',
          'docs/_includes/nav-customize.html': 'docs/jade/customizer-nav.jade'
295
296
297
298
        }
      }
    },

299
300
    validation: {
      options: {
301
302
        charset: 'utf-8',
        doctype: 'HTML5',
303
        failHard: true,
304
305
        reset: true,
        relaxerror: [
XhmikosR's avatar
XhmikosR committed
306
307
          'Bad value X-UA-Compatible for attribute http-equiv on element meta.',
          'Element img is missing required attribute src.'
308
        ]
309
310
      },
      files: {
311
        src: '_gh_pages/**/*.html'
312
313
314
      }
    },

315
316
317
318
319
320
321
322
323
    watch: {
      src: {
        files: '<%= jshint.src.src %>',
        tasks: ['jshint:src', 'qunit']
      },
      test: {
        files: '<%= jshint.test.src %>',
        tasks: ['jshint:test', 'qunit']
      },
324
      less: {
325
        files: 'less/*.less',
326
        tasks: 'less'
327
      }
328
329
330
331
332
    },

    sed: {
      versionNumber: {
        pattern: (function () {
Chris Rebert's avatar
Chris Rebert committed
333
334
          var old = grunt.option('oldver');
          return old ? RegExp.quote(old) : old;
335
336
337
338
        })(),
        replacement: grunt.option('newver'),
        recursive: true
      }
339
340
341
342
343
344
    },

    'saucelabs-qunit': {
      all: {
        options: {
          build: process.env.TRAVIS_JOB_ID,
Chris Rebert's avatar
Chris Rebert committed
345
          concurrency: 10,
346
          urls: ['http://127.0.0.1:3000/js/tests/index.html'],
347
          browsers: grunt.file.readYAML('test-infra/sauce_browsers.yml')
348
349
        }
      }
Chris Rebert's avatar
Chris Rebert committed
350
351
352
353
354
355
356
357
358
    },

    exec: {
      npmUpdate: {
        command: 'npm update'
      },
      npmShrinkWrap: {
        command: 'npm shrinkwrap --dev'
      }
359
360
    }
  });
361
362


363
  // These plugins provide necessary tasks.
Tobias Lindig's avatar
Tobias Lindig committed
364
  require('load-grunt-tasks')(grunt, {scope: 'devDependencies'});
365

366
  // Docs HTML validation task
367
  grunt.registerTask('validate-html', ['jekyll', 'validation']);
368

369
  // Test task.
370
371
372
  var testSubtasks = [];
  // Skip core tests if running a different subset of the test suite
  if (!process.env.TWBS_TEST || process.env.TWBS_TEST === 'core') {
373
    testSubtasks = testSubtasks.concat(['dist-css', 'csslint', 'jshint', 'jscs', 'qunit', 'build-customizer-html']);
374
375
376
377
378
  }
  // 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');
  }
379
  // Only run Sauce Labs tests if there's a Sauce access key
Chris Rebert's avatar
Chris Rebert committed
380
  if (typeof process.env.SAUCE_ACCESS_KEY !== 'undefined' &&
381
      // Skip Sauce if running a different subset of the test suite
Chris Rebert's avatar
Chris Rebert committed
382
      (!process.env.TWBS_TEST || process.env.TWBS_TEST === 'sauce-js-unit')) {
383
384
    testSubtasks.push('connect');
    testSubtasks.push('saucelabs-qunit');
385
386
  }
  grunt.registerTask('test', testSubtasks);
387

388
389
  // JS distribution task.
  grunt.registerTask('dist-js', ['concat', 'uglify']);
390

391
  // CSS distribution task.
XhmikosR's avatar
XhmikosR committed
392
  grunt.registerTask('dist-css', ['less', 'cssmin', 'csscomb', 'usebanner']);
393

Mark Otto's avatar
derp    
Mark Otto committed
394
  // Docs distribution task.
395
  grunt.registerTask('dist-docs', 'copy:docs');
Mark Otto's avatar
Mark Otto committed
396

397
  // Full distribution task.
398
  grunt.registerTask('dist', ['clean', 'dist-css', 'copy:fonts', 'dist-js', 'dist-docs']);
399

400
  // Default task.
Chris Rebert's avatar
Chris Rebert committed
401
  grunt.registerTask('default', ['test', 'dist', 'build-glyphicons-data', 'build-customizer', 'update-shrinkwrap']);
402

403
404
405
  // 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!
406
  grunt.registerTask('change-version-number', 'sed');
407

408
  grunt.registerTask('build-glyphicons-data', generateGlyphiconsData);
409

410
  // task for building customizer
411
412
  grunt.registerTask('build-customizer', ['build-customizer-html', 'build-raw-files']);
  grunt.registerTask('build-customizer-html', 'jade');
413
414
415
416
  grunt.registerTask('build-raw-files', 'Add scripts/less files to customizer.', function () {
    var banner = grunt.template.process('<%= banner %>');
    generateRawFilesJs(banner);
  });
Chris Rebert's avatar
Chris Rebert committed
417
418
419
420

  // Task for updating the npm packages used by the Travis build.
  grunt.registerTask('update-shrinkwrap', ['exec:npmUpdate', 'exec:npmShrinkWrap', '_update-shrinkwrap']);
  grunt.registerTask('_update-shrinkwrap', function () { updateShrinkwrap.call(this, grunt); });
421
};