Gruntfile.js 11.2 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');
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
52
53
54
55
    },

    jshint: {
      options: {
        jshintrc: 'js/.jshintrc'
      },
      gruntfile: {
        src: 'Gruntfile.js'
      },
      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
      },
      docsGrunt: {
65
        src: 'docs/grunt/*.js'
66
67
      }
    },
68

Chris Rebert's avatar
Chris Rebert committed
69
70
71
72
73
    jscs: {
      options: {
        config: 'js/.jscs.json',
      },
      gruntfile: {
74
        src: 'Gruntfile.js'
Chris Rebert's avatar
Chris Rebert committed
75
76
      },
      src: {
77
        src: 'js/*.js'
Chris Rebert's avatar
Chris Rebert committed
78
79
      },
      test: {
80
        src: 'js/tests/unit/*.js'
81
82
83
      },
      assets: {
        src: ['docs/assets/js/application.js', 'docs/assets/js/customizer.js']
84
85
      },
      docsGrunt: {
86
        src: 'docs/grunt/*.js'
Chris Rebert's avatar
Chris Rebert committed
87
88
89
      }
    },

XhmikosR's avatar
XhmikosR committed
90
91
    csslint: {
      options: {
92
        csslintrc: 'less/.csslintrc'
XhmikosR's avatar
XhmikosR committed
93
      },
94
95
96
      src: [
        'dist/css/bootstrap.css',
        'dist/css/bootstrap-theme.css',
97
        'docs/assets/css/docs.css'
98
      ]
XhmikosR's avatar
XhmikosR committed
99
100
    },

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

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

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

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

216
217
    usebanner: {
      dist: {
218
        options: {
219
220
          position: 'top',
          banner: '<%= banner %>'
221
        },
222
        files: {
223
224
          src: [
            'dist/css/<%= pkg.name %>.css',
225
            'dist/css/<%= pkg.name %>.min.css',
226
            'dist/css/<%= pkg.name %>-theme.css',
227
            'dist/css/<%= pkg.name %>-theme.min.css',
228
          ]
229
230
231
232
        }
      }
    },

233
234
    csscomb: {
      sort: {
235
        options: {
236
          config: 'less/.csscomb.json'
237
        },
238
        files: {
239
240
          'dist/css/<%= pkg.name %>.css': 'dist/css/<%= pkg.name %>.css',
          'dist/css/<%= pkg.name %>-theme.css': 'dist/css/<%= pkg.name %>-theme.css'
241
        }
242
243
244
      }
    },

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

263
264
265
266
    qunit: {
      options: {
        inject: 'js/tests/unit/phantom.js'
      },
267
      files: 'js/tests/*.html'
268
    },
269

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

279
280
281
282
    jekyll: {
      docs: {}
    },

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

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

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

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

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


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

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

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

400
  // Default task.
401
  grunt.registerTask('default', ['test', 'dist', 'build-glyphicons-data', 'build-customizer']);
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);
  });
417
};