Skip to content

Commit

Permalink
Build: Generate the slim build on grunt & run compare_size on it
Browse files Browse the repository at this point in the history
Summary of the changes:
* expand `node_smoke_tests` to test the full & slim builds
* run `compare_size` on all built minified files; don't run it anymore on
  unminified files where they don't provide lots of value

The main goal of this change is to make it easier to compare sizes of both the
full & slim builds between the `3.x-stable` & `main` branches.

Closes gh-5291
Ref gh-5255

(partially cherry-picked from commit 8be4c0e)
  • Loading branch information
mgol committed Jul 10, 2023
1 parent a288838 commit 763ade6
Show file tree
Hide file tree
Showing 21 changed files with 202 additions and 111 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Expand Up @@ -4,6 +4,8 @@ node_modules
dist/**
!dist/jquery.js
!dist/jquery.min.js
!dist/jquery.slim.js
!dist/jquery.slim.min.js
test/data/jquery-1.9.1.js
test/data/badcall.js
test/data/badjson.js
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -12,7 +12,7 @@ package-lock.json

npm-debug.log*

# Ignore everything in dist folder except for eslint config
# Ignore everything in `dist` folder except for the ESLint config
/dist/*
!/dist/.eslintrc.json

Expand Down
5 changes: 1 addition & 4 deletions .npmignore
Expand Up @@ -6,10 +6,7 @@
/.mailmap

/build
/external
/speed
/test
/Gruntfile.js

/external/qunit
/external/requirejs
/external/sinon
53 changes: 37 additions & 16 deletions Gruntfile.js
Expand Up @@ -13,19 +13,19 @@ module.exports = function( grunt ) {
}

const fs = require( "fs" );
const { spawn } = require( "child_process" );
const gzip = require( "gzip-js" );
const nodeV14OrNewer = !/^v1[0-3]\./.test( process.version );
const nodeV16OrNewer = !/^v1[0-5]\./.test( process.version );
const nodeV17OrNewer = !/^v1[0-6]\./.test( process.version );
const customBrowsers = process.env.BROWSERS && process.env.BROWSERS.split( "," );

// Support: Node.js <14
// Skip running tasks that dropped support for Node.js 10 or 12
// in this Node version.
// Support: Node.js <16
// Skip running tasks that dropped support for old Node.js in these Node versions.
function runIfNewNode( task ) {
return nodeV14OrNewer ? task : "print_old_node_message:" + task;
return nodeV16OrNewer ? task : "print_old_node_message:" + task;
}

if ( nodeV14OrNewer ) {
if ( nodeV16OrNewer ) {
const playwright = require( "playwright-webkit" );
process.env.WEBKIT_HEADLESS_BIN = playwright.webkit.executablePath();
}
Expand All @@ -34,11 +34,21 @@ module.exports = function( grunt ) {
grunt.option( "filename", "jquery.js" );
}

const builtJsFiles = [
"dist/jquery.js",
"dist/jquery.min.js",
"dist/jquery.slim.js",
"dist/jquery.slim.min.js"
];

const builtJsMinFiles = builtJsFiles
.filter( filepath => filepath.endsWith( ".min.js" ) );

grunt.initConfig( {
pkg: grunt.file.readJSON( "package.json" ),
dst: readOptionalJSON( "dist/.destination.json" ),
"compare_size": {
files: [ "dist/jquery.js", "dist/jquery.min.js" ],
compare_size: {
files: builtJsMinFiles,
options: {
compress: {
gz: function( contents ) {
Expand Down Expand Up @@ -124,7 +134,7 @@ module.exports = function( grunt ) {
// We have to explicitly declare "src" property otherwise "newer"
// task wouldn't work properly :/
dist: {
src: [ "dist/jquery.js", "dist/jquery.min.js" ]
src: builtJsFiles
},
dev: {
src: [ "src/**/*.js", "Gruntfile.js", "test/**/*.js", "build/**/*.js" ]
Expand Down Expand Up @@ -308,7 +318,7 @@ module.exports = function( grunt ) {
uglify: {
all: {
files: {
"dist/<%= grunt.option('filename').replace('.js', '.min.js') %>":
"dist/<%= grunt.option('filename').replace(/\\.js$/, '.min.js') %>":
"dist/<%= grunt.option('filename') %>"
},
options: {
Expand Down Expand Up @@ -343,7 +353,7 @@ module.exports = function( grunt ) {

// Load grunt tasks from NPM packages
require( "load-grunt-tasks" )( grunt, {
pattern: nodeV14OrNewer ? [ "grunt-*" ] : [ "grunt-*", "!grunt-eslint" ]
pattern: nodeV16OrNewer ? [ "grunt-*" ] : [ "grunt-*", "!grunt-eslint" ]
} );

// Integrate jQuery specific tasks
Expand All @@ -354,6 +364,20 @@ module.exports = function( grunt ) {
grunt.log.writeln( "Old Node.js detected, running the task \"" + task + "\" skipped..." );
} );

grunt.registerTask( "build-all-variants",
"Build both the full & slim builds",
function() {
const done = this.async();

spawn( "npm run build-all-variants", {
stdio: "inherit",
shell: true
} )
.on( "close", code => {
done( code === 0 );
} );
} );

grunt.registerTask( "print_jsdom_message", () => {
grunt.log.writeln( "Node.js 17 or newer detected, skipping jsdom tests..." );
} );
Expand All @@ -377,7 +401,7 @@ module.exports = function( grunt ) {
runIfNewNode( "newer:eslint:dist" )
] );

grunt.registerTask( "test:fast", runIfNewNode( "node_smoke_tests" ) );
grunt.registerTask( "test:fast", [ "node_smoke_tests" ] );
grunt.registerTask( "test:slow", [
runIfNewNode( "promises_aplus_tests" ),

Expand Down Expand Up @@ -410,10 +434,7 @@ module.exports = function( grunt ) {

grunt.registerTask( "default", [
runIfNewNode( "eslint:dev" ),
"build:*:*",
"uglify",
"remove_map_comment",
"dist:*",
"build-all-variants",
"test:prepare",
runIfNewNode( "eslint:dist" ),
"test:fast",
Expand Down
12 changes: 11 additions & 1 deletion README.md
Expand Up @@ -117,7 +117,7 @@ The build process shows a message for each dependent module it excludes or inclu

##### AMD name

As an option, you can set the module name for jQuery's AMD definition. By default, it is set to "jquery", which plays nicely with plugins and third-party libraries, but there may be cases where you'd like to change this. Simply set the `"amd"` option:
As an option, you can set the module name for jQuery's AMD definition. By default, it is set to "jquery", which plays nicely with plugins and third-party libraries, but there may be cases where you'd like to change this. Simply pass it to the `--amd` parameter:

```bash
grunt custom --amd="custom-name"
Expand All @@ -129,6 +129,16 @@ Or, to define anonymously, set the name to an empty string.
grunt custom --amd=""
```

##### File name

The default name for the built jQuery file is `jquery.js`; it is placed under the `dist/` directory. It's possible to change the file name using the `--filename` parameter:

```bash
grunt custom:slim --filename="jquery.slim.js"
```

This would create a slim version of jQuery and place it under `dist/jquery.slim.js`. In fact, this is exactly the command we use to generate the slim jQuery during the release process.

#### Custom Build Examples

To create a custom build, first check out the version:
Expand Down
15 changes: 5 additions & 10 deletions build/release.js
Expand Up @@ -42,12 +42,8 @@ module.exports = function( Release ) {
* @param {Function} callback
*/
generateArtifacts: function( callback ) {
Release.exec( "npx grunt", "Grunt command failed" );
Release.exec(
"npx grunt custom:slim --filename=jquery.slim.js && " +
"npx grunt remove_map_comment --filename=jquery.slim.js",
"Grunt custom failed"
);
Release.exec( "npx grunt" );

cdn.makeReleaseCopies( Release );
Release._setSrcVersion();
callback( filesToCommit );
Expand All @@ -69,10 +65,9 @@ module.exports = function( Release ) {
* Publish to distribution repo and npm
* @param {Function} callback
*/
dist: function( callback ) {
cdn.makeArchives( Release, function() {
dist( Release, distFiles, callback );
} );
dist: async callback => {
await cdn.makeArchives( Release );
dist( Release, distFiles, callback );
}
} );
};
Expand Down
7 changes: 6 additions & 1 deletion build/tasks/build.js
Expand Up @@ -358,6 +358,11 @@ module.exports = function( grunt ) {
"";

grunt.log.writeln( "Creating custom build...\n" );
grunt.task.run( [ "build:*:*" + ( modules ? ":" + modules : "" ), "uglify", "dist" ] );
grunt.task.run( [
"build:*:*" + ( modules ? ":" + modules : "" ),
"uglify",
"remove_map_comment",
"dist"
] );
} );
};
24 changes: 12 additions & 12 deletions build/tasks/dist.js
@@ -1,17 +1,17 @@
"use strict";

module.exports = function( grunt ) {
var fs = require( "fs" ),
filename = grunt.option( "filename" ),
distpaths = [
"dist/" + filename,
"dist/" + filename.replace( ".js", ".min.map" ),
"dist/" + filename.replace( ".js", ".min.js" )
];
const fs = require( "fs" );
const filename = grunt.option( "filename" );
const distPaths = [
`dist/${ filename }`,
`dist/${ filename.replace( ".js", ".min.js" ) }`,
`dist/${ filename.replace( ".js", ".min.map" ) }`
];

// Process files for distribution
grunt.registerTask( "dist", function() {
var stored, flags, paths, nonascii;
let stored, flags, paths, nonascii;

// Check for stored destination paths
// ( set in dist/.destination.json )
Expand All @@ -28,9 +28,9 @@ module.exports = function( grunt ) {
// Ensure the dist files are pure ASCII
nonascii = false;

distpaths.forEach( function( filename ) {
var i, c,
text = fs.readFileSync( filename, "utf8" );
distPaths.forEach( function( filename ) {
let i, c;
const text = fs.readFileSync( filename, "utf8" );

// Ensure files use only \n for line endings, not \r\n
if ( /\x0d\x0a/.test( text ) ) {
Expand All @@ -54,7 +54,7 @@ module.exports = function( grunt ) {

// Optionally copy dist files to other locations
paths.forEach( function( path ) {
var created;
let created;

if ( !/\/$/.test( path ) ) {
path += "/";
Expand Down
57 changes: 34 additions & 23 deletions build/tasks/node_smoke_tests.js
Expand Up @@ -3,29 +3,40 @@
module.exports = ( grunt ) => {
const fs = require( "fs" );
const spawnTest = require( "./lib/spawn_test.js" );
const testsDir = "./test/node_smoke_tests/";
const nodeSmokeTests = [];

// Fire up all tests defined in test/node_smoke_tests/*.js in spawned sub-processes.
// All the files under test/node_smoke_tests/*.js are supposed to exit with 0 code
// on success or another one on failure. Spawning in sub-processes is
// important so that the tests & the main process don't interfere with
// each other, e.g. so that they don't share the require cache.

fs.readdirSync( testsDir )
.filter( ( testFilePath ) =>
fs.statSync( testsDir + testFilePath ).isFile() &&
/\.js$/.test( testFilePath )
)
.forEach( ( testFilePath ) => {
const taskName = `node_${ testFilePath.replace( /\.js$/, "" ) }`;

grunt.registerTask( taskName, function() {
spawnTest( this.async(), `node "test/node_smoke_tests/${ testFilePath }"` );
} );
const nodeV16OrNewer = !/^v1[0-5]\./.test( process.version );

grunt.registerTask( "node_smoke_tests", function( jQueryModuleSpecifier = "./dist/jquery.js" ) {
if ( !nodeV16OrNewer ) {
grunt.log.writeln( "Old Node.js detected, running the task " +
`"node_smoke_tests:${ jQueryModuleSpecifier }" skipped...` );
return;
}

const testsDir = "./test/node_smoke_tests";
const nodeSmokeTests = [];

// Fire up all tests defined in test/node_smoke_tests/*.js in spawned sub-processes.
// All the files under test/node_smoke_tests/*.js are supposed to exit with 0 code
// on success or another one on failure. Spawning in sub-processes is
// important so that the tests & the main process don't interfere with
// each other, e.g. so that they don't share the `require` cache.

nodeSmokeTests.push( taskName );
} );
fs.readdirSync( testsDir )
.filter( ( testFilePath ) =>
fs.statSync( `${ testsDir }/${ testFilePath }` ).isFile() &&
/\.[cm]?js$/.test( testFilePath )
)
.forEach( ( testFilePath ) => {
const taskName = `node_${ testFilePath.replace( /\.[cm]?js$/, "" ) }:${ jQueryModuleSpecifier }`;

grunt.registerTask( taskName, function() {
spawnTest( this.async(), `node "${ testsDir }/${
testFilePath }" ${ jQueryModuleSpecifier }` );
} );

nodeSmokeTests.push( taskName );
} );

grunt.registerTask( "node_smoke_tests", nodeSmokeTests );
grunt.task.run( nodeSmokeTests );
} );
};
16 changes: 15 additions & 1 deletion dist/.eslintrc.json
Expand Up @@ -8,12 +8,26 @@

"overrides": [
{
"files": "jquery.js",
"files": "jquery{,.slim}.min.js",

"parserOptions": {
"ecmaVersion": 5,
"sourceType": "script"
}
},

{
"files": "jquery{,.slim}.js",
"extends": "../.eslintrc-browser.json",

"rules": {

// That is okay for the built version
"no-multiple-empty-lines": "off",

// When custom compilation is used, the version string
// can get large. Accept that in the built version.
"max-len": "off",
"one-var": "off"
}
}
Expand Down
10 changes: 7 additions & 3 deletions package.json
Expand Up @@ -68,15 +68,19 @@
"uglify-js": "3.4.7"
},
"scripts": {
"build": "npm install && grunt",
"build": "npm install && npm run build-all-variants",
"build-all-variants": "grunt custom:slim --filename=jquery.slim.js && grunt custom",
"start": "grunt watch",
"test:browserless": "grunt && grunt test:slow",
"test:browserless": "grunt && npm run test:node_smoke_tests && grunt test:slow",
"test:browser": "grunt && grunt karma:main",
"test:amd": "grunt && grunt karma:amd",
"test:no-deprecated": "grunt test:prepare && grunt custom:-deprecated && grunt karma:main",
"test:selector-native": "grunt test:prepare && grunt custom:-selector && grunt karma:main",
"test:slim": "grunt test:prepare && grunt custom:slim && grunt karma:main",
"test": "npm run test:slim && npm run test:no-deprecated && npm run test:selector-native && grunt && grunt test:slow && grunt karma:main && grunt karma:amd",
"test:node_smoke_tests:full": "grunt node_smoke_tests:./dist/jquery.js",
"test:node_smoke_tests:slim": "grunt node_smoke_tests:./dist/jquery.slim.js",
"test:node_smoke_tests": "npm run test:node_smoke_tests:full && npm run test:node_smoke_tests:slim",
"test": "npm run test:browserless && npm run test:slim && npm run test:no-deprecated && npm run test:selector-native && grunt && grunt test:slow && grunt karma:main && grunt karma:amd",
"jenkins": "npm run test:browserless"
},
"commitplease": {
Expand Down
6 changes: 3 additions & 3 deletions test/node_smoke_tests/.eslintrc.json
Expand Up @@ -4,10 +4,10 @@
"extends": "../../.eslintrc-node.json",

"parserOptions": {
"ecmaVersion": 6
"ecmaVersion": 2015,
"sourceType": "script"
},

"env": {
"es6": true
"es2020": true
}
}

0 comments on commit 763ade6

Please sign in to comment.