Gruntfile.js 11.4 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');
Chris Rebert's avatar
Chris Rebert committed
20
  var generateGlyphiconsData = require('./docs/grunt/bs-glyphicons-data-generator.js');
21
  var BsLessdocParser = require('./docs/grunt/bs-lessdoc-parser.js');
Chris Rebert's avatar
Chris Rebert committed
22
  var generateRawFilesJs = require('./docs/grunt/bs-raw-files-generator.js');
Chris Rebert's avatar
Chris Rebert committed
23
  var updateShrinkwrap = require('./test-infra/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
33
34
            ' * Bootstrap v<%= pkg.version %> (<%= pkg.homepage %>)\n' +
            ' * Copyright 2011-<%= grunt.template.today("yyyy") %> <%= pkg.author %>\n' +
            ' * Licensed under <%= _.pluck(pkg.licenses, "type") %> (<%= _.pluck(pkg.licenses, "url") %>)\n' +
            ' */\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
47
      grunt: {
        src: ['Gruntfile.js', 'docs/grunt/*.js', 'test-infra/shrinkwrap.js']
48
49
      },
      src: {
50
        src: 'js/*.js'
51
52
      },
      test: {
53
        src: 'js/tests/unit/*.js'
54
55
      },
      assets: {
56
        src: ['docs/assets/js/application.js', 'docs/assets/js/customizer.js']
57
58
      }
    },
59

Chris Rebert's avatar
Chris Rebert committed
60
61
62
63
    jscs: {
      options: {
        config: 'js/.jscs.json',
      },
Chris Rebert's avatar
Chris Rebert committed
64
65
      grunt: {
        src: ['Gruntfile.js', 'docs/grunt/*.js', 'test-infra/shrinkwrap.js']
Chris Rebert's avatar
Chris Rebert committed
66
67
      },
      src: {
68
        src: 'js/*.js'
Chris Rebert's avatar
Chris Rebert committed
69
70
      },
      test: {
71
        src: 'js/tests/unit/*.js'
72
73
74
      },
      assets: {
        src: ['docs/assets/js/application.js', 'docs/assets/js/customizer.js']
Chris Rebert's avatar
Chris Rebert committed
75
76
77
      }
    },

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

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

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

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

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

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

222
223
    csscomb: {
      sort: {
224
        options: {
225
          config: 'less/.csscomb.json'
226
        },
227
        files: {
228
229
          'dist/css/<%= pkg.name %>.css': 'dist/css/<%= pkg.name %>.css',
          'dist/css/<%= pkg.name %>-theme.css': 'dist/css/<%= pkg.name %>-theme.css'
230
        }
231
232
233
      }
    },

Mark Otto's avatar
Mark Otto committed
234
235
236
    copy: {
      fonts: {
        expand: true,
237
        src: 'fonts/*',
Mark Otto's avatar
Mark Otto committed
238
        dest: 'dist/'
239
      },
Mark Otto's avatar
Mark Otto committed
240
      docs: {
241
        expand: true,
Mark Otto's avatar
Mark Otto committed
242
243
244
        cwd: './dist',
        src: [
          '{css,js}/*.min.*',
Chris Rebert's avatar
Chris Rebert committed
245
          'css/*.map',
Mark Otto's avatar
Mark Otto committed
246
247
248
          'fonts/*'
        ],
        dest: 'docs/dist'
Mark Otto's avatar
Mark Otto committed
249
250
251
      }
    },

252
253
254
255
    qunit: {
      options: {
        inject: 'js/tests/unit/phantom.js'
      },
256
      files: 'js/tests/*.html'
257
    },
258

259
260
261
262
263
    connect: {
      server: {
        options: {
          port: 3000,
          base: '.'
264
        }
265
266
267
      }
    },

268
269
270
271
    jekyll: {
      docs: {}
    },

272
273
274
275
276
277
278
279
280
281
282
283
    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: {
284
285
          'docs/_includes/customizer-variables.html': 'docs/customizer-variables.jade',
          'docs/_includes/nav-customize.html': 'docs/customizer-nav.jade'
286
287
288
289
        }
      }
    },

290
291
    validation: {
      options: {
292
293
        charset: 'utf-8',
        doctype: 'HTML5',
294
        failHard: true,
295
296
        reset: true,
        relaxerror: [
XhmikosR's avatar
XhmikosR committed
297
298
          'Bad value X-UA-Compatible for attribute http-equiv on element meta.',
          'Element img is missing required attribute src.'
299
        ]
300
301
      },
      files: {
302
        src: '_gh_pages/**/*.html'
303
304
305
      }
    },

306
307
308
309
310
311
312
313
314
    watch: {
      src: {
        files: '<%= jshint.src.src %>',
        tasks: ['jshint:src', 'qunit']
      },
      test: {
        files: '<%= jshint.test.src %>',
        tasks: ['jshint:test', 'qunit']
      },
315
      less: {
316
        files: 'less/*.less',
317
        tasks: 'less'
318
      }
319
320
321
322
323
    },

    sed: {
      versionNumber: {
        pattern: (function () {
Chris Rebert's avatar
Chris Rebert committed
324
325
          var old = grunt.option('oldver');
          return old ? RegExp.quote(old) : old;
326
327
328
329
        })(),
        replacement: grunt.option('newver'),
        recursive: true
      }
330
331
332
333
334
335
    },

    'saucelabs-qunit': {
      all: {
        options: {
          build: process.env.TRAVIS_JOB_ID,
Chris Rebert's avatar
Chris Rebert committed
336
          concurrency: 10,
337
          urls: ['http://127.0.0.1:3000/js/tests/index.html'],
338
          browsers: grunt.file.readYAML('test-infra/sauce_browsers.yml')
339
340
        }
      }
Chris Rebert's avatar
Chris Rebert committed
341
342
343
344
345
346
347
348
349
    },

    exec: {
      npmUpdate: {
        command: 'npm update'
      },
      npmShrinkWrap: {
        command: 'npm shrinkwrap --dev'
      }
350
351
    }
  });
352
353


354
  // These plugins provide necessary tasks.
Tobias Lindig's avatar
Tobias Lindig committed
355
  require('load-grunt-tasks')(grunt, {scope: 'devDependencies'});
356
  grunt.loadNpmTasks('browserstack-runner');
357

358
  // Docs HTML validation task
359
  grunt.registerTask('validate-html', ['jekyll', 'validation']);
360

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

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

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

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

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

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

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

406
  grunt.registerTask('build-glyphicons-data', generateGlyphiconsData);
407

408
  // task for building customizer
409
410
  grunt.registerTask('build-customizer', ['build-customizer-html', 'build-raw-files']);
  grunt.registerTask('build-customizer-html', 'jade');
411
412
413
414
  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
415
416
417
418

  // 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); });
419
};