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" ),
|
2016-03-12 14:48:37 +00:00
|
|
|
Insight = require( "insight" ),
|
|
|
|
pkg = require( "../../package.json" ),
|
2013-08-16 14:13:09 +00:00
|
|
|
srcFolder = __dirname + "/../../src/",
|
2015-06-16 15:24:21 +00:00
|
|
|
rdefineEnd = /\}\s*?\);[^}\w]*$/,
|
2016-03-07 16:57:35 +00:00
|
|
|
read = function( fileName ) {
|
|
|
|
return grunt.file.read( srcFolder + fileName );
|
|
|
|
},
|
|
|
|
globals = read( "exports/global.js" ),
|
|
|
|
wrapper = read( "wrapper.js" ).split( /\/\/ \@CODE\n\/\/[^\n]+/ ),
|
2013-08-15 18:15:49 +00:00
|
|
|
config = {
|
|
|
|
baseUrl: "src",
|
|
|
|
name: "jquery",
|
2015-08-16 06:59:58 +00:00
|
|
|
|
2016-04-15 21:13:59 +00:00
|
|
|
// Allow strict mode
|
|
|
|
useStrict: true,
|
|
|
|
|
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: {
|
2016-05-10 09:12:28 +00:00
|
|
|
start: wrapper[ 0 ].replace( /\/\*eslint .* \*\/\n/, "" ),
|
2016-04-25 20:35:15 +00:00
|
|
|
end: globals.replace(
|
|
|
|
/\/\*\s*ExcludeStart\s*\*\/[\w\W]*?\/\*\s*ExcludeEnd\s*\*\//ig,
|
|
|
|
""
|
|
|
|
) + wrapper[ 1 ]
|
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
|
2016-05-10 09:12:28 +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
|
2016-04-25 20:34:29 +00:00
|
|
|
.replace( /define\([^{]*?{\s*(?:("|')use strict\1(?:;|))?/, "" )
|
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[ "*" ],
|
2015-11-12 18:18:59 +00:00
|
|
|
name = grunt.option( "filename" ),
|
2013-08-15 18:15:49 +00:00
|
|
|
minimum = this.data.minimum,
|
|
|
|
removeWith = this.data.removeWith,
|
|
|
|
excluded = [],
|
|
|
|
included = [],
|
|
|
|
version = grunt.config( "pkg.version" ),
|
2016-05-10 09:12:28 +00:00
|
|
|
|
2013-08-15 18:15:49 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
}
|
|
|
|
},
|
2016-05-10 09:12:28 +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 ) {
|
2016-01-19 19:47:52 +00:00
|
|
|
var additional,
|
|
|
|
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
|
|
|
|
2016-01-19 19:47:52 +00:00
|
|
|
additional = removeWith[ module ];
|
|
|
|
|
2013-08-15 18:15:49 +00:00
|
|
|
// Check removeWith list
|
2016-01-19 19:47:52 +00:00
|
|
|
if ( additional ) {
|
|
|
|
excludeList( additional.remove || additional );
|
|
|
|
if ( additional.include ) {
|
|
|
|
included = included.concat( additional.include );
|
|
|
|
grunt.log.writeln( "+" + additional.include );
|
|
|
|
}
|
|
|
|
}
|
2013-08-15 18:15:49 +00:00
|
|
|
} 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 );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-11-12 18:18:59 +00:00
|
|
|
// Filename can be passed to the command line using
|
|
|
|
// command line options
|
|
|
|
// e.g. grunt build --filename=jquery-custom.js
|
|
|
|
name = name ? ( "dist/" + name ) : this.data.dest;
|
|
|
|
|
2013-08-15 18:15:49 +00:00
|
|
|
// 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,
|
2016-03-12 14:48:37 +00:00
|
|
|
modules = args.length ? args[ 0 ].replace( /,/g, ":" ) : "",
|
|
|
|
done = this.async(),
|
|
|
|
insight = new Insight( {
|
|
|
|
trackingCode: "UA-1076265-4",
|
|
|
|
pkg: pkg
|
|
|
|
} );
|
|
|
|
|
|
|
|
function exec( trackingAllowed ) {
|
|
|
|
var tracks = args.length ? args[ 0 ].split( "," ) : [];
|
|
|
|
var defaultPath = [ "build", "custom" ];
|
|
|
|
|
|
|
|
tracks = tracks.map( function( track ) {
|
|
|
|
return track.replace( /\//g, "+" );
|
|
|
|
} );
|
|
|
|
|
|
|
|
if ( trackingAllowed ) {
|
|
|
|
|
|
|
|
// Track individuals
|
|
|
|
tracks.forEach( function( module ) {
|
|
|
|
var path = defaultPath.concat( [ "individual" ], module );
|
|
|
|
|
|
|
|
insight.track.apply( insight, path );
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Track full command
|
|
|
|
insight.track.apply( insight, defaultPath.concat( [ "full" ], tracks ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
grunt.task.run( [ "build:*:*" + ( modules ? ":" + modules : "" ), "uglify", "dist" ] );
|
|
|
|
done();
|
|
|
|
}
|
2013-08-15 18:15:49 +00:00
|
|
|
|
|
|
|
grunt.log.writeln( "Creating custom build...\n" );
|
|
|
|
|
2016-03-12 14:48:37 +00:00
|
|
|
// Ask for permission the first time
|
|
|
|
if ( insight.optOut === undefined ) {
|
|
|
|
insight.askPermission( null, function( error, result ) {
|
|
|
|
exec( result );
|
|
|
|
} );
|
|
|
|
} else {
|
|
|
|
exec( !insight.optOut );
|
|
|
|
}
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2013-08-15 18:15:49 +00:00
|
|
|
};
|