4 * https://gist.github.com/2489540
8 module.exports = function( grunt ) {
14 // https://gist.github.com/2876125
15 function readOptionalJSON( filepath ) {
18 data = grunt.file.readJSON( filepath );
19 grunt.verbose.write( "Reading " + filepath + "..." ).ok();
24 var file = grunt.file,
26 verbose = grunt.verbose,
27 config = grunt.config,
34 pkg: "<json:package.json>",
35 dst: readOptionalJSON("dist/.destination.json"),
37 banner: "/*! jQuery v<%= pkg.version %> jquery.com | jquery.org/license */"
44 "src/sizzle-jquery.js",
45 "src/sizzle/sizzle.js"
61 "src/manipulation.js",
63 { flag: "css", src: "src/css.js" },
65 { flag: "ajax", src: "src/ajax.js" },
66 { flag: "ajax/jsonp", src: "src/ajax/jsonp.js", needs: [ "ajax", "ajax/script" ] },
67 { flag: "ajax/script", src: "src/ajax/script.js", needs: ["ajax"] },
68 { flag: "ajax/xhr", src: "src/ajax/xhr.js", needs: ["ajax"] },
69 { flag: "effects", src: "src/effects.js", needs: ["css"] },
70 { flag: "offset", src: "src/offset.js", needs: ["css"] },
71 { flag: "dimensions", src: "src/dimensions.js", needs: ["css"] },
72 { flag: "deprecated", src: "src/deprecated.js" },
79 "dist/jquery.min.js": [ "<banner>", "dist/jquery.js" ]
83 dist: "dist/jquery.js",
85 tests: "test/unit/**/*.js"
89 function jshintrc( path ) {
90 return readOptionalJSON( (path || "") + ".jshintrc" ) || {};
95 dist: jshintrc( "src/" ),
96 tests: jshintrc( "test/" )
101 files: "test/index.html"
105 "<config:lint.grunt>", "<config:lint.tests>",
118 grunt.registerTask( "default", "update_submodules selector build:*:* lint min dist:* compare_size" );
120 // Short list as a high frequency watch task
121 grunt.registerTask( "dev", "selector build:*:* lint" );
123 // Load grunt tasks from NPM packages
124 grunt.loadNpmTasks( "grunt-compare-size" );
125 grunt.loadNpmTasks( "grunt-git-authors" );
126 grunt.loadNpmTasks( "grunt-update-submodules" );
128 grunt.registerTask( "testswarm", function( commit, configFile ) {
129 var testswarm = require( "testswarm" ),
131 config = grunt.file.readJSON( configFile ).jquery,
132 tests = "ajax attributes callbacks core css data deferred dimensions effects event manipulation offset queue serialize support traversing Sizzle".split(" ");
134 tests.forEach(function( test ) {
135 testUrls.push( config.testUrl + commit + "/test/index.html?module=" + test );
139 url: config.swarmUrl,
141 timeout: 1000 * 60 * 30,
144 authUsername: config.authUsername,
145 authToken: config.authToken,
146 jobName: "jQuery commit #<a href='https://github.com/jquery/jquery/commit/" + commit + "'>" + commit.substr( 0, 10 ) + "</a>",
147 runMax: config.runMax,
149 "runUrls[]": testUrls,
150 "browserSets[]": ["popular"]
154 // Build src/selector.js
155 grunt.registerMultiTask( "selector", "Build src/selector.js", function() {
157 var name = this.file.dest,
158 files = this.file.src,
160 api: file.read( files[0] ),
161 src: file.read( files[1] )
167 sizzle-jquery.js -> sizzle between "EXPOSE" blocks,
168 replace define & window.Sizzle assignment
172 if ( typeof define === "function" && define.amd ) {
173 define(function() { return Sizzle; });
175 window.Sizzle = Sizzle;
181 Sizzle.attr = jQuery.attr;
182 jQuery.find = Sizzle;
183 jQuery.expr = Sizzle.selectors;
184 jQuery.expr[":"] = jQuery.expr.pseudos;
185 jQuery.unique = Sizzle.uniqueSort;
186 jQuery.text = Sizzle.getText;
187 jQuery.isXMLDoc = Sizzle.isXML;
188 jQuery.contains = Sizzle.contains;
192 // Break into 3 pieces
193 parts = sizzle.src.split("// EXPOSE");
194 // Replace the if/else block with api
195 parts[1] = sizzle.api;
197 compiled = parts.join("");
200 verbose.write("Injected sizzle-jquery.js into sizzle.js");
202 // Write concatenated source to file
203 file.write( name, compiled );
205 // Fail task if errors were logged.
206 if ( this.errorCount ) {
210 // Otherwise, print a success message.
211 log.writeln( "File '" + name + "' created." );
215 // Special "alias" task to make custom build creation less grawlix-y
216 grunt.registerTask( "custom", function() {
217 var done = this.async(),
218 args = [].slice.call(arguments),
219 modules = args.length ? args[0].replace(/,/g, ":") : "";
222 // Translation example
224 // grunt custom:+ajax,-dimensions,-effects,-offset
228 // grunt build:*:*:+ajax:-dimensions:-effects:-offset
230 grunt.log.writeln( "Creating custom build...\n" );
234 args: [ "build:*:*:" + modules, "min" ]
235 }, function( err, result ) {
237 grunt.verbose.error();
242 grunt.log.writeln( result.replace("Done, without errors.", "") );
248 // Special concat/build task to handle various jQuery build requirements
250 grunt.registerMultiTask(
252 "Concatenate source (include/exclude modules with +/- flags), embed date/version",
254 // Concat specified files.
256 modules = this.flags,
257 explicit = Object.keys(modules).length > 1,
258 optIn = !modules["*"],
259 name = this.file.dest,
261 version = config( "pkg.version" ),
262 excluder = function( flag, needsFlag ) {
263 // explicit > implicit, so set this first and let it be overridden by explicit
264 if ( optIn && !modules[ flag ] && !modules[ "+" + flag ] ) {
265 excluded[ flag ] = false;
268 if ( excluded[ needsFlag ] || modules[ "-" + flag ] ) {
269 // explicit exclusion from flag or dependency
270 excluded[ flag ] = true;
271 } else if ( modules[ "+" + flag ] && ( excluded[ needsFlag ] === false ) ) {
272 // explicit inclusion from flag or dependency overriding a weak inclusion
273 delete excluded[ needsFlag ];
277 // append commit id to version
278 if ( process.env.COMMIT ) {
279 version += " " + process.env.COMMIT;
282 // figure out which files to exclude based on these rules in this order:
283 // explicit > implicit (explicit also means a dependency/dependent that was explicit)
286 // *: none (implicit exclude)
287 // *:* all (implicit include)
288 // *:*:-effects all except effects (explicit > implicit)
289 // *:*:-css all except css and its deps (explicit)
290 // *:*:-css:+effects all except css and its deps (explicit exclude from dep. trumps explicit include)
291 // *:+effects none except effects and its deps (explicit include from dep. trumps implicit exclude)
292 this.file.src.forEach(function( filepath ) {
293 var flag = filepath.flag;
299 // check for dependencies
300 if ( filepath.needs ) {
301 filepath.needs.forEach(function( needsFlag ) {
302 excluder( flag, needsFlag );
308 // append excluded modules to version
309 if ( Object.keys( excluded ).length ) {
310 version += " -" + Object.keys( excluded ).join( ",-" );
311 // set pkg.version to version with excludes, so minified file picks it up
312 grunt.config.set( "pkg.version", version );
316 // conditionally concatenate source
317 this.file.src.forEach(function( filepath ) {
318 var flag = filepath.flag,
324 if ( excluded[ flag ] !== undefined ) {
325 message = ( "Excluding " + flag ).red;
329 message = ( "Including " + flag ).green;
331 // If this module was actually specified by the
332 // builder, then st the flag to include it in the
334 if ( modules[ "+" + flag ] ) {
339 // Only display the inclusion/exclusion list when handling
342 // Additionally, only display modules that have been specified
344 if ( explicit && specified ) {
345 grunt.log.writetableln([ 27, 30 ], [
347 ( "(" + filepath.src + ")").grey
351 filepath = filepath.src;
355 compiled += file.read( filepath );
361 compiled = compiled.replace( /@VERSION/g, version )
362 .replace( "@DATE", function () {
363 var date = new Date();
373 // Write concatenated source to file
374 file.write( name, compiled );
376 // Fail task if errors were logged.
377 if ( this.errorCount ) {
381 // Otherwise, print a success message.
382 log.writeln( "File '" + name + "' created." );
385 // Allow custom dist file locations
386 grunt.registerTask( "dist", function() {
387 var flags, paths, stored;
389 // Check for stored destination paths
390 // ( set in dist/.destination.json )
391 stored = Object.keys( config("dst") );
393 // Allow command line input as well
394 flags = Object.keys( this.flags );
396 // Combine all output target paths
397 paths = [].concat( stored, flags ).filter(function( path ) {
401 // Ensure the dist files are pure ASCII
402 var fs = require("fs"),
404 distpaths.forEach(function( filename ) {
405 var buf = fs.readFileSync( filename, "utf8" ),
407 if ( buf.length !== Buffer.byteLength( buf, "utf8" ) ) {
408 log.writeln( filename + ": Non-ASCII characters detected:" );
409 for ( i = 0; i < buf.length; i++ ) {
410 c = buf.charCodeAt( i );
412 log.writeln( "- position " + i + ": " + c );
413 log.writeln( "-- " + buf.substring( i - 20, i + 20 ) );
423 // Proceed only if there are actual
425 if ( paths.length ) {
427 // 'distpaths' is declared at the top of the
428 // module.exports function scope. It is an array
429 // of default files that jQuery creates
430 distpaths.forEach(function( filename ) {
431 paths.forEach(function( path ) {
434 if ( !/\/$/.test( path ) ) {
438 created = path + filename.replace( "dist/", "" );
440 if ( !/^\//.test( path ) ) {
441 log.error( "File '" + created + "' was NOT created." );
445 file.write( created, file.read(filename) );
447 log.writeln( "File '" + created + "' created." );