From 8be4c0e4f89d6c8f780e5937a0534921d8c7815e Mon Sep 17 00:00:00 2001 From: Michał Gołębiowski-Owczarek Date: Mon, 10 Jul 2023 19:14:08 +0200 Subject: Build: Add `exports` to package.json, export slim & esm builds Summary of the changes: * define the `exports` field in `package.json`; `jQuery` & `$` are also exported as named exports in ESM builds now * declare `"type": "module"` globally except for the `build` folder * add the `--esm` option to `grunt custom`, generating jQuery as an ECMAScript module into the `dist-module` folder * expand `node_smoke_tests` to test the slim & ESM builds and their various combinations; also, test both jQuery loaded via a path to the file as well as from module specifiers that should be parsed via the `exports` feature * add details about ESM usage to the release package README * run `compare_size` on all built minified files; don't run it anymore on unminified files where they don't provide lots of value * remove the remove_map_comment task; SWC doesn't insert the `//# sourceMappingURL=` pragma by default so there's nothing to strip Fixes gh-4592 Closes gh-5255 --- build/fixtures/README.md | 138 ++++++++++++++++++++-- build/package.json | 3 + build/release.js | 23 ++-- build/release/cdn.js | 229 +++++++++++++++++++++--------------- build/tasks/build.js | 43 ++++--- build/tasks/dist.js | 25 ++-- build/tasks/node_smoke_tests.js | 66 +++++++---- build/tasks/promises_aplus_tests.js | 4 +- build/tasks/sourcemap.js | 17 --- 9 files changed, 360 insertions(+), 188 deletions(-) create mode 100644 build/package.json delete mode 100644 build/tasks/sourcemap.js (limited to 'build') diff --git a/build/fixtures/README.md b/build/fixtures/README.md index 360eb1c5c..a12e6121c 100644 --- a/build/fixtures/README.md +++ b/build/fixtures/README.md @@ -19,9 +19,78 @@ Below are some of the most common ways to include jQuery. ``` -#### Webpack / Browserify / Babel +or, to use the jQuery ECMAScript module: -There are several ways to use [Webpack](https://webpack.js.org/), [Browserify](http://browserify.org/) or [Babel](https://babeljs.io/). For more information on using these tools, please refer to the corresponding project's documentation. In the script, including jQuery will usually look like this: +```html + +``` + +Sometimes you don’t need AJAX, or you prefer to use one of the many standalone libraries that focus on AJAX requests. And often it is simpler to use a combination of CSS, class manipulation or the Web Animations API. Similarly, many projects opt into relying on native browser promises instead of jQuery Deferreds. Along with the regular version of jQuery that includes the `ajax`, `callbacks`, `deferred`, `effects` & `queue` modules, we’ve released a “slim” version that excludes these modules. You can load it as a regular script: + +```html + +``` + +or as a module: + +```html + +``` + +#### Import maps + +To avoid repeating long import paths that change on each jQuery release, you can use import maps - they are now supported in every modern browser. Put the following script tag before any ` +``` + +Now, the following will work to get the full version: + +```html + +``` + +and the following to get the slim one: + +```html + +``` + +The advantage of these specific mappings is they match the ones embedded in the jQuery npm package, providing better interoperability between the environments. + +You can also use jQuery from npm even in the browser setup. Read along for more details. + +### Using jQuery from npm + +There are several ways to use jQuery from npm. One is to use a build tool like [Webpack](https://webpack.js.org/), [Browserify](http://browserify.org/) or [Babel](https://babeljs.io/). For more information on using these tools, please refer to the corresponding project's documentation. + +Another way is to use jQuery directly in Node.js. See the [Node.js pre-requisites](#nodejs-pre-requisites) section for more details on the Node.js-specific part of this. + +To install the jQuery npm package, invoke: + +```sh +npm install jquery +``` + +In the script, including jQuery will usually look like this: ```js import $ from "jquery"; @@ -30,10 +99,30 @@ import $ from "jquery"; If you need to use jQuery in a file that's not an ECMAScript module, you can use the CommonJS syntax: ```js -var $ = require( "jquery" ); +const $ = require( "jquery" ); ``` -#### AMD (Asynchronous Module Definition) +#### Individual modules + +jQuery is authored in ECMAScript modules; it's also possible to use them directly. They are contained in the `src/` folder; inspect the package contents to see what's there. Full file names are required, including the `.js` extension. + +Be aware that this is an advanced & low-level interface, and we don't consider it stable, even between minor or patch releases - this is especially the case for modules in subdirectories or `src/`. If you rely on it, verify your setup before updating jQuery. + +All top-level modules, i.e. files directly in the `src/` directory export jQuery. Importing multiple modules will all attach to the same jQuery instance. + +Remember that some modules have other dependencies (e.g. the `event` module depends on the `selector` one) so in some cases you may get more than you expect. + +Example usage: + +```js +import $ from "jquery/src/css.js"; // adds the `.css()` method +import "jquery/src/event.js"; // adds the `.on()` method; pulls "selector" as a dependency +$( ".toggle" ).on( "click", function() { + $( this ).css( "color", "red" ); +} ); +``` + +### AMD (Asynchronous Module Definition) AMD is a module format built for the browser. For more information, we recommend [require.js' documentation](https://requirejs.org/docs/whyamd.html). @@ -43,18 +132,49 @@ define( [ "jquery" ], function( $ ) { } ); ``` -### Node +Node.js doesn't understand AMD natively so this method is mostly used in a browser setup. -To include jQuery in [Node](https://nodejs.org/), first install with npm. +### Node.js pre-requisites -```sh -npm install jquery +For jQuery to work in Node, a window with a document is required. Since no such window exists natively in Node, one can be mocked by tools such as [jsdom](https://github.com/jsdom/jsdom). This can be useful for testing purposes. + +jQuery checks for a `window` global with a `document` property and - if one is not present, as is the default in Node.js - it returns a factory accepting a `window` as a parameter instead. + +To `import` jQuery using this factory, use the following: + +```js +import { JSDOM } from "jsdom"; +const { window } = new JSDOM( "" ); +import jQueryFactory from "jquery"; +const $ = jQueryFactory( window ); ``` -For jQuery to work in Node, a window with a document is required. Since no such window exists natively in Node, one can be mocked by tools such as [jsdom](https://github.com/jsdom/jsdom). This can be useful for testing purposes. +or, if you use `require`: ```js const { JSDOM } = require( "jsdom" ); const { window } = new JSDOM( "" ); const $ = require( "jquery" )( window ); ``` + +If the `window` global is present at the moment of the `import` or `require` of `"jquery"`, it will resolve to a jQuery instance, as in the browser. You can set such a global manually to simulate the behavior; with `import`: + +```js +import { JSDOM } from "jsdom"; +const { window } = new JSDOM( "" ); +globalThis.window = window; +const { default: $ } = await import( "jquery" ); +``` + +or with `require`: + +```js +const { JSDOM } = require( "jsdom" ); +const { window } = new JSDOM( "" ); +globalThis.window = window; +const $ = require( "jquery" ); +``` + +#### Slim build in Node.js + +To use the slim build of jQuery in Node.js, use `"jquery/slim"` instead of `"jquery"` in both `require` or `import` calls above. diff --git a/build/package.json b/build/package.json new file mode 100644 index 000000000..5bbefffba --- /dev/null +++ b/build/package.json @@ -0,0 +1,3 @@ +{ + "type": "commonjs" +} diff --git a/build/release.js b/build/release.js index 379f7de0a..f4509931a 100644 --- a/build/release.js +++ b/build/release.js @@ -10,7 +10,13 @@ module.exports = function( Release ) { "dist/jquery.min.map", "dist/jquery.slim.js", "dist/jquery.slim.min.js", - "dist/jquery.slim.min.map" + "dist/jquery.slim.min.map", + "dist-module/jquery.module.js", + "dist-module/jquery.module.min.js", + "dist-module/jquery.module.min.map", + "dist-module/jquery.slim.module.js", + "dist-module/jquery.slim.module.min.js", + "dist-module/jquery.slim.module.min.map" ]; const filesToCommit = [ ...distFiles, @@ -45,12 +51,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 ); @@ -72,10 +74,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 ); } } ); }; diff --git a/build/release/cdn.js b/build/release/cdn.js index 0cbea4a7a..a75ad7303 100644 --- a/build/release/cdn.js +++ b/build/release/cdn.js @@ -1,130 +1,163 @@ "use strict"; -var - fs = require( "fs" ), - shell = require( "shelljs" ), - path = require( "path" ), - os = require( "os" ), - - cdnFolder = "dist/cdn", - - releaseFiles = { - "jquery-VER.js": "dist/jquery.js", - "jquery-VER.min.js": "dist/jquery.min.js", - "jquery-VER.min.map": "dist/jquery.min.map", - "jquery-VER.slim.js": "dist/jquery.slim.js", - "jquery-VER.slim.min.js": "dist/jquery.slim.min.js", - "jquery-VER.slim.min.map": "dist/jquery.slim.min.map" - }, - - googleFilesCDN = [ - "jquery.js", "jquery.min.js", "jquery.min.map", - "jquery.slim.js", "jquery.slim.min.js", "jquery.slim.min.map" - ], - - msFilesCDN = [ - "jquery-VER.js", "jquery-VER.min.js", "jquery-VER.min.map", - "jquery-VER.slim.js", "jquery-VER.slim.min.js", "jquery-VER.slim.min.map" - ]; +const fs = require( "fs" ); +const shell = require( "shelljs" ); +const path = require( "path" ); +const os = require( "os" ); + +const cdnFolderContainer = "dist/cdn"; +const cdnFolderVersioned = `${ cdnFolderContainer }/versioned`; +const cdnFolderUnversioned = `${ cdnFolderContainer }/unversioned`; + +const versionedReleaseFiles = { + "jquery-@VER.js": "dist/jquery.js", + "jquery-@VER.min.js": "dist/jquery.min.js", + "jquery-@VER.min.map": "dist/jquery.min.map", + "jquery-@VER.slim.js": "dist/jquery.slim.js", + "jquery-@VER.slim.min.js": "dist/jquery.slim.min.js", + "jquery-@VER.slim.min.map": "dist/jquery.slim.min.map", + "jquery-@VER.module.js": "dist-module/jquery.module.js", + "jquery-@VER.module.min.js": "dist-module/jquery.module.min.js", + "jquery-@VER.module.min.map": "dist-module/jquery.module.min.map", + "jquery-@VER.slim.module.js": "dist-module/jquery.slim.module.js", + "jquery-@VER.slim.module.min.js": "dist-module/jquery.slim.module.min.js", + "jquery-@VER.slim.module.min.map": "dist-module/jquery.slim.module.min.map" +}; + +const unversionedReleaseFiles = { + "jquery.js": "dist/jquery.js", + "jquery.min.js": "dist/jquery.min.js", + "jquery.min.map": "dist/jquery.min.map", + "jquery.slim.js": "dist/jquery.slim.js", + "jquery.slim.min.js": "dist/jquery.slim.min.js", + "jquery.slim.min.map": "dist/jquery.slim.min.map", + "jquery.module.js": "dist-module/jquery.module.js", + "jquery.module.min.js": "dist-module/jquery.module.min.js", + "jquery.module.min.map": "dist-module/jquery.module.min.map", + "jquery.slim.module.js": "dist-module/jquery.slim.module.js", + "jquery.slim.module.min.js": "dist-module/jquery.slim.module.min.js", + "jquery.slim.module.min.map": "dist-module/jquery.slim.module.min.map" +}; /** * Generates copies for the CDNs */ function makeReleaseCopies( Release ) { - shell.mkdir( "-p", cdnFolder ); - - Object.keys( releaseFiles ).forEach( function( key ) { - var text, - builtFile = releaseFiles[ key ], - unpathedFile = key.replace( /VER/g, Release.newVersion ), - releaseFile = cdnFolder + "/" + unpathedFile; - - if ( /\.map$/.test( releaseFile ) ) { - - // Map files need to reference the new uncompressed name; - // assume that all files reside in the same directory. - // "file":"jquery.min.js" ... "sources":["jquery.js"] - text = fs.readFileSync( builtFile, "utf8" ) - .replace( /"file":"([^"]+)"/, - "\"file\":\"" + unpathedFile.replace( /\.min\.map/, ".min.js\"" ) ) - .replace( /"sources":\["([^"]+)"\]/, - "\"sources\":[\"" + unpathedFile.replace( /\.min\.map/, ".js" ) + "\"]" ); - fs.writeFileSync( releaseFile, text ); - } else if ( builtFile !== releaseFile ) { - shell.cp( "-f", builtFile, releaseFile ); - } + [ + { filesMap: versionedReleaseFiles, cdnFolder: cdnFolderVersioned }, + { filesMap: unversionedReleaseFiles, cdnFolder: cdnFolderUnversioned } + ].forEach( ( { filesMap, cdnFolder } ) => { + shell.mkdir( "-p", cdnFolder ); + + Object.keys( filesMap ).forEach( key => { + let text; + const builtFile = filesMap[ key ]; + const unpathedFile = key.replace( /@VER/g, Release.newVersion ); + const releaseFile = cdnFolder + "/" + unpathedFile; + + if ( /\.map$/.test( releaseFile ) ) { + + // Map files need to reference the new uncompressed name; + // assume that all files reside in the same directory. + // "file":"jquery.min.js" ... "sources":["jquery.js"] + text = fs.readFileSync( builtFile, "utf8" ) + .replace( /"file":"([^"]+)"/, + "\"file\":\"" + unpathedFile.replace( /\.min\.map/, ".min.js\"" ) ) + .replace( /"sources":\["([^"]+)"\]/, + "\"sources\":[\"" + unpathedFile.replace( /\.min\.map/, ".js" ) + "\"]" ); + fs.writeFileSync( releaseFile, text ); + } else if ( builtFile !== releaseFile ) { + shell.cp( "-f", builtFile, releaseFile ); + } + } ); + } ); } -function makeArchives( Release, callback ) { +async function makeArchives( Release ) { Release.chdir( Release.dir.repo ); - function makeArchive( cdn, files, callback ) { - if ( Release.preRelease ) { - console.log( "Skipping archive creation for " + cdn + "; this is a beta release." ); - callback(); - return; - } + async function makeArchive( { cdn, filesMap, cdnFolder } ) { + return new Promise( ( resolve, reject ) => { + if ( Release.preRelease ) { + console.log( "Skipping archive creation for " + cdn + "; this is a beta release." ); + resolve(); + return; + } - console.log( "Creating production archive for " + cdn ); + console.log( "Creating production archive for " + cdn ); - var i, sum, result, - archiver = require( "archiver" )( "zip" ), - md5file = cdnFolder + "/" + cdn + "-md5.txt", - output = fs.createWriteStream( + let i, sum, result; + const archiver = require( "archiver" )( "zip" ); + const md5file = cdnFolder + "/" + cdn + "-md5.txt"; + const output = fs.createWriteStream( cdnFolder + "/" + cdn + "-jquery-" + Release.newVersion + ".zip" - ), - rmd5 = /[a-f0-9]{32}/, - rver = /VER/; + ); + const rmd5 = /[a-f0-9]{32}/; + const rver = /@VER/; - output.on( "close", callback ); + output.on( "close", resolve ); - output.on( "error", function( err ) { - throw err; - } ); + output.on( "error", err => { + reject( err ); + } ); - archiver.pipe( output ); + archiver.pipe( output ); - files = files.map( function( item ) { - return "dist" + ( rver.test( item ) ? "/cdn" : "" ) + "/" + - item.replace( rver, Release.newVersion ); - } ); + let finalFilesMap = Object.create( null ); + for ( const [ releaseFile, builtFile ] of Object.entries( filesMap ) ) { + finalFilesMap[ releaseFile.replace( rver, Release.newVersion ) ] = builtFile; + } - if ( os.platform() === "win32" ) { - sum = []; - for ( i = 0; i < files.length; i++ ) { - result = Release.exec( - "certutil -hashfile " + files[ i ] + " MD5", "Error retrieving md5sum" - ); - sum.push( rmd5.exec( result )[ 0 ] + " " + files[ i ] ); + const files = Object + .keys( filesMap ) + .map( item => `${ cdnFolder }/${ + item.replace( rver, Release.newVersion ) + }` ); + + if ( os.platform() === "win32" ) { + sum = []; + for ( i = 0; i < files.length; i++ ) { + result = Release.exec( + "certutil -hashfile " + files[ i ] + " MD5", "Error retrieving md5sum" + ); + sum.push( rmd5.exec( result )[ 0 ] + " " + files[ i ] ); + } + sum = sum.join( "\n" ); + } else { + sum = Release.exec( "md5 -r " + files.join( " " ), "Error retrieving md5sum" ); } - sum = sum.join( "\n" ); - } else { - sum = Release.exec( "md5 -r " + files.join( " " ), "Error retrieving md5sum" ); - } - fs.writeFileSync( md5file, sum ); - files.push( md5file ); - - files.forEach( function( file ) { - archiver.append( fs.createReadStream( file ), - { name: path.basename( file ) } ); - } ); + fs.writeFileSync( md5file, sum ); + files.push( md5file ); + + files.forEach( file => { + archiver.append( fs.createReadStream( file ), + { name: path.basename( file ) } ); + } ); - archiver.finalize(); + archiver.finalize(); + } ); } - function buildGoogleCDN( callback ) { - makeArchive( "googlecdn", googleFilesCDN, callback ); + async function buildGoogleCDN() { + await makeArchive( { + cdn: "googlecdn", + filesMap: unversionedReleaseFiles, + cdnFolder: cdnFolderUnversioned + } ); } - function buildMicrosoftCDN( callback ) { - makeArchive( "mscdn", msFilesCDN, callback ); + async function buildMicrosoftCDN() { + await makeArchive( { + cdn: "mscdn", + filesMap: versionedReleaseFiles, + cdnFolder: cdnFolderVersioned + } ); } - buildGoogleCDN( function() { - buildMicrosoftCDN( callback ); - } ); + await buildGoogleCDN(); + await buildMicrosoftCDN(); } module.exports = { diff --git a/build/tasks/build.js b/build/tasks/build.js index e1dd39993..79498d012 100644 --- a/build/tasks/build.js +++ b/build/tasks/build.js @@ -17,27 +17,33 @@ module.exports = function( grunt ) { return grunt.file.read( `${ srcFolder }/${ fileName }` ); }; - // Catch `// @CODE` and subsequent comment lines event if they don't start - // in the first column. - const wrapper = read( "wrapper.js" ) - .split( /[\x20\t]*\/\/ @CODE\n(?:[\x20\t]*\/\/[^\n]+\n)*/ ); - const inputFileName = "jquery.js"; const inputRollupOptions = { input: `${ srcFolder }/${ inputFileName }` }; - const outputRollupOptions = { - // The ESM format is not actually used as we strip it during - // the build; it's just that it doesn't generate any extra - // wrappers so there's nothing for us to remove. - format: "esm", + function getOutputRollupOptions( { + esm = false + } = {} ) { + const wrapperFileName = `wrapper${ esm ? "-esm" : "" }.js`; + + // Catch `// @CODE` and subsequent comment lines event if they don't start + // in the first column. + const wrapper = read( wrapperFileName ) + .split( /[\x20\t]*\/\/ @CODE\n(?:[\x20\t]*\/\/[^\n]+\n)*/ ); + + return { + + // The ESM format is not actually used as we strip it during the + // build, inserting our own wrappers; it's just that it doesn't + // generate any extra wrappers so there's nothing for us to remove. + format: "esm", + + intro: `${ wrapper[ 0 ].replace( /\n*$/, "" ) }`, + outro: wrapper[ 1 ].replace( /^\n*/, "" ) + }; + } - intro: wrapper[ 0 ] - .replace( /\n*$/, "" ), - outro: wrapper[ 1 ] - .replace( /^\n*/, "" ) - }; const fileOverrides = new Map(); function getOverride( filePath ) { @@ -62,6 +68,8 @@ module.exports = function( grunt ) { const flags = this.flags; const optIn = flags[ "*" ]; let name = grunt.option( "filename" ); + const esm = !!grunt.option( "esm" ); + const distFolder = grunt.option( "dist-folder" ); const minimum = this.data.minimum; const removeWith = this.data.removeWith; const excluded = []; @@ -185,7 +193,7 @@ module.exports = function( grunt ) { // 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; + name = name ? `${ distFolder }/${ name }` : this.data.dest; // append commit id to version if ( process.env.COMMIT ) { @@ -296,6 +304,9 @@ module.exports = function( grunt ) { plugins: [ rollupFileOverrides( fileOverrides ) ] } ); + const outputRollupOptions = + getOutputRollupOptions( { esm } ); + const { output: [ { code } ] } = await bundle.generate( outputRollupOptions ); const compiledContents = code diff --git a/build/tasks/dist.js b/build/tasks/dist.js index f19929b89..36ce38b00 100644 --- a/build/tasks/dist.js +++ b/build/tasks/dist.js @@ -1,17 +1,18 @@ "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 distFolder = grunt.option( "dist-folder" ); + const distPaths = [ + `${ distFolder }/${ filename }`, + `${ distFolder }/${ filename.replace( ".js", ".min.js" ) }`, + `${ distFolder }/${ 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 ) @@ -28,9 +29,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 ) ) { @@ -54,7 +55,7 @@ module.exports = function( grunt ) { // Optionally copy dist files to other locations paths.forEach( function( path ) { - var created; + let created; if ( !/\/$/.test( path ) ) { path += "/"; diff --git a/build/tasks/node_smoke_tests.js b/build/tasks/node_smoke_tests.js index 574a63b4a..7edbac881 100644 --- a/build/tasks/node_smoke_tests.js +++ b/build/tasks/node_smoke_tests.js @@ -3,29 +3,49 @@ 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( moduleType, jQueryModuleSpecifier ) { + if ( + ( moduleType !== "commonjs" && moduleType !== "module" ) || + !jQueryModuleSpecifier + ) { + grunt.fatal( "Use `node_smoke_tests:commonjs:JQUERY` " + + "or `node_smoke_tests:module:JQUERY.\n" + + "JQUERY can be `jquery`, `jquery/slim` or a path to any of them." ); + } + + if ( !nodeV16OrNewer ) { + grunt.log.writeln( "Old Node.js detected, running the task " + + `"node_smoke_tests:${ moduleType }:${ jQueryModuleSpecifier }" skipped...` ); + return; + } + + const testsDir = `./test/node_smoke_tests/${ moduleType }`; + const nodeSmokeTests = []; - nodeSmokeTests.push( taskName ); - } ); + // 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() && + /\.[cm]?js$/.test( testFilePath ) + ) + .forEach( ( testFilePath ) => { + const taskName = `node_${ testFilePath.replace( /\.[cm]?js$/, "" ) }:${ moduleType }:${ 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 ); + } ); }; diff --git a/build/tasks/promises_aplus_tests.js b/build/tasks/promises_aplus_tests.js index cb1a4fddb..1bbeff08e 100644 --- a/build/tasks/promises_aplus_tests.js +++ b/build/tasks/promises_aplus_tests.js @@ -10,7 +10,7 @@ module.exports = grunt => { grunt.registerTask( "promises_aplus_tests:deferred", function() { spawnTest( this.async(), "\"" + __dirname + "/../../node_modules/.bin/promises-aplus-tests\"" + - " test/promises_aplus_adapters/deferred.js" + + " test/promises_aplus_adapters/deferred.cjs" + " --reporter dot" + " --timeout " + timeout ); @@ -19,7 +19,7 @@ module.exports = grunt => { grunt.registerTask( "promises_aplus_tests:when", function() { spawnTest( this.async(), "\"" + __dirname + "/../../node_modules/.bin/promises-aplus-tests\"" + - " test/promises_aplus_adapters/when.js" + + " test/promises_aplus_adapters/when.cjs" + " --reporter dot" + " --timeout " + timeout ); diff --git a/build/tasks/sourcemap.js b/build/tasks/sourcemap.js deleted file mode 100644 index 8b9c248e0..000000000 --- a/build/tasks/sourcemap.js +++ /dev/null @@ -1,17 +0,0 @@ -"use strict"; - -var fs = require( "fs" ); - -module.exports = function( grunt ) { - var config = grunt.config( "minify.all.files" ); - grunt.registerTask( "remove_map_comment", function() { - var minLoc = grunt.config.process( Object.keys( config )[ 0 ] ); - - // Remove the source map comment; it causes way too many problems. - // The map file is still generated for manual associations - // https://github.com/jquery/jquery/issues/1707 - var text = fs.readFileSync( minLoc, "utf8" ) - .replace( /\/\/# sourceMappingURL=\S+/, "" ); - fs.writeFileSync( minLoc, text ); - } ); -}; -- cgit v1.2.3