Core: Move the factory to separate exports
commit46f6e3da796ee9d28c7c1428793b72d66bcbb0b7
authorMichał Gołębiowski-Owczarek <m.goleb@gmail.com>
Tue, 19 Sep 2023 16:58:24 +0000 (19 18:58 +0200)
committerGitHub <noreply@github.com>
Tue, 19 Sep 2023 16:58:24 +0000 (19 18:58 +0200)
treeb4c2810c9c39816db7d937b6a02803d55dab1728
parentb923047d29d37f2d5c96f8b33992f322bc7b7944
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
27 files changed:
README.md
build/command.js
build/fixtures/README.md
build/tasks/build.js
build/tasks/dist.js
build/tasks/node_smoke_tests.js
eslint.config.js
package.json
src/wrapper-esm.js
src/wrapper-factory-esm.js [new file with mode: 0644]
src/wrapper-factory.js [new file with mode: 0644]
src/wrapper.js
test/node_smoke_tests/commonjs/document_passed.cjs [deleted file]
test/node_smoke_tests/commonjs/factory/document_missing.cjs [moved from test/node_smoke_tests/commonjs/document_missing.cjs with 52% similarity]
test/node_smoke_tests/commonjs/factory/document_passed.cjs [new file with mode: 0644]
test/node_smoke_tests/commonjs/factory/iterable_with_native_symbol.cjs [moved from test/node_smoke_tests/commonjs/iterable_with_native_symbol.cjs with 63% similarity]
test/node_smoke_tests/commonjs/lib/ensure_iterability_es6.cjs
test/node_smoke_tests/commonjs/regular/window_present_originally.cjs [moved from test/node_smoke_tests/commonjs/window_present_originally.cjs with 58% similarity]
test/node_smoke_tests/module/document_missing.js [deleted file]
test/node_smoke_tests/module/document_passed.js [deleted file]
test/node_smoke_tests/module/factory/document_missing.js [new file with mode: 0644]
test/node_smoke_tests/module/factory/document_passed.js [new file with mode: 0644]
test/node_smoke_tests/module/factory/iterable_with_native_symbol.js [moved from test/node_smoke_tests/module/iterable_with_native_symbol.js with 64% similarity]
test/node_smoke_tests/module/lib/ensure_iterability_es6.js
test/node_smoke_tests/module/regular/window_present_originally.js [moved from test/node_smoke_tests/module/window_present_originally.js with 59% similarity]
test/promises_aplus_adapters/deferred.cjs
test/promises_aplus_adapters/when.cjs