Gruntfile.js 11.7 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' +
Zlatan Vasović's avatar
Zlatan Vasović committed
31
              ' * Bootstrap v<%= pkg.version %> (<%= pkg.homepage %>)\n' +
32
              ' * Copyright 2011-<%= grunt.template.today("yyyy") %> <%= pkg.author %>\n' +
Zlatan Vasović's avatar
Zlatan Vasović committed
33
34
              ' * Licensed under <%= _.pluck(pkg.licenses, "type") %> (<%= _.pluck(pkg.licenses, "url") %>)\n' +
              ' */\n',
35
36
37
38
39
40
    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',
41
    jqueryCheck: 'if (typeof jQuery === \'undefined\') { throw new Error(\'Bootstrap requires jQuery\') }\n\n',
42
43
44

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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