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
|
|
|
*/
|
|
|
|
|
2019-10-08 22:17:55 +00:00
|
|
|
"use strict";
|
2013-08-16 13:48:00 +00:00
|
|
|
|
2019-10-08 22:17:55 +00:00
|
|
|
module.exports = function( grunt ) {
|
2013-08-16 13:48:00 +00:00
|
|
|
var fs = require( "fs" ),
|
2013-10-15 14:44:50 +00:00
|
|
|
requirejs = require( "requirejs" ),
|
2022-07-12 15:12:27 +00:00
|
|
|
slimBuildFlags = require( "./lib/slim-build-flags" ),
|
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 );
|
|
|
|
},
|
2016-12-04 00:16:36 +00:00
|
|
|
|
|
|
|
// Catch `// @CODE` and subsequent comment lines event if they don't start
|
|
|
|
// in the first column.
|
|
|
|
wrapper = read( "wrapper.js" ).split( /[\x20\t]*\/\/ @CODE\n(?:[\x20\t]*\/\/[^\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-07-13 08:49:13 +00:00
|
|
|
start: wrapper[ 0 ].replace( /\/\*\s*eslint(?: |-).*\s*\*\/\n/, "" ),
|
2016-08-15 15:41:11 +00:00
|
|
|
end: 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(
|
2019-05-13 19:55:45 +00:00
|
|
|
/define\(\s*(["'])[\w\W]*?\1[\w\W]*?return/,
|
2016-05-10 09:12:28 +00:00
|
|
|
"var " +
|
|
|
|
( /var\/([\w-]+)/.exec( name )[ 1 ] ) +
|
|
|
|
" ="
|
|
|
|
)
|
2013-08-15 18:15:49 +00:00
|
|
|
.replace( rdefineEnd, "" );
|
|
|
|
|
|
|
|
} 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
|
2022-12-14 00:41:31 +00:00
|
|
|
.replace( /define\(\s*\[[^\]]*\]\s*\)[\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
|
2022-12-14 00:41:31 +00:00
|
|
|
* whether it should be included or excluded
|
2013-08-15 18:15:49 +00:00
|
|
|
*/
|
|
|
|
excluder = function( flag ) {
|
2016-01-19 19:47:52 +00:00
|
|
|
var additional,
|
2022-12-14 00:41:31 +00:00
|
|
|
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
|
|
|
|
2022-12-14 00:41:31 +00:00
|
|
|
// Recognize the legacy `sizzle` alias
|
|
|
|
if ( module === "sizzle" ) {
|
|
|
|
module = "selector";
|
|
|
|
}
|
|
|
|
|
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 {
|
2022-12-14 00:41:31 +00:00
|
|
|
|
|
|
|
// `selector` is a special case as we don't just remove
|
|
|
|
// the module, but we replace it with `selector-native`
|
|
|
|
// which re-uses parts of the `src/selector` folder.
|
|
|
|
if ( module !== "selector" ) {
|
|
|
|
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
|
|
|
}
|
|
|
|
} 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
|
|
|
}
|
|
|
|
|
2022-12-14 00:41:31 +00:00
|
|
|
// Handle full selector module exclusion.
|
|
|
|
// Replace with selector-native.
|
|
|
|
if ( excluded.indexOf( "selector" ) > -1 ) {
|
|
|
|
config.rawText.selector = "define([ \"./selector-native\" ]);";
|
2013-08-15 18:15:49 +00:00
|
|
|
}
|
|
|
|
|
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 ) {
|
2022-12-14 00:41:31 +00:00
|
|
|
config.rawText[ "exports/global" ] = "define( [\n\t\"../core\"\n], " +
|
|
|
|
"function( jQuery ) {\n\tjQuery.noConflict = function() {};\n} );";
|
2013-12-19 20:00:06 +00:00
|
|
|
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
|
2022-12-14 00:41:31 +00:00
|
|
|
config.rawText.jquery = "define( [\n" +
|
|
|
|
( included.length ?
|
|
|
|
included.map( module => "\t\"./" + module + "\"" ).join( ",\n" ) :
|
|
|
|
"" ) +
|
|
|
|
"\n] );";
|
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
|
2020-01-07 15:42:49 +00:00
|
|
|
//
|
|
|
|
// There's also a special "slim" alias that resolves to the jQuery Slim build
|
|
|
|
// configuration:
|
|
|
|
//
|
|
|
|
// grunt custom:slim
|
2013-08-15 18:15:49 +00:00
|
|
|
grunt.registerTask( "custom", function() {
|
2013-12-19 20:31:36 +00:00
|
|
|
var args = this.args,
|
2020-01-07 15:42:49 +00:00
|
|
|
modules = args.length ?
|
|
|
|
args[ 0 ]
|
|
|
|
.split( "," )
|
|
|
|
|
|
|
|
// Replace "slim" with respective exclusions meant for
|
|
|
|
// the official slim build
|
|
|
|
.reduce( ( acc, elem ) => acc.concat(
|
|
|
|
elem === "slim" ?
|
2022-07-12 15:12:27 +00:00
|
|
|
slimBuildFlags :
|
2020-01-07 15:42:49 +00:00
|
|
|
[ elem ]
|
|
|
|
), [] )
|
|
|
|
|
|
|
|
.join( ":" ) :
|
2022-12-20 20:53:36 +00:00
|
|
|
"";
|
2013-08-15 18:15:49 +00:00
|
|
|
|
|
|
|
grunt.log.writeln( "Creating custom build...\n" );
|
2023-07-10 18:45:30 +00:00
|
|
|
grunt.task.run( [
|
|
|
|
"build:*:*" + ( modules ? ":" + modules : "" ),
|
|
|
|
"uglify",
|
|
|
|
"remove_map_comment",
|
|
|
|
"dist"
|
|
|
|
] );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2013-08-15 18:15:49 +00:00
|
|
|
};
|