Gruntfile.js 11.7 KB
Newer Older
1
/* jshint node: true */
Chris Rebert's avatar
Chris Rebert committed
2
3
4
5
6
7
/*!
 * Bootstrap's Gruntfile
 * http://getbootstrap.com
 * Copyright 2013-2014 Twitter, Inc.
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
 */
8

Chris Rebert's avatar
Chris Rebert committed
9
module.exports = function (grunt) {
XhmikosR's avatar
XhmikosR committed
10
  'use strict';
11

12
13
14
  // Force use of Unix newlines
  grunt.util.linefeed = '\n';

Zlatan Vasović's avatar
Zlatan Vasović committed
15
  RegExp.quote = function (string) {
Chris Rebert's avatar
Chris Rebert committed
16
17
    return string.replace(/[-\\^$*+?.()|[\]{}]/g, '\\$&');
  };
18

Chris Rebert's avatar
Chris Rebert committed
19
  var fs = require('fs');
20
  var path = require('path');
Chris Rebert's avatar
Chris Rebert committed
21
  var generateGlyphiconsData = require('./docs/grunt/bs-glyphicons-data-generator.js');
22
  var BsLessdocParser = require('./docs/grunt/bs-lessdoc-parser.js');
Chris Rebert's avatar
Chris Rebert committed
23
  var generateRawFilesJs = require('./docs/grunt/bs-raw-files-generator.js');
Chris Rebert's avatar
Chris Rebert committed
24
  var updateShrinkwrap = require('./test-infra/shrinkwrap.js');
25

26
27
28
29
30
  // Project configuration.
  grunt.initConfig({

    // Metadata.
    pkg: grunt.file.readJSON('package.json'),
31
    banner: '/*!\n' +
Zlatan Vasović's avatar
Zlatan Vasović committed
32
              ' * Bootstrap v<%= pkg.version %> (<%= pkg.homepage %>)\n' +
33
              ' * Copyright 2011-<%= grunt.template.today("yyyy") %> <%= pkg.author %>\n' +
Zlatan Vasović's avatar
Zlatan Vasović committed
34
35
              ' * Licensed under <%= _.pluck(pkg.licenses, "type") %> (<%= _.pluck(pkg.licenses, "url") %>)\n' +
              ' */\n',
36
37
38
39
40
41
    bannerDocs: '/*!\n' +
              ' * Bootstrap Docs (<%= pkg.homepage %>)\n' +
              ' * Copyright 2011-<%= grunt.template.today("yyyy") %> <%= pkg.author %>\n' +
              ' * Licensed under the Creative Commons Attribution 3.0 Unported License. For\n' +
              ' * details, see http://creativecommons.org/licenses/by/3.0/.\n' +
              ' */\n',
42
    jqueryCheck: 'if (typeof jQuery === \'undefined\') { throw new Error(\'Bootstrap requires jQuery\') }\n\n',
43
44
45

    // Task configuration.
    clean: {
46
      dist: 'dist'
47
48
49
50
51
52
    },

    jshint: {
      options: {
        jshintrc: 'js/.jshintrc'
      },
Chris Rebert's avatar
Chris Rebert committed
53
54
      grunt: {
        src: ['Gruntfile.js', 'docs/grunt/*.js', 'test-infra/shrinkwrap.js']
55
56
      },
      src: {
57
        src: 'js/*.js'
58
59
      },
      test: {
60
        src: 'js/tests/unit/*.js'
61
62
      },
      assets: {
63
        src: ['docs/assets/js/application.js', 'docs/assets/js/customizer.js']
64
65
      }
    },
66

Chris Rebert's avatar
Chris Rebert committed
67
68
69
70
    jscs: {
      options: {
        config: 'js/.jscs.json',
      },
Chris Rebert's avatar
Chris Rebert committed
71
72
      grunt: {
        src: ['Gruntfile.js', 'docs/grunt/*.js', 'test-infra/shrinkwrap.js']
Chris Rebert's avatar
Chris Rebert committed
73
74
      },
      src: {
75
        src: 'js/*.js'
Chris Rebert's avatar
Chris Rebert committed
76
77
      },
      test: {
78
        src: 'js/tests/unit/*.js'
79
80
81
      },
      assets: {
        src: ['docs/assets/js/application.js', 'docs/assets/js/customizer.js']
Chris Rebert's avatar
Chris Rebert committed
82
83
84
      }
    },

XhmikosR's avatar
XhmikosR committed
85
86
    csslint: {
      options: {
87
        csslintrc: 'less/.csslintrc'
XhmikosR's avatar
XhmikosR committed
88
      },
89
90
91
      src: [
        'dist/css/bootstrap.css',
        'dist/css/bootstrap-theme.css',
92
        'docs/assets/css/docs.css'
93
      ]
XhmikosR's avatar
XhmikosR committed
94
95
    },

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

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

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

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

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

228
229
    csscomb: {
      sort: {
230
        options: {
231
          config: 'less/.csscomb.json'
232
        },
233
        files: {
234
235
          'dist/css/<%= pkg.name %>.css': 'dist/css/<%= pkg.name %>.css',
          'dist/css/<%= pkg.name %>-theme.css': 'dist/css/<%= pkg.name %>-theme.css'
236
        }
237
238
239
      }
    },

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

258
259
260
261
    qunit: {
      options: {
        inject: 'js/tests/unit/phantom.js'
      },
262
      files: 'js/tests/*.html'
263
    },
264

265
266
267
268
269
    connect: {
      server: {
        options: {
          port: 3000,
          base: '.'
270
        }
271
272
273
      }
    },

274
275
276
277
    jekyll: {
      docs: {}
    },

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

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

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

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

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

    exec: {
      npmUpdate: {
        command: 'npm update'
      },
      npmShrinkWrap: {
        command: 'npm shrinkwrap --dev'
      }
356
357
    }
  });
358
359


360
  // These plugins provide necessary tasks.
Tobias Lindig's avatar
Tobias Lindig committed
361
  require('load-grunt-tasks')(grunt, {scope: 'devDependencies'});
362
  grunt.loadNpmTasks('browserstack-runner');
363

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

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

392
393
  // JS distribution task.
  grunt.registerTask('dist-js', ['concat', 'uglify']);
394

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

Mark Otto's avatar
derp    
Mark Otto committed
398
  // Docs distribution task.
399
  grunt.registerTask('dist-docs', 'copy:docs');
Mark Otto's avatar
Mark Otto committed
400

401
  // Full distribution task.
402
  grunt.registerTask('dist', ['clean', 'dist-css', 'copy:fonts', 'dist-docs', 'dist-js']);
403

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

407
408
409
  // 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!
410
  grunt.registerTask('change-version-number', 'sed');
411

412
  grunt.registerTask('build-glyphicons-data', generateGlyphiconsData);
413

414
  // task for building customizer
415
416
  grunt.registerTask('build-customizer', ['build-customizer-html', 'build-raw-files']);
  grunt.registerTask('build-customizer-html', 'jade');
417
418
419
420
  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
421
422
423
424

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