8000 Core: Fix the exports setup to make bundlers work with ESM & CommonJS by mgol · Pull Request #5429 · jquery/jquery · GitHub
[go: up one dir, main page]

Skip to content

Core: Fix the exports setup to make bundlers work with ESM & CommonJS #5429

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading
0