Skip to content

Commit

Permalink
Core: Fix the exports setup to make bundlers work with ESM & CommonJS
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mgol committed Mar 11, 2024
1 parent fedffe7 commit 60f11b5
Show file tree
Hide file tree
Showing 35 changed files with 1,173 additions and 48 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
.sizecache.json
yarn.lock
.eslintcache
tmp

npm-debug.log*

# Ignore everything in `dist` folder except for
# the ESLint config & package.json files
/dist/*
!/dist/package.json
!/dist/jquery.bundler-require-wrapper.js
!/dist/jquery.bundler-require-wrapper.slim.js

# Ignore everything in the `dist-module` folder except for the ESLint config,
# package.json & Node module wrapper files
Expand Down
2 changes: 2 additions & 0 deletions build/fixtures/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ If you need to use jQuery in a file that's not an ECMAScript module, you can use
const $ = require( "jquery" );
```

The CommonJS module _does not_ expose named `$` & `jQuery` exports.

#### Individual modules

jQuery is authored in ECMAScript modules; it's also possible to use them directly. They are contained in the `src/` folder; inspect the package contents to see what's there. Full file names are required, including the `.js` extension.
Expand Down
2 changes: 2 additions & 0 deletions build/release/dist.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ module.exports = function( Release, files, complete ) {
"LICENSE.txt",
"AUTHORS.txt",
"dist/package.json",
"dist/jquery.bundler-require-wrapper.js",
"dist/jquery.bundler-require-wrapper.slim.js",
"dist-module/package.json",
"dist-module/jquery.node-module-wrapper.js",
"dist-module/jquery.node-module-wrapper.slim.js"
Expand Down
30 changes: 26 additions & 4 deletions build/tasks/node_smoke_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const util = require( "node:util" );
const exec = util.promisify( require( "node:child_process" ).exec );
const verifyNodeVersion = require( "./lib/verifyNodeVersion" );

const allowedLibraryTypes = [ "regular", "factory" ];
const allowedSourceTypes = [ "commonjs", "module" ];
const allowedLibraryTypes = new Set( [ "regular", "factory" ] );
const allowedSourceTypes = new Set( [ "commonjs", "module", "dual" ] );

if ( !verifyNodeVersion() ) {
return;
Expand All @@ -19,8 +19,8 @@ if ( !verifyNodeVersion() ) {
// each other, e.g. so that they don't share the `require` cache.

async function runTests( { libraryType, sourceType, module } ) {
if ( !allowedLibraryTypes.includes( libraryType ) ||
!allowedSourceTypes.includes( sourceType ) ) {
if ( !allowedLibraryTypes.has( libraryType ) ||
!allowedSourceTypes.has( sourceType ) ) {
throw new Error( `Incorrect libraryType or sourceType value; passed: ${
libraryType
} ${ sourceType } "${ module }"` );
Expand Down Expand Up @@ -127,6 +127,28 @@ async function runDefaultTests() {
libraryType: "factory",
sourceType: "module",
module: "./dist-module/jquery.factory.slim.module.js"
} ),

runTests( {
libraryType: "regular",
sourceType: "dual",
module: "jquery"
} ),
runTests( {
libraryType: "regular",
sourceType: "dual",
module: "jquery/slim"
} ),

runTests( {
libraryType: "factory",
sourceType: "dual",
module: "jquery/factory"
} ),
runTests( {
libraryType: "factory",
sourceType: "dual",
module: "jquery/factory-slim"
} )
] );
}
Expand Down
5 changes: 3 additions & 2 deletions dist-module/jquery.node-module-wrapper.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Node.js is able to import from a CommonJS module in an ESM one.
import jQuery from "../dist/jquery.js";
const $ = jQuery;
export { jQuery, $ };

export { jQuery, jQuery as $ };
export default jQuery;
5 changes: 3 additions & 2 deletions dist-module/jquery.node-module-wrapper.slim.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Node.js is able to import from a CommonJS module in an ESM one.
import jQuery from "../dist/jquery.slim.js";
const $ = jQuery;
export { jQuery, $ };

export { jQuery, jQuery as $ };
export default jQuery;
5 changes: 5 additions & 0 deletions dist/jquery.bundler-require-wrapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"use strict";

// Bundlers are able to synchronously require an ESM module from a CommonJS one.
const { jQuery } = require( "../dist-module/jquery.module.js" );
module.exports = jQuery;
5 changes: 5 additions & 0 deletions dist/jquery.bundler-require-wrapper.slim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"use strict";

// Bundlers are able to synchronously require an ESM module from a CommonJS one.
const { jQuery } = require( "../dist-module/jquery.slim.module.js" );
module.exports = jQuery;
29 changes: 24 additions & 5 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default [
// See https://github.com/eslint/eslint/discussions/17412
ignores: [
"external",
"**/tmp",
"test/data/json_obj.js"
]
},
Expand All @@ -18,8 +19,8 @@ export default [
files: [
"eslint.config.js",
"Gruntfile.cjs",
"test/node_smoke_tests/commonjs/**",
"test/node_smoke_tests/module/**",
"test/bundler_smoke_tests/**/*",
"test/node_smoke_tests/**",
"test/promises_aplus_adapters/**",
"test/middleware-mockserver.cjs"
],
Expand Down Expand Up @@ -260,8 +261,8 @@ export default [

{
files: [
"test/node_smoke_tests/commonjs/**",
"test/node_smoke_tests/module/**",
"test/bundler_smoke_tests/**",
"test/node_smoke_tests/**",
"test/promises_aplus_adapters/**",
"test/middleware-mockserver.cjs"
],
Expand Down Expand Up @@ -317,7 +318,25 @@ export default [

languageOptions: {
globals: {
...globals.browser,
...globals.es2021,
define: false,
module: false,
Symbol: false
}
}
},

{
files: [
"dist/jquery.bundler-require-wrapper.js",
"dist/jquery.bundler-require-wrapper.slim.js",
"dist-module/jquery.node-module-wrapper.js",
"dist-module/jquery.node-module-wrapper.slim.js"
],

languageOptions: {
globals: {
...globals.node,
...globals.es2021,
define: false,
module: false,
Expand Down

0 comments on commit 60f11b5

Please sign in to comment.