From 46f6e3da796ee9d28c7c1428793b72d66bcbb0b7 Mon Sep 17 00:00:00 2001 From: Michał Gołębiowski-Owczarek Date: Tue, 19 Sep 2023 18:58:24 +0200 Subject: 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 --- test/node_smoke_tests/module/document_missing.js | 13 ------------- test/node_smoke_tests/module/document_passed.js | 14 -------------- .../node_smoke_tests/module/factory/document_missing.js | 13 +++++++++++++ test/node_smoke_tests/module/factory/document_passed.js | 14 ++++++++++++++ .../module/factory/iterable_with_native_symbol.js | 12 ++++++++++++ .../module/iterable_with_native_symbol.js | 12 ------------ .../module/lib/ensure_iterability_es6.js | 2 +- .../module/regular/window_present_originally.js | 17 +++++++++++++++++ .../module/window_present_originally.js | 17 ----------------- 9 files changed, 57 insertions(+), 57 deletions(-) delete mode 100644 test/node_smoke_tests/module/document_missing.js delete mode 100644 test/node_smoke_tests/module/document_passed.js create mode 100644 test/node_smoke_tests/module/factory/document_missing.js create mode 100644 test/node_smoke_tests/module/factory/document_passed.js create mode 100644 test/node_smoke_tests/module/factory/iterable_with_native_symbol.js delete mode 100644 test/node_smoke_tests/module/iterable_with_native_symbol.js create mode 100644 test/node_smoke_tests/module/regular/window_present_originally.js delete mode 100644 test/node_smoke_tests/module/window_present_originally.js (limited to 'test/node_smoke_tests/module') diff --git a/test/node_smoke_tests/module/document_missing.js b/test/node_smoke_tests/module/document_missing.js deleted file mode 100644 index 9394af796..000000000 --- a/test/node_smoke_tests/module/document_missing.js +++ /dev/null @@ -1,13 +0,0 @@ -import assert from "node:assert"; - -import { ensureGlobalNotCreated } from "./lib/ensure_global_not_created.js"; -import { getJQueryModuleSpecifier } from "./lib/jquery-module-specifier.js"; - -const jQueryModuleSpecifier = getJQueryModuleSpecifier(); -const { default: jQueryFactory } = await import( jQueryModuleSpecifier ); - -assert.throws( () => { - jQueryFactory( {} ); -}, /jQuery requires a window with a document/ ); - -ensureGlobalNotCreated(); diff --git a/test/node_smoke_tests/module/document_passed.js b/test/node_smoke_tests/module/document_passed.js deleted file mode 100644 index 7af5a7972..000000000 --- a/test/node_smoke_tests/module/document_passed.js +++ /dev/null @@ -1,14 +0,0 @@ -import { JSDOM } from "jsdom"; - -import { ensureJQuery } from "./lib/ensure_jquery.js"; -import { ensureGlobalNotCreated } from "./lib/ensure_global_not_created.js"; -import { getJQueryModuleSpecifier } from "./lib/jquery-module-specifier.js"; - -const { window } = new JSDOM( "" ); - -const jQueryModuleSpecifier = getJQueryModuleSpecifier(); -const { default: jQueryFactory } = await import( jQueryModuleSpecifier ); -const jQuery = jQueryFactory( window ); - -ensureJQuery( jQuery ); -ensureGlobalNotCreated(); diff --git a/test/node_smoke_tests/module/factory/document_missing.js b/test/node_smoke_tests/module/factory/document_missing.js new file mode 100644 index 000000000..ccb3383ad --- /dev/null +++ b/test/node_smoke_tests/module/factory/document_missing.js @@ -0,0 +1,13 @@ +import assert from "node:assert"; + +import { ensureGlobalNotCreated } from "../lib/ensure_global_not_created.js"; +import { getJQueryModuleSpecifier } from "../lib/jquery-module-specifier.js"; + +const jQueryModuleSpecifier = getJQueryModuleSpecifier(); +const { jQueryFactory } = await import( jQueryModuleSpecifier ); + +assert.throws( () => { + jQueryFactory( {} ); +}, /jQuery requires a window with a document/ ); + +ensureGlobalNotCreated(); diff --git a/test/node_smoke_tests/module/factory/document_passed.js b/test/node_smoke_tests/module/factory/document_passed.js new file mode 100644 index 000000000..800b34a4e --- /dev/null +++ b/test/node_smoke_tests/module/factory/document_passed.js @@ -0,0 +1,14 @@ +import { JSDOM } from "jsdom"; + +import { ensureJQuery } from "../lib/ensure_jquery.js"; +import { ensureGlobalNotCreated } from "../lib/ensure_global_not_created.js"; +import { getJQueryModuleSpecifier } from "../lib/jquery-module-specifier.js"; + +const { window } = new JSDOM( "" ); + +const jQueryModuleSpecifier = getJQueryModuleSpecifier(); +const { jQueryFactory } = await import( jQueryModuleSpecifier ); +const jQuery = jQueryFactory( window ); + +ensureJQuery( jQuery ); +ensureGlobalNotCreated(); diff --git a/test/node_smoke_tests/module/factory/iterable_with_native_symbol.js b/test/node_smoke_tests/module/factory/iterable_with_native_symbol.js new file mode 100644 index 000000000..cb19c26c2 --- /dev/null +++ b/test/node_smoke_tests/module/factory/iterable_with_native_symbol.js @@ -0,0 +1,12 @@ +import process from "node:process"; + +import { ensureIterability } from "../lib/ensure_iterability_es6.js"; +import { getJQueryModuleSpecifier } from "../lib/jquery-module-specifier.js"; + +if ( typeof Symbol === "undefined" ) { + console.log( "Symbols not supported, skipping the test..." ); + process.exit(); +} + +const jQueryModuleSpecifier = getJQueryModuleSpecifier(); +await ensureIterability( jQueryModuleSpecifier ); diff --git a/test/node_smoke_tests/module/iterable_with_native_symbol.js b/test/node_smoke_tests/module/iterable_with_native_symbol.js deleted file mode 100644 index 3417e8ac0..000000000 --- a/test/node_smoke_tests/module/iterable_with_native_symbol.js +++ /dev/null @@ -1,12 +0,0 @@ -import process from "node:process"; - -import { ensureIterability } from "./lib/ensure_iterability_es6.js"; -import { getJQueryModuleSpecifier } from "./lib/jquery-module-specifier.js"; - -if ( typeof Symbol === "undefined" ) { - console.log( "Symbols not supported, skipping the test..." ); - process.exit(); -} - -const jQueryModuleSpecifier = getJQueryModuleSpecifier(); -await ensureIterability( jQueryModuleSpecifier ); diff --git a/test/node_smoke_tests/module/lib/ensure_iterability_es6.js b/test/node_smoke_tests/module/lib/ensure_iterability_es6.js index 8893267a0..3f88913d0 100644 --- a/test/node_smoke_tests/module/lib/ensure_iterability_es6.js +++ b/test/node_smoke_tests/module/lib/ensure_iterability_es6.js @@ -6,7 +6,7 @@ const { ensureJQuery } = await import( "./ensure_jquery.js" ); export const ensureIterability = async( jQueryModuleSpecifier ) => { const { window } = new JSDOM( "" ); - const { default: jQueryFactory } = await import( jQueryModuleSpecifier ); + const { jQueryFactory } = await import( jQueryModuleSpecifier ); const jQuery = jQueryFactory( window ); const elem = jQuery( "
" ); diff --git a/test/node_smoke_tests/module/regular/window_present_originally.js b/test/node_smoke_tests/module/regular/window_present_originally.js new file mode 100644 index 000000000..ac5a416c8 --- /dev/null +++ b/test/node_smoke_tests/module/regular/window_present_originally.js @@ -0,0 +1,17 @@ +import { JSDOM } from "jsdom"; + +import { ensureJQuery } from "../lib/ensure_jquery.js"; +import { ensureGlobalNotCreated } from "../lib/ensure_global_not_created.js"; +import { getJQueryModuleSpecifier } from "../lib/jquery-module-specifier.js"; + +const jQueryModuleSpecifier = getJQueryModuleSpecifier(); + +const { window } = new JSDOM( "" ); + +// Set the window global. +globalThis.window = window; + +const { default: jQuery } = await import( jQueryModuleSpecifier ); + +ensureJQuery( jQuery ); +ensureGlobalNotCreated( window ); diff --git a/test/node_smoke_tests/module/window_present_originally.js b/test/node_smoke_tests/module/window_present_originally.js deleted file mode 100644 index ab60947e8..000000000 --- a/test/node_smoke_tests/module/window_present_originally.js +++ /dev/null @@ -1,17 +0,0 @@ -import { JSDOM } from "jsdom"; - -import { ensureJQuery } from "./lib/ensure_jquery.js"; -import { ensureGlobalNotCreated } from "./lib/ensure_global_not_created.js"; -import { getJQueryModuleSpecifier } from "./lib/jquery-module-specifier.js"; - -const jQueryModuleSpecifier = getJQueryModuleSpecifier(); - -const { window } = new JSDOM( "" ); - -// Set the window global. -globalThis.window = window; - -const { default: jQuery } = await import( jQueryModuleSpecifier ); - -ensureJQuery( jQuery ); -ensureGlobalNotCreated( window ); -- cgit v1.2.3