2013-08-15 18:15:49 +00:00
|
|
|
/**
|
2023-09-20 22:18:42 +00:00
|
|
|
* Special build task to handle various jQuery build requirements.
|
|
|
|
* Compiles JS modules into one bundle, sets the custom AMD name,
|
2014-07-17 17:25:59 +00:00
|
|
|
* 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
|
|
|
|
2024-03-08 23:18:24 +00:00
|
|
|
const fs = require( "node:fs/promises" );
|
|
|
|
const path = require( "node:path" );
|
|
|
|
const util = require( "node:util" );
|
|
|
|
const exec = util.promisify( require( "node:child_process" ).exec );
|
2023-09-20 22:18:42 +00:00
|
|
|
const requirejs = require( "requirejs" );
|
|
|
|
const excludedFromSlim = require( "./lib/slim-exclude" );
|
|
|
|
const pkg = require( "../../package.json" );
|
|
|
|
const isCleanWorkingDir = require( "./lib/isCleanWorkingDir" );
|
|
|
|
const minify = require( "./minify" );
|
|
|
|
const getTimestamp = require( "./lib/getTimestamp" );
|
|
|
|
const verifyNodeVersion = require( "./lib/verifyNodeVersion" );
|
|
|
|
const srcFolder = path.resolve( __dirname, "../../src" );
|
|
|
|
|
|
|
|
const rdefineEnd = /\}\s*?\);[^}\w]*$/;
|
|
|
|
|
|
|
|
const minimum = [ "core" ];
|
|
|
|
|
|
|
|
// Exclude specified modules if the module matching the key is removed
|
|
|
|
const removeWith = {
|
|
|
|
ajax: [ "manipulation/_evalUrl", "deprecated/ajax-event-alias" ],
|
|
|
|
callbacks: [ "deferred" ],
|
|
|
|
css: [ "effects", "dimensions", "offset" ],
|
|
|
|
"css/showHide": [ "effects" ],
|
|
|
|
deferred: {
|
|
|
|
remove: [ "ajax", "effects", "queue", "core/ready" ],
|
|
|
|
include: [ "core/ready-no-deferred" ]
|
|
|
|
},
|
|
|
|
event: [ "deprecated/ajax-event-alias", "deprecated/event" ],
|
|
|
|
selector: [ "css/hiddenVisibleSelectors", "effects/animatedSelector" ]
|
|
|
|
};
|
2016-04-15 21:13:59 +00:00
|
|
|
|
2023-09-20 22:18:42 +00:00
|
|
|
async function read( filename ) {
|
2024-03-08 23:18:24 +00:00
|
|
|
return fs.readFile( path.join( srcFolder, filename ), "utf8" );
|
2023-09-20 22:18:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the src folder and file extension
|
|
|
|
// and ensure unix-style path separators
|
|
|
|
function moduleName( filename ) {
|
|
|
|
return filename
|
2023-11-01 23:48:05 +00:00
|
|
|
.replace( `${ srcFolder }${ path.sep }`, "" )
|
2023-09-20 22:18:42 +00:00
|
|
|
.replace( /\.js$/, "" )
|
|
|
|
.split( path.sep )
|
|
|
|
.join( path.posix.sep );
|
|
|
|
}
|
|
|
|
|
|
|
|
async function readdirRecursive( dir, all = [] ) {
|
|
|
|
let files;
|
|
|
|
try {
|
2024-03-08 23:18:24 +00:00
|
|
|
files = await fs.readdir( path.join( srcFolder, dir ), {
|
2023-09-20 22:18:42 +00:00
|
|
|
withFileTypes: true
|
|
|
|
} );
|
2024-06-15 13:10:59 +00:00
|
|
|
} catch ( _ ) {
|
2023-09-20 22:18:42 +00:00
|
|
|
return all;
|
|
|
|
}
|
|
|
|
for ( const file of files ) {
|
|
|
|
const filepath = path.join( dir, file.name );
|
2015-08-16 06:59:58 +00:00
|
|
|
|
2023-09-20 22:18:42 +00:00
|
|
|
if ( file.isDirectory() ) {
|
|
|
|
all.push( ...( await readdirRecursive( filepath ) ) );
|
|
|
|
} else {
|
|
|
|
all.push( moduleName( filepath ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return all;
|
|
|
|
}
|
2015-08-16 06:59:58 +00:00
|
|
|
|
2023-09-20 22:18:42 +00:00
|
|
|
async function getRequireConfig( { amd } = {} ) {
|
|
|
|
const wrapperSource = await read( "wrapper.js" );
|
2015-08-16 06:59:58 +00:00
|
|
|
|
2023-09-20 22:18:42 +00:00
|
|
|
// Catch `// @CODE` and subsequent comment lines event if they don't start
|
|
|
|
// in the first column.
|
|
|
|
const wrapper = wrapperSource.split(
|
|
|
|
/[\x20\t]*\/\/ @CODE\n(?:[\x20\t]*\/\/[^\n]+\n)*/
|
|
|
|
);
|
2013-08-15 18:15:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 ) {
|
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
|
2023-09-20 22:18:42 +00:00
|
|
|
if ( amd != null && /^exports\/amd$/.test( name ) ) {
|
|
|
|
if ( amd ) {
|
|
|
|
console.log( "Naming jQuery with AMD name: " + amd );
|
2014-03-07 14:55:26 +00:00
|
|
|
} else {
|
2023-09-20 22:18:42 +00:00
|
|
|
console.log( "AMD name now anonymous" );
|
2014-03-07 14:55:26 +00:00
|
|
|
}
|
2015-08-16 06:59:58 +00:00
|
|
|
|
2014-03-07 14:55:26 +00:00
|
|
|
// Remove the comma for anonymous defines
|
|
|
|
contents = contents
|
2023-09-20 22:18:42 +00:00
|
|
|
.replace( /(\s*)"jquery"(\,\s*)/, amd ? "$1\"" + amd + "\"$2" : "" );
|
2014-03-07 14:55:26 +00:00
|
|
|
|
|
|
|
}
|
2013-08-15 18:15:49 +00:00
|
|
|
return contents;
|
|
|
|
}
|
|
|
|
|
2023-09-20 22:18:42 +00:00
|
|
|
return {
|
|
|
|
baseUrl: "src",
|
|
|
|
name: "jquery",
|
|
|
|
|
|
|
|
// Allow strict mode
|
|
|
|
useStrict: true,
|
|
|
|
|
|
|
|
// We have multiple minify steps
|
|
|
|
optimize: "none",
|
|
|
|
|
|
|
|
// Include dependencies loaded with require
|
|
|
|
findNestedDependencies: true,
|
|
|
|
|
|
|
|
// Avoid inserting define() placeholder
|
|
|
|
skipModuleInsertion: true,
|
2013-08-15 18:15:49 +00:00
|
|
|
|
2023-09-20 22:18:42 +00:00
|
|
|
// Avoid breaking semicolons inserted by r.js
|
|
|
|
skipSemiColonInsertion: true,
|
|
|
|
wrap: {
|
|
|
|
start: wrapper[ 0 ].replace( /\/\*\s*eslint(?: |-).*\s*\*\/\n/, "" ),
|
|
|
|
end: wrapper[ 1 ]
|
|
|
|
},
|
|
|
|
rawText: {},
|
|
|
|
onBuildWrite: convert
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function unique( array ) {
|
|
|
|
return [ ...new Set( array ) ];
|
|
|
|
}
|
|
|
|
|
|
|
|
async function checkExclude( exclude, include ) {
|
|
|
|
const included = [ ...include ];
|
|
|
|
const excluded = [ ...exclude ];
|
|
|
|
|
|
|
|
for ( const module of exclude ) {
|
|
|
|
if ( minimum.indexOf( module ) !== -1 ) {
|
2023-11-01 23:48:05 +00:00
|
|
|
throw new Error( `Module \"${ module }\" is a minimum requirement.` );
|
2013-08-15 18:15:49 +00:00
|
|
|
}
|
|
|
|
|
2023-09-20 22:18:42 +00:00
|
|
|
// Exclude all files in the dir of the same name
|
|
|
|
// These are the removable dependencies
|
|
|
|
// It's fine if the directory is not there
|
|
|
|
// `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.
|
|
|
|
// "sizzle" is legacy for selector
|
|
|
|
if ( module !== "selector" && module !== "sizzle" ) {
|
|
|
|
const files = await readdirRecursive( module );
|
|
|
|
excluded.push( ...files );
|
2013-08-15 18:15:49 +00:00
|
|
|
}
|
|
|
|
|
2023-09-20 22:18:42 +00:00
|
|
|
// Check removeWith list
|
|
|
|
const additional = removeWith[ module ];
|
|
|
|
if ( additional ) {
|
|
|
|
const [ additionalExcluded, additionalIncluded ] = await checkExclude(
|
|
|
|
additional.remove || additional,
|
|
|
|
additional.include || []
|
|
|
|
);
|
|
|
|
excluded.push( ...additionalExcluded );
|
|
|
|
included.push( ...additionalIncluded );
|
2013-12-19 20:00:06 +00:00
|
|
|
}
|
2023-09-20 22:18:42 +00:00
|
|
|
}
|
2013-12-19 20:00:06 +00:00
|
|
|
|
2023-09-20 22:18:42 +00:00
|
|
|
return [ unique( excluded ), unique( included ) ];
|
|
|
|
}
|
|
|
|
|
|
|
|
async function build( {
|
|
|
|
amd,
|
|
|
|
dir = "dist",
|
|
|
|
exclude = [],
|
|
|
|
filename = "jquery.js",
|
|
|
|
include = [],
|
|
|
|
slim = false,
|
|
|
|
version
|
|
|
|
} = {} ) {
|
|
|
|
|
|
|
|
// Add the short commit hash to the version string
|
|
|
|
// when the version is not for a release.
|
|
|
|
if ( !version ) {
|
|
|
|
const { stdout } = await exec( "git rev-parse --short HEAD" );
|
|
|
|
const isClean = await isCleanWorkingDir();
|
|
|
|
|
|
|
|
// Add "+SHA" if the version is not set.
|
|
|
|
// Add ".dirty" as well if the working dir is not clean.
|
2023-11-01 23:48:05 +00:00
|
|
|
version = `${ pkg.version }+${ stdout.trim() }${ isClean ? "" : ".dirty" }`;
|
2023-09-20 22:18:42 +00:00
|
|
|
}
|
2013-08-15 18:15:49 +00:00
|
|
|
|
2024-03-08 23:18:24 +00:00
|
|
|
await fs.mkdir( dir, { recursive: true } );
|
2023-09-20 22:18:42 +00:00
|
|
|
|
|
|
|
// Exclude slim modules when slim is true
|
|
|
|
const [ excluded, included ] = await checkExclude(
|
|
|
|
slim ? exclude.concat( excludedFromSlim ) : exclude,
|
|
|
|
include
|
|
|
|
);
|
|
|
|
const config = await getRequireConfig( { amd } );
|
|
|
|
|
|
|
|
// Replace exports/global with a noop noConflict
|
|
|
|
if ( excluded.includes( "exports/global" ) ) {
|
|
|
|
const index = excluded.indexOf( "exports/global" );
|
|
|
|
config.rawText[ "exports/global" ] = "define( [\n\t\"../core\"\n], " +
|
|
|
|
"function( jQuery ) {\n\tjQuery.noConflict = function() {};\n} );";
|
|
|
|
excluded.splice( index, 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
// "sizzle" is legacy for selector
|
|
|
|
if ( excluded.indexOf( "selector" ) > -1 || excluded.indexOf( "sizzle" ) > -1 ) {
|
|
|
|
config.rawText.selector = "define( [ \"./selector-native\" ] );";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( excluded.length ) {
|
|
|
|
|
|
|
|
// Append excluded modules to version.
|
|
|
|
// Skip adding exclusions for slim builds.
|
|
|
|
// Don't worry about semver syntax for these.
|
2013-08-15 18:15:49 +00:00
|
|
|
if ( excluded.length ) {
|
|
|
|
version += " -" + excluded.join( ",-" );
|
2023-09-20 22:18:42 +00:00
|
|
|
}
|
2015-08-16 06:59:58 +00:00
|
|
|
|
2023-09-20 22:18:42 +00:00
|
|
|
// Have to use shallow or core will get excluded since it is a dependency
|
|
|
|
config.excludeShallow = excluded;
|
|
|
|
}
|
2015-08-16 06:59:58 +00:00
|
|
|
|
2023-09-20 22:18:42 +00:00
|
|
|
if ( included.length ) {
|
2013-08-15 18:15:49 +00:00
|
|
|
config.include = included;
|
|
|
|
|
2023-09-20 22:18:42 +00:00
|
|
|
// Append extra included modules to version.
|
|
|
|
if ( included.length ) {
|
|
|
|
version += " +" + included.join( ",+" );
|
2013-09-09 02:26:05 +00:00
|
|
|
}
|
|
|
|
|
2023-09-20 22:18:42 +00:00
|
|
|
// Overwrite the default inclusions with the explicit ones provided
|
|
|
|
config.rawText.jquery = "define( [\n" +
|
|
|
|
included.map( module => "\t\"./" + module + "\"" ).join( ",\n" ) +
|
|
|
|
"\n] );";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle Final output from the optimizer
|
|
|
|
* @param {String} compiled
|
|
|
|
*/
|
|
|
|
config.out = async function( compiled ) {
|
|
|
|
const compiledContents = compiled
|
|
|
|
|
|
|
|
// Embed Version
|
|
|
|
.replace( /@VERSION/g, version )
|
|
|
|
|
|
|
|
// Embed Date
|
|
|
|
// yyyy-mm-ddThh:mmZ
|
|
|
|
.replace( /@DATE/g, new Date().toISOString().replace( /:\d+\.\d+Z$/, "Z" ) );
|
|
|
|
|
|
|
|
// Write concatenated source to file
|
2024-03-08 23:18:24 +00:00
|
|
|
await fs.writeFile(
|
2023-09-20 22:18:42 +00:00
|
|
|
path.join( dir, filename ),
|
|
|
|
compiledContents
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
await new Promise( ( resolve, reject ) => {
|
|
|
|
requirejs.optimize( config, () => {
|
|
|
|
|
|
|
|
// Wait a beat. For some reason, the write can
|
|
|
|
// take a moment after this to complete in Node 10.
|
|
|
|
setTimeout( resolve, 100 );
|
|
|
|
}, ( error ) => {
|
|
|
|
console.error( error );
|
|
|
|
reject( error );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
|
|
|
} );
|
2013-08-15 18:15:49 +00:00
|
|
|
|
2023-11-01 23:48:05 +00:00
|
|
|
console.log( `[${ getTimestamp() }] ${ filename } v${ version } created.` );
|
2023-09-20 22:18:42 +00:00
|
|
|
|
|
|
|
await minify( { filename, dir } );
|
|
|
|
}
|
|
|
|
|
2024-02-06 15:16:05 +00:00
|
|
|
async function buildDefaultFiles( {
|
|
|
|
version = process.env.VERSION
|
|
|
|
} = {} ) {
|
2023-09-20 22:18:42 +00:00
|
|
|
await Promise.all( [
|
|
|
|
build( { version } ),
|
|
|
|
build( { filename: "jquery.slim.js", slim: true, version } )
|
|
|
|
] );
|
|
|
|
|
|
|
|
// Earlier Node.js versions do not support the ESM format.
|
|
|
|
if ( !verifyNodeVersion() ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { compareSize } = await import( "./compare_size.mjs" );
|
|
|
|
return compareSize( {
|
|
|
|
files: [
|
|
|
|
"dist/jquery.min.js",
|
|
|
|
"dist/jquery.slim.min.js"
|
|
|
|
]
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2023-09-20 22:18:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = { build, buildDefaultFiles };
|