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 /README.md | |
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 'README.md')
-rw-r--r-- | README.md | 14 |
1 files changed, 14 insertions, 0 deletions
@@ -143,6 +143,20 @@ By default, jQuery generates a regular script JavaScript file. You can also gene npm run build -- --filename=jquery.module.js --esm ``` +##### Factory mode + +By default, jQuery depends on a global `window`. For environments that don't have one, you can generate a factory build that exposes a function accepting `window` as a parameter that you can provide externally (see [`README` of the published package](build/fixtures/README.md) for usage instructions). You can generate such a factory using the `--factory` parameter: + +```bash +npm run build -- --filename=jquery.factory.js --factory +``` + +This option can be mixed with others like `--esm` or `--slim`: + +```bash +npm run build -- --filename=jquery.factory.slim.module.js --factory --esm --slim --dir="/dist-module" +``` + #### Custom Build Examples Create a custom build using `npm run build`, listing the modules to be excluded. Excluding a top-level module also excludes its corresponding directory of modules. |