diff options
author | Michał Gołębiowski-Owczarek <m.goleb@gmail.com> | 2023-09-19 18:58:24 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-19 18:58:24 +0200 |
commit | 46f6e3da796ee9d28c7c1428793b72d66bcbb0b7 (patch) | |
tree | b4c2810c9c39816db7d937b6a02803d55dab1728 /test | |
parent | b923047d29d37f2d5c96f8b33992f322bc7b7944 (diff) | |
download | jquery-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 'test')
15 files changed, 62 insertions, 60 deletions
diff --git a/test/node_smoke_tests/commonjs/document_passed.cjs b/test/node_smoke_tests/commonjs/document_passed.cjs deleted file mode 100644 index 4cf280109..000000000 --- a/test/node_smoke_tests/commonjs/document_passed.cjs +++ /dev/null @@ -1,16 +0,0 @@ -"use strict"; - -const { JSDOM } = require( "jsdom" ); - -const { ensureJQuery } = require( "./lib/ensure_jquery.cjs" ); -const { ensureGlobalNotCreated } = require( "./lib/ensure_global_not_created.cjs" ); -const { getJQueryModuleSpecifier } = require( "./lib/jquery-module-specifier.cjs" ); - -const { window } = new JSDOM( "" ); - -const jQueryModuleSpecifier = getJQueryModuleSpecifier(); -const jQueryFactory = require( jQueryModuleSpecifier ); -const jQuery = jQueryFactory( window ); - -ensureJQuery( jQuery ); -ensureGlobalNotCreated( module.exports ); diff --git a/test/node_smoke_tests/commonjs/document_missing.cjs b/test/node_smoke_tests/commonjs/factory/document_missing.cjs index cf31fe427..e079912d8 100644 --- a/test/node_smoke_tests/commonjs/document_missing.cjs +++ b/test/node_smoke_tests/commonjs/factory/document_missing.cjs @@ -2,11 +2,11 @@ const assert = require( "node:assert" ); -const { ensureGlobalNotCreated } = require( "./lib/ensure_global_not_created.cjs" ); -const { getJQueryModuleSpecifier } = require( "./lib/jquery-module-specifier.cjs" ); +const { ensureGlobalNotCreated } = require( "../lib/ensure_global_not_created.cjs" ); +const { getJQueryModuleSpecifier } = require( "../lib/jquery-module-specifier.cjs" ); const jQueryModuleSpecifier = getJQueryModuleSpecifier(); -const jQueryFactory = require( jQueryModuleSpecifier ); +const { jQueryFactory } = require( jQueryModuleSpecifier ); assert.throws( () => { jQueryFactory( {} ); diff --git a/test/node_smoke_tests/commonjs/factory/document_passed.cjs b/test/node_smoke_tests/commonjs/factory/document_passed.cjs new file mode 100644 index 000000000..d7770b00a --- /dev/null +++ b/test/node_smoke_tests/commonjs/factory/document_passed.cjs @@ -0,0 +1,16 @@ +"use strict"; + +const { JSDOM } = require( "jsdom" ); + +const { ensureJQuery } = require( "../lib/ensure_jquery.cjs" ); +const { ensureGlobalNotCreated } = require( "../lib/ensure_global_not_created.cjs" ); +const { getJQueryModuleSpecifier } = require( "../lib/jquery-module-specifier.cjs" ); + +const { window } = new JSDOM( "" ); + +const jQueryModuleSpecifier = getJQueryModuleSpecifier(); +const { jQueryFactory } = require( jQueryModuleSpecifier ); +const jQuery = jQueryFactory( window ); + +ensureJQuery( jQuery ); +ensureGlobalNotCreated( module.exports ); diff --git a/test/node_smoke_tests/commonjs/iterable_with_native_symbol.cjs b/test/node_smoke_tests/commonjs/factory/iterable_with_native_symbol.cjs index 8fce34e06..d973271b4 100644 --- a/test/node_smoke_tests/commonjs/iterable_with_native_symbol.cjs +++ b/test/node_smoke_tests/commonjs/factory/iterable_with_native_symbol.cjs @@ -7,8 +7,8 @@ if ( typeof Symbol === "undefined" ) { process.exit(); } -const { ensureIterability } = require( "./lib/ensure_iterability_es6.cjs" ); -const { getJQueryModuleSpecifier } = require( "./lib/jquery-module-specifier.cjs" ); +const { ensureIterability } = require( "../lib/ensure_iterability_es6.cjs" ); +const { getJQueryModuleSpecifier } = require( "../lib/jquery-module-specifier.cjs" ); const jQueryModuleSpecifier = getJQueryModuleSpecifier(); ensureIterability( jQueryModuleSpecifier ); diff --git a/test/node_smoke_tests/commonjs/lib/ensure_iterability_es6.cjs b/test/node_smoke_tests/commonjs/lib/ensure_iterability_es6.cjs index eb4914758..01e0d4b56 100644 --- a/test/node_smoke_tests/commonjs/lib/ensure_iterability_es6.cjs +++ b/test/node_smoke_tests/commonjs/lib/ensure_iterability_es6.cjs @@ -8,7 +8,7 @@ const { ensureJQuery } = require( "./ensure_jquery.cjs" ); const ensureIterability = ( jQueryModuleSpecifier ) => { const { window } = new JSDOM( "" ); - const jQueryFactory = require( jQueryModuleSpecifier ); + const { jQueryFactory } = require( jQueryModuleSpecifier ); const jQuery = jQueryFactory( window ); const elem = jQuery( "<div></div><span></span><a></a>" ); diff --git a/test/node_smoke_tests/commonjs/window_present_originally.cjs b/test/node_smoke_tests/commonjs/regular/window_present_originally.cjs index 908fcb940..644a15a77 100644 --- a/test/node_smoke_tests/commonjs/window_present_originally.cjs +++ b/test/node_smoke_tests/commonjs/regular/window_present_originally.cjs @@ -2,9 +2,9 @@ const { JSDOM } = require( "jsdom" ); -const { ensureJQuery } = require( "./lib/ensure_jquery.cjs" ); -const { ensureGlobalNotCreated } = require( "./lib/ensure_global_not_created.cjs" ); -const { getJQueryModuleSpecifier } = require( "./lib/jquery-module-specifier.cjs" ); +const { ensureJQuery } = require( "../lib/ensure_jquery.cjs" ); +const { ensureGlobalNotCreated } = require( "../lib/ensure_global_not_created.cjs" ); +const { getJQueryModuleSpecifier } = require( "../lib/jquery-module-specifier.cjs" ); const jQueryModuleSpecifier = getJQueryModuleSpecifier(); 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/iterable_with_native_symbol.js b/test/node_smoke_tests/module/factory/iterable_with_native_symbol.js index 3417e8ac0..cb19c26c2 100644 --- a/test/node_smoke_tests/module/iterable_with_native_symbol.js +++ b/test/node_smoke_tests/module/factory/iterable_with_native_symbol.js @@ -1,7 +1,7 @@ import process from "node:process"; -import { ensureIterability } from "./lib/ensure_iterability_es6.js"; -import { getJQueryModuleSpecifier } from "./lib/jquery-module-specifier.js"; +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..." ); 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( "<div></div><span></span><a></a>" ); diff --git a/test/node_smoke_tests/module/window_present_originally.js b/test/node_smoke_tests/module/regular/window_present_originally.js index ab60947e8..ac5a416c8 100644 --- a/test/node_smoke_tests/module/window_present_originally.js +++ b/test/node_smoke_tests/module/regular/window_present_originally.js @@ -1,8 +1,8 @@ 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"; +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(); diff --git a/test/promises_aplus_adapters/deferred.cjs b/test/promises_aplus_adapters/deferred.cjs index 5e3ffe2d8..10e824561 100644 --- a/test/promises_aplus_adapters/deferred.cjs +++ b/test/promises_aplus_adapters/deferred.cjs @@ -4,7 +4,8 @@ const { JSDOM } = require( "jsdom" ); const { window } = new JSDOM( "" ); -const jQuery = require( "../../" )( window ); +const { jQueryFactory } = require( "jquery/factory" ); +const jQuery = jQueryFactory( window ); module.exports.deferred = () => { const deferred = jQuery.Deferred(); diff --git a/test/promises_aplus_adapters/when.cjs b/test/promises_aplus_adapters/when.cjs index 3e945d475..827216803 100644 --- a/test/promises_aplus_adapters/when.cjs +++ b/test/promises_aplus_adapters/when.cjs @@ -4,7 +4,8 @@ const { JSDOM } = require( "jsdom" ); const { window } = new JSDOM( "" ); -const jQuery = require( "../../" )( window ); +const { jQueryFactory } = require( "jquery/factory" ); +const jQuery = jQueryFactory( window ); module.exports.deferred = () => { let adopted, promised; |