Skip to content

Conversation

@mgol
Copy link
Owner

@mgol mgol commented Jul 6, 2023

Summary

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.

Checklist

@mgol mgol force-pushed the exports branch 2 times, most recently from 638b3ff to 7a978e6 Compare July 10, 2023 16:37
mgol added 3 commits July 10, 2023 19:14
Summary of the changes:
* define the `exports` field in `package.json`; `jQuery` & `$` are also
  exported as named exports in ESM builds now
* declare `"type": "module"` globally except for the `build` folder
* add the `--esm` option to `grunt custom`, generating jQuery as an ECMAScript
  module into the `dist-module` folder
* expand `node_smoke_tests` to test the slim & ESM builds and their various
  combinations; also, test both jQuery loaded via a path to the file as well
  as from module specifiers that should be parsed via the `exports` feature
* add details about ESM usage to the release package README
* run `compare_size` on all built minified files; don't run it anymore on
  unminified files where they don't provide lots of value
* remove the remove_map_comment task; SWC doesn't insert the
`//# sourceMappingURL=` pragma by default so there's nothing to strip

Fixes jquerygh-4592
Closes jquerygh-5255
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.
@mgol mgol force-pushed the exports-separate-factory branch from 307092c to f7f27e2 Compare July 10, 2023 18:11
@mgol
Copy link
Owner Author

mgol commented Jul 10, 2023

Superseded by jquery#5293

@mgol mgol closed this Jul 10, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 23, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants