2013-08-15 18:15:49 +00:00
|
|
|
/**
|
|
|
|
* Special concat/build task to handle various jQuery build requirements
|
2014-07-17 17:25:59 +00:00
|
|
|
* Concats AMD modules, removes their definitions,
|
|
|
|
* and includes/excludes specified modules
|
2013-08-15 18:15:49 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
module.exports = function( grunt ) {
|
|
|
|
|
2013-08-16 13:48:00 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var fs = require( "fs" ),
|
2013-10-15 14:44:50 +00:00
|
|
|
requirejs = require( "requirejs" ),
|
2013-08-16 14:13:09 +00:00
|
|
|
srcFolder = __dirname + "/../../src/",
|
2015-06-16 15:24:21 +00:00
|
|
|
rdefineEnd = /\}\s*?\);[^}\w]*$/,
|
2013-08-15 18:15:49 +00:00
|
|
|
config = {
|
|
|
|
baseUrl: "src",
|
|
|
|
name: "jquery",
|
|
|
|
out: "dist/jquery.js",
|
2015-08-16 06:59:58 +00:00
|
|
|
|
2013-08-15 18:15:49 +00:00
|
|
|
// We have multiple minify steps
|
|
|
|
optimize: "none",
|
2015-08-16 06:59:58 +00:00
|
|
|
|
2013-09-09 15:26:21 +00:00
|
|
|
// Include dependencies loaded with require
|
|
|
|
findNestedDependencies: true,
|
2015-08-16 06:59:58 +00:00
|
|
|
|
2014-05-01 10:21:34 +00:00
|
|
|
// Avoid inserting define() placeholder
|
|
|
|
skipModuleInsertion: true,
|
2015-08-16 06:59:58 +00:00
|
|
|
|
2013-09-09 15:26:21 +00:00
|
|
|
// Avoid breaking semicolons inserted by r.js
|
2013-08-15 18:15:49 +00:00
|
|
|
skipSemiColonInsertion: true,
|
|
|
|
wrap: {
|
|
|
|
startFile: "src/intro.js",
|
2014-12-09 20:39:24 +00:00
|
|
|
endFile: [ "src/exports/global.js", "src/outro.js" ]
|
2013-08-15 18:15:49 +00:00
|
|
|
},
|
2013-09-09 02:26:05 +00:00
|
|
|
rawText: {},
|
2013-08-15 18:15:49 +00:00
|
|
|
onBuildWrite: convert
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Strip all definitions generated by requirejs
|
|
|
|
* Convert "var" modules to var declarations
|
2014-07-17 17:25:59 +00:00
|
|
|
* "var module" means the module only contains a return
|
|
|
|
* statement that should be converted to a var declaration
|
2013-08-15 18:15:49 +00:00
|
|
|
* This is indicated by including the file in any "var" folder
|
|
|
|
* @param {String} name
|
|
|
|
* @param {String} path
|
|
|
|
* @param {String} contents The contents to be written (including their AMD wrappers)
|
|
|
|
*/
|
|
|
|
function convert( name, path, contents ) {
|
2013-12-19 20:31:36 +00:00
|
|
|
var amdName;
|
2015-08-16 06:59:58 +00:00
|
|
|
|
2013-08-15 18:15:49 +00:00
|
|
|
// Convert var modules
|
2015-10-12 14:02:25 +00:00
|
|
|
if ( /.\/var\//.test( path.replace( process.cwd(), "" ) ) ) {
|
2013-08-15 18:15:49 +00:00
|
|
|
contents = contents
|
2015-08-16 06:59:58 +00:00
|
|
|
.replace( /define\([\w\W]*?return/, "var " + ( /var\/([\w-]+)/.exec( name )[ 1 ] ) + " =" )
|
2013-08-15 18:15:49 +00:00
|
|
|
.replace( rdefineEnd, "" );
|
|
|
|
|
|
|
|
// Sizzle treatment
|
2016-02-10 21:47:25 +00:00
|
|
|
} else if ( /\/sizzle$/.test( name ) ) {
|
2013-08-15 18:15:49 +00:00
|
|
|
contents = "var Sizzle =\n" + contents
|
2015-08-16 06:59:58 +00:00
|
|
|
|
2013-08-15 18:15:49 +00:00
|
|
|
// Remove EXPOSE lines from Sizzle
|
|
|
|
.replace( /\/\/\s*EXPOSE[\w\W]*\/\/\s*EXPOSE/, "return Sizzle;" );
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2014-12-09 20:39:24 +00:00
|
|
|
contents = contents
|
2015-06-16 15:24:21 +00:00
|
|
|
.replace( /\s*return\s+[^\}]+(\}\s*?\);[^\w\}]*)$/, "$1" )
|
2015-08-16 06:59:58 +00:00
|
|
|
|
2014-12-09 20:39:24 +00:00
|
|
|
// Multiple exports
|
|
|
|
.replace( /\s*exports\.\w+\s*=\s*\w+;/g, "" );
|
2013-08-15 18:15:49 +00:00
|
|
|
|
|
|
|
// Remove define wrappers, closure ends, and empty declarations
|
|
|
|
contents = contents
|
|
|
|
.replace( /define\([^{]*?{/, "" )
|
2013-08-16 16:54:43 +00:00
|
|
|
.replace( rdefineEnd, "" );
|
|
|
|
|
2013-10-15 16:03:55 +00:00
|
|
|
// Remove anything wrapped with
|
|
|
|
// /* ExcludeStart */ /* ExcludeEnd */
|
|
|
|
// or a single line directly after a // BuildExclude comment
|
2013-09-09 13:50:12 +00:00
|
|
|
contents = contents
|
2013-10-15 16:03:55 +00:00
|
|
|
.replace( /\/\*\s*ExcludeStart\s*\*\/[\w\W]*?\/\*\s*ExcludeEnd\s*\*\//ig, "" )
|
|
|
|
.replace( /\/\/\s*BuildExclude\n\r?[\w\W]*?\n\r?/ig, "" );
|
2013-09-09 13:50:12 +00:00
|
|
|
|
2013-08-16 16:54:43 +00:00
|
|
|
// Remove empty definitions
|
|
|
|
contents = contents
|
2014-05-01 10:21:34 +00:00
|
|
|
.replace( /define\(\[[^\]]*\]\)[\W\n]+$/, "" );
|
2013-08-15 18:15:49 +00:00
|
|
|
}
|
2015-08-16 06:59:58 +00:00
|
|
|
|
2014-03-07 14:55:26 +00:00
|
|
|
// AMD Name
|
2015-08-16 06:59:58 +00:00
|
|
|
if ( ( amdName = grunt.option( "amd" ) ) != null && /^exports\/amd$/.test( name ) ) {
|
|
|
|
if ( amdName ) {
|
2014-03-07 14:55:26 +00:00
|
|
|
grunt.log.writeln( "Naming jQuery with AMD name: " + amdName );
|
|
|
|
} else {
|
|
|
|
grunt.log.writeln( "AMD name now anonymous" );
|
|
|
|
}
|
2015-08-16 06:59:58 +00:00
|
|
|
|
2014-03-07 14:55:26 +00:00
|
|
|
// Remove the comma for anonymous defines
|
|
|
|
contents = contents
|
|
|
|
.replace( /(\s*)"jquery"(\,\s*)/, amdName ? "$1\"" + amdName + "\"$2" : "" );
|
|
|
|
|
|
|
|
}
|
2013-08-15 18:15:49 +00:00
|
|
|
return contents;
|
|
|
|
}
|
|
|
|
|
|
|
|
grunt.registerMultiTask(
|
|
|
|
"build",
|
2014-07-17 17:25:59 +00:00
|
|
|
"Concatenate source, remove sub AMD definitions, " +
|
|
|
|
"(include/exclude modules with +/- flags), embed date/version",
|
2013-08-15 18:15:49 +00:00
|
|
|
function() {
|
|
|
|
var flag, index,
|
|
|
|
done = this.async(),
|
|
|
|
flags = this.flags,
|
2013-09-09 02:26:05 +00:00
|
|
|
optIn = flags[ "*" ],
|
2013-08-15 18:15:49 +00:00
|
|
|
name = this.data.dest,
|
|
|
|
minimum = this.data.minimum,
|
|
|
|
removeWith = this.data.removeWith,
|
|
|
|
excluded = [],
|
|
|
|
included = [],
|
|
|
|
version = grunt.config( "pkg.version" ),
|
|
|
|
/**
|
|
|
|
* Recursively calls the excluder to remove on all modules in the list
|
|
|
|
* @param {Array} list
|
2014-07-17 17:25:59 +00:00
|
|
|
* @param {String} [prepend] Prepend this to the module name.
|
|
|
|
* Indicates we're walking a directory
|
2013-08-15 18:15:49 +00:00
|
|
|
*/
|
|
|
|
excludeList = function( list, prepend ) {
|
|
|
|
if ( list ) {
|
|
|
|
prepend = prepend ? prepend + "/" : "";
|
2015-08-16 06:59:58 +00:00
|
|
|
list.forEach( function( module ) {
|
|
|
|
|
2013-08-15 18:15:49 +00:00
|
|
|
// Exclude var modules as well
|
|
|
|
if ( module === "var" ) {
|
2014-07-17 17:25:59 +00:00
|
|
|
excludeList(
|
|
|
|
fs.readdirSync( srcFolder + prepend + module ), prepend + module
|
|
|
|
);
|
2013-08-15 18:15:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( prepend ) {
|
2015-08-16 06:59:58 +00:00
|
|
|
|
2013-08-15 18:15:49 +00:00
|
|
|
// Skip if this is not a js file and we're walking files in a dir
|
2015-08-16 06:59:58 +00:00
|
|
|
if ( !( module = /([\w-\/]+)\.js$/.exec( module ) ) ) {
|
2013-08-15 18:15:49 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-08-16 06:59:58 +00:00
|
|
|
|
2013-08-15 18:15:49 +00:00
|
|
|
// Prepend folder name if passed
|
|
|
|
// Remove .js extension
|
2015-08-16 06:59:58 +00:00
|
|
|
module = prepend + module[ 1 ];
|
2013-08-15 18:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Avoid infinite recursion
|
|
|
|
if ( excluded.indexOf( module ) === -1 ) {
|
|
|
|
excluder( "-" + module );
|
|
|
|
}
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2013-08-15 18:15:49 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Adds the specified module to the excluded or included list, depending on the flag
|
2014-07-17 17:25:59 +00:00
|
|
|
* @param {String} flag A module path relative to
|
|
|
|
* the src directory starting with + or - to indicate
|
|
|
|
* whether it should included or excluded
|
2013-08-15 18:15:49 +00:00
|
|
|
*/
|
|
|
|
excluder = function( flag ) {
|
|
|
|
var m = /^(\+|\-|)([\w\/-]+)$/.exec( flag ),
|
2013-08-16 13:48:00 +00:00
|
|
|
exclude = m[ 1 ] === "-",
|
|
|
|
module = m[ 2 ];
|
2013-08-15 18:15:49 +00:00
|
|
|
|
|
|
|
if ( exclude ) {
|
2015-08-16 06:59:58 +00:00
|
|
|
|
2013-08-15 18:15:49 +00:00
|
|
|
// Can't exclude certain modules
|
|
|
|
if ( minimum.indexOf( module ) === -1 ) {
|
2015-08-16 06:59:58 +00:00
|
|
|
|
2013-08-15 18:15:49 +00:00
|
|
|
// Add to excluded
|
|
|
|
if ( excluded.indexOf( module ) === -1 ) {
|
|
|
|
grunt.log.writeln( flag );
|
|
|
|
excluded.push( module );
|
2015-08-16 06:59:58 +00:00
|
|
|
|
2013-08-15 18:15:49 +00:00
|
|
|
// Exclude all files in the folder of the same name
|
|
|
|
// These are the removable dependencies
|
|
|
|
// It's fine if the directory is not there
|
|
|
|
try {
|
|
|
|
excludeList( fs.readdirSync( srcFolder + module ), module );
|
2014-07-17 17:25:59 +00:00
|
|
|
} catch ( e ) {
|
2013-08-15 18:15:49 +00:00
|
|
|
grunt.verbose.writeln( e );
|
|
|
|
}
|
|
|
|
}
|
2015-08-16 06:59:58 +00:00
|
|
|
|
2013-08-15 18:15:49 +00:00
|
|
|
// Check removeWith list
|
|
|
|
excludeList( removeWith[ module ] );
|
|
|
|
} else {
|
2015-08-16 06:59:58 +00:00
|
|
|
grunt.log.error( "Module \"" + module + "\" is a minimum requirement." );
|
2013-08-15 18:15:49 +00:00
|
|
|
if ( module === "selector" ) {
|
2014-07-17 17:25:59 +00:00
|
|
|
grunt.log.error(
|
|
|
|
"If you meant to replace Sizzle, use -sizzle instead."
|
|
|
|
);
|
2013-08-15 18:15:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
grunt.log.writeln( flag );
|
|
|
|
included.push( module );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// append commit id to version
|
|
|
|
if ( process.env.COMMIT ) {
|
|
|
|
version += " " + process.env.COMMIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
// figure out which files to exclude based on these rules in this order:
|
|
|
|
// dependency explicit exclude
|
|
|
|
// > explicit exclude
|
|
|
|
// > explicit include
|
|
|
|
// > dependency implicit exclude
|
|
|
|
// > implicit exclude
|
|
|
|
// examples:
|
|
|
|
// * none (implicit exclude)
|
|
|
|
// *:* all (implicit include)
|
|
|
|
// *:*:-css all except css and dependents (explicit > implicit)
|
2014-07-17 17:25:59 +00:00
|
|
|
// *:*:-css:+effects same (excludes effects because explicit include is
|
|
|
|
// trumped by explicit exclude of dependency)
|
|
|
|
// *:+effects none except effects and its dependencies
|
|
|
|
// (explicit include trumps implicit exclude of dependency)
|
2013-09-09 02:26:05 +00:00
|
|
|
delete flags[ "*" ];
|
2013-08-15 18:15:49 +00:00
|
|
|
for ( flag in flags ) {
|
2013-09-09 02:26:05 +00:00
|
|
|
excluder( flag );
|
2013-08-15 18:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle Sizzle exclusion
|
|
|
|
// Replace with selector-native
|
2015-08-16 06:59:58 +00:00
|
|
|
if ( ( index = excluded.indexOf( "sizzle" ) ) > -1 ) {
|
2013-09-09 02:26:05 +00:00
|
|
|
config.rawText.selector = "define(['./selector-native']);";
|
2013-08-15 18:15:49 +00:00
|
|
|
excluded.splice( index, 1 );
|
|
|
|
}
|
|
|
|
|
2013-12-19 20:00:06 +00:00
|
|
|
// Replace exports/global with a noop noConflict
|
2015-08-16 06:59:58 +00:00
|
|
|
if ( ( index = excluded.indexOf( "exports/global" ) ) > -1 ) {
|
2013-12-19 20:00:06 +00:00
|
|
|
config.rawText[ "exports/global" ] = "define(['../core']," +
|
|
|
|
"function( jQuery ) {\njQuery.noConflict = function() {};\n});";
|
|
|
|
excluded.splice( index, 1 );
|
|
|
|
}
|
|
|
|
|
2013-08-15 18:15:49 +00:00
|
|
|
grunt.verbose.writeflags( excluded, "Excluded" );
|
|
|
|
grunt.verbose.writeflags( included, "Included" );
|
|
|
|
|
|
|
|
// append excluded modules to version
|
|
|
|
if ( excluded.length ) {
|
|
|
|
version += " -" + excluded.join( ",-" );
|
2015-08-16 06:59:58 +00:00
|
|
|
|
2013-08-15 18:15:49 +00:00
|
|
|
// set pkg.version to version with excludes, so minified file picks it up
|
|
|
|
grunt.config.set( "pkg.version", version );
|
|
|
|
grunt.verbose.writeln( "Version changed to " + version );
|
2015-08-16 06:59:58 +00:00
|
|
|
|
2013-08-15 18:15:49 +00:00
|
|
|
// Have to use shallow or core will get excluded since it is a dependency
|
|
|
|
config.excludeShallow = excluded;
|
|
|
|
}
|
|
|
|
config.include = included;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle Final output from the optimizer
|
|
|
|
* @param {String} compiled
|
|
|
|
*/
|
|
|
|
config.out = function( compiled ) {
|
|
|
|
compiled = compiled
|
2015-08-16 06:59:58 +00:00
|
|
|
|
2013-08-15 18:15:49 +00:00
|
|
|
// Embed Version
|
|
|
|
.replace( /@VERSION/g, version )
|
2015-08-16 06:59:58 +00:00
|
|
|
|
2013-08-15 18:15:49 +00:00
|
|
|
// Embed Date
|
|
|
|
// yyyy-mm-ddThh:mmZ
|
|
|
|
.replace( /@DATE/g, ( new Date() ).toISOString().replace( /:\d+\.\d+Z$/, "Z" ) );
|
|
|
|
|
|
|
|
// Write concatenated source to file
|
|
|
|
grunt.file.write( name, compiled );
|
|
|
|
};
|
|
|
|
|
2013-09-09 02:26:05 +00:00
|
|
|
// Turn off opt-in if necessary
|
|
|
|
if ( !optIn ) {
|
2015-08-16 06:59:58 +00:00
|
|
|
|
2013-09-09 02:26:05 +00:00
|
|
|
// Overwrite the default inclusions with the explicit ones provided
|
2014-07-17 17:25:59 +00:00
|
|
|
config.rawText.jquery = "define([" +
|
2015-08-16 06:59:58 +00:00
|
|
|
( included.length ? included.join( "," ) : "" ) +
|
2014-07-17 17:25:59 +00:00
|
|
|
"]);";
|
2013-09-09 02:26:05 +00:00
|
|
|
}
|
|
|
|
|
2013-08-15 18:15:49 +00:00
|
|
|
// Trace dependencies and concatenate files
|
|
|
|
requirejs.optimize( config, function( response ) {
|
|
|
|
grunt.verbose.writeln( response );
|
|
|
|
grunt.log.ok( "File '" + name + "' created." );
|
|
|
|
done();
|
|
|
|
}, function( err ) {
|
|
|
|
done( err );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
|
|
|
} );
|
2013-08-15 18:15:49 +00:00
|
|
|
|
|
|
|
// Special "alias" task to make custom build creation less grawlix-y
|
|
|
|
// Translation example
|
|
|
|
//
|
|
|
|
// grunt custom:+ajax,-dimensions,-effects,-offset
|
|
|
|
//
|
|
|
|
// Becomes:
|
|
|
|
//
|
|
|
|
// grunt build:*:*:+ajax:-dimensions:-effects:-offset
|
|
|
|
grunt.registerTask( "custom", function() {
|
2013-12-19 20:31:36 +00:00
|
|
|
var args = this.args,
|
2013-08-16 13:48:00 +00:00
|
|
|
modules = args.length ? args[ 0 ].replace( /,/g, ":" ) : "";
|
2013-08-15 18:15:49 +00:00
|
|
|
|
|
|
|
grunt.log.writeln( "Creating custom build...\n" );
|
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
grunt.task.run( [ "build:*:*" + ( modules ? ":" + modules : "" ), "uglify", "dist" ] );
|
|
|
|
} );
|
2013-08-15 18:15:49 +00:00
|
|
|
};
|