Style nit in test/unit/callbacks.js. Closes gh-992
[jquery.git] / grunt.js
blob8bc9f09dc1da479244d843c893b5dc2fdcb8c1d0
1 /**
2  * Resources
3  *
4  * https://gist.github.com/2489540
5  *
6  */
8 module.exports = function( grunt ) {
10         "use strict";
12         // readOptionalJSON
13         // by Ben Alman
14         // https://gist.github.com/2876125
15         function readOptionalJSON( filepath ) {
16                 var data = {};
17                 try {
18                         data = grunt.file.readJSON( filepath );
19                         grunt.verbose.write( "Reading " + filepath + "..." ).ok();
20                 } catch(e) {}
21                 return data;
22         }
24         var file = grunt.file,
25                 log = grunt.log,
26                 verbose = grunt.verbose,
27                 config = grunt.config,
28                 distpaths = [
29                         "dist/jquery.js",
30                         "dist/jquery.min.js"
31                 ];
33         grunt.initConfig({
34                 pkg: "<json:package.json>",
35                 dst: readOptionalJSON("dist/.destination.json"),
36                 meta: {
37                         banner: "/*! jQuery v<%= pkg.version %> jquery.com | jquery.org/license */"
38                 },
39                 compare_size: {
40                         files: distpaths
41                 },
42                 selector: {
43                         "src/selector.js": [
44                                 "src/sizzle-jquery.js",
45                                 "src/sizzle/sizzle.js"
46                         ]
47                 },
48                 build: {
49                         "dist/jquery.js": [
50                                 "src/intro.js",
51                                 "src/core.js",
52                                 "src/callbacks.js",
53                                 "src/deferred.js",
54                                 "src/support.js",
55                                 "src/data.js",
56                                 "src/queue.js",
57                                 "src/attributes.js",
58                                 "src/event.js",
59                                 "src/selector.js",
60                                 "src/traversing.js",
61                                 "src/manipulation.js",
63                                 { flag: "css", src: "src/css.js" },
64                                 "src/serialize.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" },
74                                 "src/exports.js",
75                                 "src/outro.js"
76                         ]
77                 },
78                 min: {
79                         "dist/jquery.min.js": [ "<banner>", "dist/jquery.js" ]
80                 },
82                 lint: {
83                         dist: "dist/jquery.js",
84                         grunt: "grunt.js",
85                         tests: "test/unit/**/*.js"
86                 },
88                 jshint: (function() {
89                         function jshintrc( path ) {
90                                 return readOptionalJSON( (path || "") + ".jshintrc" ) || {};
91                         }
93                         return {
94                                 grunt: jshintrc(),
95                                 dist: jshintrc( "src/" ),
96                                 tests: jshintrc( "test/" )
97                         };
98                 })(),
100                 qunit: {
101                         files: "test/index.html"
102                 },
103                 watch: {
104                         files: [
105                                 "<config:lint.grunt>", "<config:lint.tests>",
106                                 "src/**/*.js"
107                         ],
108                         tasks: "dev"
109                 },
110                 uglify: {
111                         codegen: {
112                                 ascii_only: true
113                         }
114                 }
115         });
117         // Default grunt.
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" ),
130                         testUrls = [],
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 );
136                 });
138                 testswarm({
139                         url: config.swarmUrl,
140                         pollInterval: 10000,
141                         timeout: 1000 * 60 * 30,
142                         done: this.async()
143                 }, {
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,
148                         "runNames[]": tests,
149                         "runUrls[]": testUrls,
150                         "browserSets[]": ["popular"]
151                 });
152         });
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,
159                                 sizzle = {
160                                         api: file.read( files[0] ),
161                                         src: file.read( files[1] )
162                                 },
163                                 compiled, parts;
165                 /**
167                         sizzle-jquery.js -> sizzle between "EXPOSE" blocks,
168                         replace define & window.Sizzle assignment
171                         // EXPOSE
172                         if ( typeof define === "function" && define.amd ) {
173                                 define(function() { return Sizzle; });
174                         } else {
175                                 window.Sizzle = Sizzle;
176                         }
177                         // EXPOSE
179                         Becomes...
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;
190                  */
192                 // Break into 3 pieces
193                 parts = sizzle.src.split("// EXPOSE");
194                 // Replace the if/else block with api
195                 parts[1] = sizzle.api;
196                 // Rejoin the pieces
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 ) {
207                         return false;
208                 }
210                 // Otherwise, print a success message.
211                 log.writeln( "File '" + name + "' created." );
212         });
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
223                 //
224                 //   grunt custom:+ajax,-dimensions,-effects,-offset
225                 //
226                 // Becomes:
227                 //
228                 //   grunt build:*:*:+ajax:-dimensions:-effects:-offset
230                 grunt.log.writeln( "Creating custom build...\n" );
232                 grunt.utils.spawn({
233                         cmd: "grunt",
234                         args: [ "build:*:*:" + modules, "min" ]
235                 }, function( err, result ) {
236                         if ( err ) {
237                                 grunt.verbose.error();
238                                 done( err );
239                                 return;
240                         }
242                         grunt.log.writeln( result.replace("Done, without errors.", "") );
244                         done();
245                 });
246         });
248         // Special concat/build task to handle various jQuery build requirements
249         //
250         grunt.registerMultiTask(
251                 "build",
252                 "Concatenate source (include/exclude modules with +/- flags), embed date/version",
253                 function() {
254                         // Concat specified files.
255                         var compiled = "",
256                                 modules = this.flags,
257                                 explicit = Object.keys(modules).length > 1,
258                                 optIn = !modules["*"],
259                                 name = this.file.dest,
260                                 excluded = {},
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;
266                                         }
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 ];
274                                         }
275                                 };
277                         // append commit id to version
278                         if ( process.env.COMMIT ) {
279                                 version += " " + process.env.COMMIT;
280                         }
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)
284                         //  exclude > include
285                         // examples:
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;
295                                 if ( flag ) {
297                                         excluder(flag);
299                                         // check for dependencies
300                                         if ( filepath.needs ) {
301                                                 filepath.needs.forEach(function( needsFlag ) {
302                                                         excluder( flag, needsFlag );
303                                                 });
304                                         }
305                                 }
306                         });
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 );
313                         }
316                         // conditionally concatenate source
317                         this.file.src.forEach(function( filepath ) {
318                                 var flag = filepath.flag,
319                                                 specified = false,
320                                                 omit = false,
321                                                 message = "";
323                                 if ( flag ) {
324                                         if ( excluded[ flag ] !== undefined ) {
325                                                 message = ( "Excluding " + flag ).red;
326                                                 specified = true;
327                                                 omit = true;
328                                         } else {
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
333                                                 // output list
334                                                 if ( modules[ "+" + flag ] ) {
335                                                         specified = true;
336                                                 }
337                                         }
339                                         // Only display the inclusion/exclusion list when handling
340                                         // an explicit list.
341                                         //
342                                         // Additionally, only display modules that have been specified
343                                         // by the user
344                                         if ( explicit && specified ) {
345                                                 grunt.log.writetableln([ 27, 30 ], [
346                                                         message,
347                                                         ( "(" + filepath.src + ")").grey
348                                                 ]);
349                                         }
351                                         filepath = filepath.src;
352                                 }
354                                 if ( !omit ) {
355                                         compiled += file.read( filepath );
356                                 }
357                         });
359                         // Embed Version
360                         // Embed Date
361                         compiled = compiled.replace( /@VERSION/g, version )
362                                 .replace( "@DATE", function () {
363                                         var date = new Date();
365                                         // YYYY-MM-DD
366                                         return [
367                                                 date.getFullYear(),
368                                                 date.getMonth() + 1,
369                                                 date.getDate()
370                                         ].join( "-" );
371                                 });
373                         // Write concatenated source to file
374                         file.write( name, compiled );
376                         // Fail task if errors were logged.
377                         if ( this.errorCount ) {
378                                 return false;
379                         }
381                         // Otherwise, print a success message.
382                         log.writeln( "File '" + name + "' created." );
383                 });
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 ) {
398                         return path !== "*";
399                 });
401                 // Ensure the dist files are pure ASCII
402                 var fs = require("fs"),
403                         nonascii = false;
404                 distpaths.forEach(function( filename ) {
405                         var buf = fs.readFileSync( filename, "utf8" ),
406                         i, c;
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 );
411                                         if ( c > 127 ) {
412                                                 log.writeln( "- position " + i + ": " + c );
413                                                 log.writeln( "-- " + buf.substring( i - 20, i + 20 ) );
414                                                 nonascii = true;
415                                         }
416                                 }
417                         }
418                 });
419                 if ( nonascii ) {
420                         return false;
421                 }
423                 // Proceed only if there are actual
424                 // paths to write to
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 ) {
432                                         var created;
434                                         if ( !/\/$/.test( path ) ) {
435                                                 path += "/";
436                                         }
438                                         created = path + filename.replace( "dist/", "" );
440                                         if ( !/^\//.test( path ) ) {
441                                                 log.error( "File '" + created + "' was NOT created." );
442                                                 return;
443                                         }
445                                         file.write( created, file.read(filename) );
447                                         log.writeln( "File '" + created + "' created." );
448                                 });
449                         });
450                 }
451         });