diff options
author | Michał Gołębiowski-Owczarek <m.goleb@gmail.com> | 2024-03-12 00:39:34 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-12 00:39:34 +0100 |
commit | 60f11b58bfeece6b6d0189d7d19b61a4e1e61139 (patch) | |
tree | cdd1cd1b736bf646373f18af7111bf460cabf71a /test/node_smoke_tests/module | |
parent | fedffe7448b9e2328b43641158335be18eff5f69 (diff) | |
download | jquery-60f11b58bfeece6b6d0189d7d19b61a4e1e61139.tar.gz jquery-60f11b58bfeece6b6d0189d7d19b61a4e1e61139.zip |
Core: Fix the exports setup to make bundlers work with ESM & CommonJS
We cannot pass a single file via the `module` condition as then
`require( "jquery" )` will not return jQuery but instead the module object
with `default`, `$` & `jQuery` as keys. Instead:
1. For Node.js, detected via the `node` condition:
1. Expose a regular CommonJS version to `require`
2. Expose a tiny wrapper over CommonJS to `import`
2. For bundlers, detected via the `module` condition:
1. Expose a regular ESM version to `import`
2. Expose a tiny wrapper over ESM to `require`
3. If neither Node.js nor bundlers are detected (no `node` or `module`
conditions`):
1. Expose a regular CommonJS version to `require`
2. Expose a regular ESM version to `import`
The reasons for such definitions are as follows:
1. In Node.js, one can synchronously import from a CommonJS file inside of
an ESM one but not vice-versa. To use an ESM file in a CommonJS one,
a dynamic import is required and that forces asynchronicity.
2. In some bundlers CommonJS is not necessarily enabled - e.g. in Rollup without
the CommonJS plugin. Therefore, the ESM version needs to be pure ESM.
However, bundlers allow synchronously calling `require` on an ESM file. This
is possible since bundlers merge the files before they are passed to
the browser to execute and the final bundles no longer contain async import
code.
3. Bare ESM & CommonJS versions are provided to non-Node non-bundler
environments where we cannot assume interoperability between ESM & CommonJS
is supported.
4. Bare versions cannot be supplied to Node or bundlers as projects using both
ESM & CommonJS to fetch jQuery would result in duplicate jQuery instances,
leading to increased JS size and disjoint data storage.
In addition to the above changes, the `script` condition has been dropped. Only
Webpack documents this condition and it's not clear when exactly it's triggered.
Adding support for a new condition can be added later without a breaking change;
removing is not so easy.
The `production` & `development` conditions have been removed as well. They were
not really applied correctly; we'd need to provide both of them to each current
leaf which would double the size of the definition for the `.` & `./slim` entry
points. In jQuery, the only difference between development & production builds
is minification; there are no logic changes so we can pass unminified versions
to all the tooling, expecting minification down the line.
As for the factory entry points:
1. Node.js always gets the CommonJS version
2. Bundlers always get the ESM version
3. Other tools take the ESM version when using `import` and the CommonJS when
using `require`.
The complexity is lower than for the `.` & `./slim` entry points because there's
no default export to handle so Node/bundler wrapper files are not necessary.
Other changes:
* Tests: Change "node:assert" to "node:assert/strict"; the former is deprecated
* Docs: Mention that the CommonJS module doesn't expose named exports
* Tests: Run Node & bundler tests for all the above cases
Fixes gh-5416
Closes gh-5429
Diffstat (limited to 'test/node_smoke_tests/module')
5 files changed, 5 insertions, 5 deletions
diff --git a/test/node_smoke_tests/module/factory/document_missing.js b/test/node_smoke_tests/module/factory/document_missing.js index ccb3383ad..b902d2913 100644 --- a/test/node_smoke_tests/module/factory/document_missing.js +++ b/test/node_smoke_tests/module/factory/document_missing.js @@ -1,4 +1,4 @@ -import assert from "node:assert"; +import assert from "node:assert/strict"; import { ensureGlobalNotCreated } from "../lib/ensure_global_not_created.js"; import { getJQueryModuleSpecifier } from "../lib/jquery-module-specifier.js"; diff --git a/test/node_smoke_tests/module/lib/ensure_global_not_created.js b/test/node_smoke_tests/module/lib/ensure_global_not_created.js index d7648bd68..caccd82e3 100644 --- a/test/node_smoke_tests/module/lib/ensure_global_not_created.js +++ b/test/node_smoke_tests/module/lib/ensure_global_not_created.js @@ -1,4 +1,4 @@ -import assert from "node:assert"; +import assert from "node:assert/strict"; // Ensure the jQuery property on global/window/module "this"/etc. was not // created in a CommonJS environment. 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 3f88913d0..c12e58c59 100644 --- a/test/node_smoke_tests/module/lib/ensure_iterability_es6.js +++ b/test/node_smoke_tests/module/lib/ensure_iterability_es6.js @@ -1,4 +1,4 @@ -import assert from "node:assert"; +import assert from "node:assert/strict"; const { JSDOM } = await import( "jsdom" ); const { ensureJQuery } = await import( "./ensure_jquery.js" ); diff --git a/test/node_smoke_tests/module/lib/ensure_jquery.js b/test/node_smoke_tests/module/lib/ensure_jquery.js index d07624ebd..13749306e 100644 --- a/test/node_smoke_tests/module/lib/ensure_jquery.js +++ b/test/node_smoke_tests/module/lib/ensure_jquery.js @@ -1,4 +1,4 @@ -import assert from "node:assert"; +import assert from "node:assert/strict"; // Check if the object we got is the jQuery object by invoking a basic API. export const ensureJQuery = ( jQuery ) => { diff --git a/test/node_smoke_tests/module/regular/window_present_originally.js b/test/node_smoke_tests/module/regular/window_present_originally.js index ac5a416c8..fee3ca5ae 100644 --- a/test/node_smoke_tests/module/regular/window_present_originally.js +++ b/test/node_smoke_tests/module/regular/window_present_originally.js @@ -11,7 +11,7 @@ const { window } = new JSDOM( "" ); // Set the window global. globalThis.window = window; -const { default: jQuery } = await import( jQueryModuleSpecifier ); +const { jQuery } = await import( jQueryModuleSpecifier ); ensureJQuery( jQuery ); ensureGlobalNotCreated( window ); |