8000 Core: Fix the exports setup to make bundlers work with ESM & CommonJS · jquery/jquery@d3a28fa · GitHub
[go: up one dir, main page]

Skip to content

Commit d3a28fa

Browse files
committed
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. Fixes gh-5416
1 parent fcb111c commit d3a28fa

File tree

6 files changed

+30
-12
lines changed

6 files changed

+30
-12
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ npm-debug.log*
1616
# the ESLint config & package.json files
1717
/dist/*
1818
!/dist/package.json
19+
!/dist/jquery.bundler-require-wrapper.js
20+
!/dist/jquery.bundler-require-wrapper.slim.js
1921

2022
# Ignore everything in the `dist-module` folder except for the ESLint config,
2123
# package.json & Node module wrapper files

build/release/dist.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ module.exports = function( Release, files, complete ) {
1717
"LICENSE.txt",
1818
"AUTHORS.txt",
1919
"dist/package.json",
20+
"dist/jquery.bundler-require-wrapper.js",
21+
"dist/jquery.bundler-require-wrapper.slim.js",
2022
"dist-module/package.json",
2123
"dist-module/jquery.node-module-wrapper.js",
2224
"dist-module/jquery.node-module-wrapper.slim.js"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"use strict";
2+
3+
// Bundlers are able to synchronously require an ESM module from a CommonJS one.
4+
const { jQuery } = require( "../dist-module/jquery.module.js" );
5+
module.exports = jQuery;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"use strict";
2+
3+
// Bundlers are able to synchronously require an ESM module from a CommonJS one.
4+
const { jQuery } = require( "../dist-module/jquery.slim.module.js" );
5+
module.exports = jQuery;

eslint.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,8 @@ export default [
303303

304304
{
305305
files: [
306+
"dist/jquery.bundler-require-wrapper.js",
307+
"dist/jquery.bundler-require-wrapper.slim.js",
306308
"dist-module/jquery.node-module-wrapper.js",
307309
"dist-module/jquery.node-module-wrapper.slim.js"
308310
],

package.json

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,27 @@
77
"exports": {
88
".": {
99
"node": {
10-
"module": "./dist-module/jquery.module.js",
1110
"import": "./dist-module/jquery.node-module-wrapper.js",
12-
"require": "./dist/jquery.js"
11+
"default": "./dist/jquery.js"
1312
},
14-
"production": "./dist-module/jquery.module.min.js",
15-
"development": "./dist-module/jquery.module.js",
16-
"script": "./dist/jquery.min.js",
17-
"default": "./dist-module/jquery.module.min.js"
13+
"module": {
14+
"import": "./dist-module/jquery.module.js",
15+
"default": "./dist/jquery.bundler-require-wrapper.js"
16+
},
17+
"import": "./dist-module/jquery.module.js",
18+
"default": "./dist/jquery.js"
1819
},
1920
"./slim": {
2021
"node": {
21-
"module": "./dist-module/jquery.slim.module.js",
2222
"import": "./dist-module/jquery.node-module-wrapper.slim.js",
23-
"require": "./dist/jquery.slim.js"
23+
"default": "./dist/jquery.slim.js"
24+
},
25+
"module": {
26+
"import": "./dist-module/jquery.slim.module.js",
27+
"default": "./dist/jquery.bundler-require-wrapper.slim.js"
2428
},
25-
"production": "./dist-module/jquery.slim.module.min.js",
26-
"development": "./dist-module/jquery.slim.module.js",
27-
"script": "./dist/jquery.slim.min.js",
28-
"default": "./dist-module/jquery.slim.module.min.js"
29+
"import": "./dist-module/jquery.slim.module.js",
30+
"default": "./dist/jquery.slim.js"
2931
},
3032
"./factory": "./dist-module/jquery.factory.module.js",
3133
"./factory-slim": "./dist-module/jquery.factory.slim.module.js",

0 commit comments

Comments
 (0)
0