Gruntfile.js 8.72 KB
Newer Older
1
/* jshint node: true */
2

3
module.exports = function(grunt) {
4
5
  "use strict";

6
7
8
  // Force use of Unix newlines
  grunt.util.linefeed = '\n';

9
  RegExp.quote = require('regexp-quote')
10
  var btoa = require('btoa')
11
12
13
14
15
  // Project configuration.
  grunt.initConfig({

    // Metadata.
    pkg: grunt.file.readJSON('package.json'),
16
    banner: '/*!\n' +
Zlatan Vasović's avatar
Zlatan Vasović committed
17
              ' * Bootstrap v<%= pkg.version %> (<%= pkg.homepage %>)\n' +
18
19
              ' * Copyright <%= grunt.template.today("yyyy") %> <%= pkg.author %>\n' +
              ' * Licensed under <%= _.pluck(pkg.licenses, "url").join(", ") %>\n' +
20
              ' */\n\n',
21
    jqueryCheck: 'if (typeof jQuery === "undefined") { throw new Error("Bootstrap requires jQuery") }\n\n',
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

    // Task configuration.
    clean: {
      dist: ['dist']
    },

    jshint: {
      options: {
        jshintrc: 'js/.jshintrc'
      },
      gruntfile: {
        src: 'Gruntfile.js'
      },
      src: {
        src: ['js/*.js']
      },
      test: {
        src: ['js/tests/unit/*.js']
      }
    },
42

43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
    concat: {
      options: {
        banner: '<%= banner %><%= jqueryCheck %>',
        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'
      }
    },
66

67
68
    uglify: {
      options: {
69
70
        banner: '<%= banner %>',
        report: 'min'
71
72
73
74
75
76
77
78
79
      },
      bootstrap: {
        src: ['<%= concat.bootstrap.dest %>'],
        dest: 'dist/js/<%= pkg.name %>.min.js'
      }
    },

    recess: {
      options: {
80
81
        compile: true,
        banner: '<%= banner %>'
82
83
84
85
86
87
88
89
      },
      bootstrap: {
        src: ['less/bootstrap.less'],
        dest: 'dist/css/<%= pkg.name %>.css'
      },
      min: {
        options: {
          compress: true
90
        },
91
92
        src: ['less/bootstrap.less'],
        dest: 'dist/css/<%= pkg.name %>.min.css'
93
94
95
96
97
98
99
100
101
102
103
      },
      theme: {
        src: ['less/theme.less'],
        dest: 'dist/css/<%= pkg.name %>-theme.css'
      },
      theme_min: {
        options: {
          compress: true
        },
        src: ['less/theme.less'],
        dest: 'dist/css/<%= pkg.name %>-theme.min.css'
104
105
106
      }
    },

Mark Otto's avatar
Mark Otto committed
107
108
109
110
111
112
113
114
    copy: {
      fonts: {
        expand: true,
        src: ["fonts/*"],
        dest: 'dist/'
      }
    },

115
116
117
118
119
120
    qunit: {
      options: {
        inject: 'js/tests/unit/phantom.js'
      },
      files: ['js/tests/*.html']
    },
121

122
123
124
125
126
    connect: {
      server: {
        options: {
          port: 3000,
          base: '.'
127
        }
128
129
130
      }
    },

131
132
133
134
135
136
    jekyll: {
      docs: {}
    },

    validation: {
      options: {
137
138
        reset: true,
        relaxerror: [
Zlatan Vasović's avatar
Zlatan Vasović committed
139
140
          "Bad value X-UA-Compatible for attribute http-equiv on element meta.",
          "Element img is missing required attribute src."
141
        ]
142
143
144
145
146
147
      },
      files: {
        src: ["_gh_pages/**/*.html"]
      }
    },

148
149
150
151
152
153
154
155
156
157
158
159
160
    watch: {
      src: {
        files: '<%= jshint.src.src %>',
        tasks: ['jshint:src', 'qunit']
      },
      test: {
        files: '<%= jshint.test.src %>',
        tasks: ['jshint:test', 'qunit']
      },
      recess: {
        files: 'less/*.less',
        tasks: ['recess']
      }
161
162
163
164
165
166
167
168
169
170
171
    },

    sed: {
      versionNumber: {
        pattern: (function () {
          var old = grunt.option('oldver')
          return old ? RegExp.quote(old) : old
        })(),
        replacement: grunt.option('newver'),
        recursive: true
      }
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
    },

    'saucelabs-qunit': {
      all: {
        options: {
          build: process.env.TRAVIS_JOB_ID,
          concurrency: 3,
          urls: ['http://127.0.0.1:3000/js/tests/index.html'],
          browsers: [
            // See https://saucelabs.com/docs/platforms/webdriver
            {
              browserName: 'safari',
              version: '6',
              platform: 'OS X 10.8'
            },
            {
              browserName: 'chrome',
              version: '28',
              platform: 'OS X 10.6'
            },
            /* FIXME: currently fails 1 tooltip test
            {
              browserName: 'firefox',
              version: '25',
              platform: 'OS X 10.6'
            },*/
            // Mac Opera not currently supported by Sauce Labs
            /* FIXME: currently fails 1 tooltip test
            {
              browserName: 'internet explorer',
              version: '11',
              platform: 'Windows 8.1'
            },*/
            /*
            {
              browserName: 'internet explorer',
              version: '10',
              platform: 'Windows 8'
            },
            {
              browserName: 'internet explorer',
              version: '9',
              platform: 'Windows 7'
            },
            {
              browserName: 'internet explorer',
              version: '8',
              platform: 'Windows 7'
            },
            {// unofficial
              browserName: 'internet explorer',
              version: '7',
              platform: 'Windows XP'
            },
            */
            {
              browserName: 'chrome',
              version: '31',
              platform: 'Windows 8.1'
            },
            {
              browserName: 'firefox',
              version: '25',
              platform: 'Windows 8.1'
            },
            // Win Opera 15+ not currently supported by Sauce Labs
            {
              browserName: 'iphone',
              version: '6.1',
              platform: 'OS X 10.8'
            },
            // iOS Chrome not currently supported by Sauce Labs
            // Linux (unofficial)
            {
              browserName: 'chrome',
              version: '30',
              platform: 'Linux'
            },
            {
              browserName: 'firefox',
              version: '25',
              platform: 'Linux'
            }
            // Android Chrome not currently supported by Sauce Labs
            /* Android Browser (super-unofficial)
            {
              browserName: 'android',
              version: '4.0',
              platform: 'Linux'
            }
            */
          ],
        }
      }
266
267
    }
  });
268
269


270
271
272
273
  // These plugins provide necessary tasks.
  grunt.loadNpmTasks('grunt-contrib-clean');
  grunt.loadNpmTasks('grunt-contrib-concat');
  grunt.loadNpmTasks('grunt-contrib-connect');
Mark Otto's avatar
Mark Otto committed
274
  grunt.loadNpmTasks('grunt-contrib-copy');
275
276
277
278
  grunt.loadNpmTasks('grunt-contrib-jshint');
  grunt.loadNpmTasks('grunt-contrib-qunit');
  grunt.loadNpmTasks('grunt-contrib-uglify');
  grunt.loadNpmTasks('grunt-contrib-watch');
279
280
  grunt.loadNpmTasks('grunt-html-validation');
  grunt.loadNpmTasks('grunt-jekyll');
281
  grunt.loadNpmTasks('grunt-recess');
282
  grunt.loadNpmTasks('grunt-saucelabs');
283
  grunt.loadNpmTasks('grunt-sed');
284

285
  // Docs HTML validation task
286
  grunt.registerTask('validate-html', ['jekyll', 'validation']);
287

288
  // Test task.
fat's avatar
fat committed
289
  var testSubtasks = ['dist-css', 'jshint', 'qunit', 'validate-html'];
290
291
292
293
  // Only run Sauce Labs tests if there's a Sauce access key
  if (typeof process.env.SAUCE_ACCESS_KEY !== 'undefined') {
    testSubtasks.push('connect');
    testSubtasks.push('saucelabs-qunit');
294
295
  }
  grunt.registerTask('test', testSubtasks);
296

297
298
  // JS distribution task.
  grunt.registerTask('dist-js', ['concat', 'uglify']);
299

300
301
  // CSS distribution task.
  grunt.registerTask('dist-css', ['recess']);
302

Mark Otto's avatar
Mark Otto committed
303
304
305
  // Fonts distribution task.
  grunt.registerTask('dist-fonts', ['copy']);

306
  // Full distribution task.
Mark Otto's avatar
Mark Otto committed
307
  grunt.registerTask('dist', ['clean', 'dist-css', 'dist-fonts', 'dist-js']);
308

309
  // Default task.
310
  grunt.registerTask('default', ['test', 'dist', 'build-customizer']);
311

312
313
314
315
316
  // 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!
  grunt.registerTask('change-version-number', ['sed']);

317
318
319
320
321
322
323
324
  // task for building customizer
  grunt.registerTask('build-customizer', 'Add scripts/less files to customizer.', function () {
    var fs = require('fs')

    function getFiles(type) {
      var files = {}
      fs.readdirSync(type)
        .filter(function (path) {
fat's avatar
fat committed
325
          return type == 'fonts' ? true : new RegExp('\\.' + type + '$').test(path)
326
327
        })
        .forEach(function (path) {
328
329
          var fullPath = type + '/' + path
          return files[path] = (type == 'fonts' ? btoa(fs.readFileSync(fullPath)) : fs.readFileSync(fullPath, 'utf8'))
330
331
332
333
        })
      return 'var __' + type + ' = ' + JSON.stringify(files) + '\n'
    }

fat's avatar
fat committed
334
    var files = getFiles('js') + getFiles('less') + getFiles('fonts')
335
    fs.writeFileSync('docs-assets/js/raw-files.js', files)
336
  });
337
};