🌐 AI搜索 & 代理 主页
Skip to content

Conversation

@mgol
Copy link
Member

@mgol mgol commented Jul 10, 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 added Needs review Build Blocker Discuss in Meeting Reserved for Issues and PRs that anyone would like to discuss in the weekly meeting. labels Jul 10, 2023
@mgol mgol added this to the 4.0.0 milestone Jul 10, 2023
@mgol mgol self-assigned this Jul 10, 2023
@mgol
Copy link
Member Author

mgol commented Jul 10, 2023

Since the main builds no longer include the factory code in the wrapper, there's a small size reduction - bigger for ESM builds:

   raw     gz Compared to main @ f75daab09102a4dd5107deadb55d4a169f86254a      
   -36    -10 dist/jquery.min.js                                               
   -36    -10 dist/jquery.slim.min.js                                          
  -139    -30 dist-module/jquery.module.min.js                                 
  -139    -34 dist-module/jquery.slim.module.min.js                            

@mgol mgol force-pushed the exports-separate-factory branch from f7f27e2 to e9da84e Compare July 10, 2023 18:43
@mgol mgol force-pushed the exports-separate-factory branch 2 times, most recently from eb81726 to 3e6453c Compare September 18, 2023 22:57
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 3e6453c to 5911250 Compare September 18, 2023 23:14
@mgol mgol requested a review from timmywil September 19, 2023 14:46
@mgol
Copy link
Member Author

mgol commented Sep 19, 2023

@timmywil PR updated

@timmywil timmywil removed the Discuss in Meeting Reserved for Issues and PRs that anyone would like to discuss in the weekly meeting. label Sep 19, 2023
Copy link
Member

@timmywil timmywil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my comment on eslint config.

@mgol
Copy link
Member Author

mgol commented Sep 19, 2023

PR updated

@mgol mgol requested a review from timmywil September 19, 2023 16:25
@mgol mgol merged commit 46f6e3d into jquery:main Sep 19, 2023
@mgol mgol deleted the exports-separate-factory branch September 19, 2023 16:58
@mgol mgol removed the Needs review label Sep 19, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 24, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Development

Successfully merging this pull request may close these issues.

2 participants