aboutsummaryrefslogtreecommitdiffstats
path: root/test/jquery.js
diff options
context:
space:
mode:
authorMichał Gołębiowski-Owczarek <m.goleb@gmail.com>2019-11-18 21:15:03 +0100
committerGitHub <noreply@github.com>2019-11-18 21:15:03 +0100
commitd0ce00cdfa680f1f0c38460bc51ea14079ae8b07 (patch)
tree8625b82760f0722443f3901f78d94025d7645463 /test/jquery.js
parenta612733be0c68d337647a6fcc8f8e0cabc1fc36b (diff)
downloadjquery-d0ce00cdfa680f1f0c38460bc51ea14079ae8b07.tar.gz
jquery-d0ce00cdfa680f1f0c38460bc51ea14079ae8b07.zip
Core: Migrate from AMD to ES modules 🎉
Migrate all source AMD modules to ECMAScript modules. The final bundle is compiled by a custom build process that uses Rollup under the hood. Test files themselves are still loaded via RequireJS as that has to work in IE 11. Tests can now be run in "Load as modules" mode which replaces the previous "Load with AMD" option. That option of running tests doesn't work in IE and Edge as it requires support for dynamic imports. Some of the changes required by the migration: * check `typeof` of `noGlobal` instead of using the variable directly as it's not available when modules are used * change the nonce module to be an object as ECMASscript module exports are immutable * remove some unused exports * import `./core/parseHTML.js` directly in `jquery.js` so that it's not being cut out when the `ajax` module is excluded in a custom compilation Closes gh-4541
Diffstat (limited to 'test/jquery.js')
-rw-r--r--test/jquery.js37
1 files changed, 21 insertions, 16 deletions
diff --git a/test/jquery.js b/test/jquery.js
index 6b1aef42f..d6d53fb26 100644
--- a/test/jquery.js
+++ b/test/jquery.js
@@ -2,7 +2,8 @@
( function() {
/* global loadTests: false */
- var FILEPATH = "/test/jquery.js",
+ var dynamicImportSource,
+ FILEPATH = "/test/jquery.js",
activeScript = [].slice.call( document.getElementsByTagName( "script" ), -1 )[ 0 ],
parentUrl = activeScript && activeScript.src ?
activeScript.src.replace( /[?#].*/, "" ) + FILEPATH.replace( /[^/]+/g, ".." ) + "/" :
@@ -21,12 +22,13 @@
// Define configuration parameters controlling how jQuery is loaded
if ( QUnit ) {
- // AMD loading is asynchronous and incompatible with synchronous test loading in Karma
+ // ES modules loading is asynchronous and incompatible with synchronous
+ // test loading in Karma.
if ( !window.__karma__ ) {
QUnit.config.urlConfig.push( {
- id: "amd",
- label: "Load with AMD",
- tooltip: "Load the AMD jQuery file (and its dependencies)"
+ id: "esmodules",
+ label: "Load as modules",
+ tooltip: "Load a relevant jQuery module file (and its dependencies)"
} );
}
@@ -39,18 +41,21 @@
// Honor AMD loading on the main window (detected by seeing QUnit on it).
// This doesn't apply to iframes because they synchronously expect jQuery to be there.
- if ( urlParams.amd && window.QUnit ) {
- require.config( {
- baseUrl: parentUrl
- } );
- src = "src/jquery";
+ if ( urlParams.esmodules && window.QUnit ) {
- // Include tests if specified
- if ( typeof loadTests !== "undefined" ) {
- require( [ src ], loadTests );
- } else {
- require( [ src ] );
- }
+ // Support: IE 11+, Edge 12 - 18+
+ // IE/Edge don't support the dynamic import syntax so they'd crash
+ // with a SyntaxError here.
+ dynamicImportSource = "" +
+ "import( `${ parentUrl }src/jquery.js` ).then( ( { default: jQuery } ) => {\n" +
+ " window.jQuery = jQuery;\n" +
+ " if ( typeof loadTests === \"function\" ) {\n" +
+ " // Include tests if specified\n" +
+ " loadTests();\n" +
+ " }\n" +
+ "} );";
+
+ eval( dynamicImportSource );
// Otherwise, load synchronously
} else {