aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMichał Gołębiowski-Owczarek <m.goleb@gmail.com>2023-09-19 18:58:24 +0200
committerGitHub <noreply@github.com>2023-09-19 18:58:24 +0200
commit46f6e3da796ee9d28c7c1428793b72d66bcbb0b7 (patch)
treeb4c2810c9c39816db7d937b6a02803d55dab1728 /src
parentb923047d29d37f2d5c96f8b33992f322bc7b7944 (diff)
downloadjquery-46f6e3da796ee9d28c7c1428793b72d66bcbb0b7.tar.gz
jquery-46f6e3da796ee9d28c7c1428793b72d66bcbb0b7.zip
Core: Move the factory to separate exports
Since versions 1.11.0/2.1.0, jQuery has used a module wrapper with one strange addition - in CommonJS environments, if a global `window` with a `document` was not present, jQuery exported a factory accepting a `window` implementation and returning jQuery. This approach created a number of problems: 1. Properly typing jQuery would be a nightmare as the exported value depends on the environment. In practice, typing definitions ignored the factory case. 2. Since we now use named exports for the jQuery module version, it felt weird to have `jQuery` and `$` pointing to the factory instead of real jQuery. Instead, for jQuery 4.0 we leverage the just added `exports` field in `package.json` to expose completely separate factory entry points: one for the full build, one for the slim one. Exports definitions for `./factory` & `./factory-slim` are simpler than for `.` and `./slim` - this is because it's a new entry point, we only expose a named export and so there's no issue with just pointing Node.js to the CommonJS version (we cannot use the module version for `import` from Node.js to avoid double package hazard). The factory entry points are also not meant for the Web browser which always has a proper `window` - and they'd be unfit for an inclusion in a regular script tag anyway. Because of that, we also don't generate minified versions of these entry points. The factory files are not pushed to the CDN since they are mostly aimed at Node.js. Closes gh-5293
Diffstat (limited to 'src')
-rw-r--r--src/wrapper-esm.js27
-rw-r--r--src/wrapper-factory-esm.js33
-rw-r--r--src/wrapper-factory.js38
-rw-r--r--src/wrapper.js18
4 files changed, 84 insertions, 32 deletions
diff --git a/src/wrapper-esm.js b/src/wrapper-esm.js
index 00ff2995c..a4574a3fa 100644
--- a/src/wrapper-esm.js
+++ b/src/wrapper-esm.js
@@ -10,22 +10,12 @@
*/
// For ECMAScript module environments where a proper `window`
// is present, execute the factory and get jQuery.
-// For environments that do not have a `window` with a `document`
-// (such as Node.js), expose a factory as module.exports.
-// This accentuates the need for the creation of a real `window`.
-// e.g. var jQuery = require("jquery")(window);
-// See ticket trac-14549 for more info.
-var jQueryOrJQueryFactory = typeof window !== "undefined" && window.document ?
- jQueryFactory( window, true ) :
- function( w ) {
- if ( !w.document ) {
- throw new Error( "jQuery requires a window with a document" );
- }
- return jQueryFactory( w );
- };
-
function jQueryFactory( window, noGlobal ) {
+if ( typeof window === "undefined" || !window.document ) {
+ throw new Error( "jQuery requires a window with a document" );
+}
+
// @CODE
// build.js inserts compiled jQuery here
@@ -33,9 +23,8 @@ return jQuery;
}
-export {
- jQueryOrJQueryFactory as jQuery,
- jQueryOrJQueryFactory as $
-};
+var jQuery = jQueryFactory( window, true );
+
+export { jQuery, jQuery as $ };
-export default jQueryOrJQueryFactory;
+export default jQuery;
diff --git a/src/wrapper-factory-esm.js b/src/wrapper-factory-esm.js
new file mode 100644
index 000000000..9127d41e6
--- /dev/null
+++ b/src/wrapper-factory-esm.js
@@ -0,0 +1,33 @@
+/*!
+ * jQuery JavaScript Library v@VERSION
+ * https://jquery.com/
+ *
+ * Copyright OpenJS Foundation and other contributors
+ * Released under the MIT license
+ * https://jquery.org/license
+ *
+ * Date: @DATE
+ */
+// Expose a factory as `jQueryFactory`. Aimed at environments without
+// a real `window` where an emulated window needs to be constructed. Example:
+//
+// import { jQueryFactory } from "jquery/factory";
+// const jQuery = jQueryFactory( window );
+//
+// See ticket trac-14549 for more info.
+function jQueryFactoryWrapper( window, noGlobal ) {
+
+if ( !window.document ) {
+ throw new Error( "jQuery requires a window with a document" );
+}
+
+// @CODE
+// build.js inserts compiled jQuery here
+
+return jQuery;
+
+}
+
+export function jQueryFactory( window ) {
+ return jQueryFactoryWrapper( window, true );
+}
diff --git a/src/wrapper-factory.js b/src/wrapper-factory.js
new file mode 100644
index 000000000..212ff33bf
--- /dev/null
+++ b/src/wrapper-factory.js
@@ -0,0 +1,38 @@
+/*!
+ * jQuery JavaScript Library v@VERSION
+ * https://jquery.com/
+ *
+ * Copyright OpenJS Foundation and other contributors
+ * Released under the MIT license
+ * https://jquery.org/license
+ *
+ * Date: @DATE
+ */
+// Expose a factory as `jQueryFactory`. Aimed at environments without
+// a real `window` where an emulated window needs to be constructed. Example:
+//
+// const jQuery = require( "jquery/factory" )( window );
+//
+// See ticket trac-14549 for more info.
+function jQueryFactoryWrapper( window, noGlobal ) {
+
+"use strict";
+
+if ( !window.document ) {
+ throw new Error( "jQuery requires a window with a document" );
+}
+
+// @CODE
+// build.js inserts compiled jQuery here
+
+return jQuery;
+
+}
+
+function jQueryFactory( window ) {
+ "use strict";
+
+ return jQueryFactoryWrapper( window, true );
+}
+
+module.exports = { jQueryFactory: jQueryFactory };
diff --git a/src/wrapper.js b/src/wrapper.js
index fa8240e1e..ce7637c63 100644
--- a/src/wrapper.js
+++ b/src/wrapper.js
@@ -16,19 +16,7 @@
// For CommonJS and CommonJS-like environments where a proper `window`
// is present, execute the factory and get jQuery.
- // For environments that do not have a `window` with a `document`
- // (such as Node.js), expose a factory as module.exports.
- // This accentuates the need for the creation of a real `window`.
- // e.g. var jQuery = require("jquery")(window);
- // See ticket trac-14549 for more info.
- module.exports = global.document ?
- factory( global, true ) :
- function( w ) {
- if ( !w.document ) {
- throw new Error( "jQuery requires a window with a document" );
- }
- return factory( w );
- };
+ module.exports = factory( global, true );
} else {
factory( global );
}
@@ -38,6 +26,10 @@
"use strict";
+if ( !window.document ) {
+ throw new Error( "jQuery requires a window with a document" );
+}
+
// @CODE
// build.js inserts compiled jQuery here