jquery/build/release/dist.js
Michał Gołębiowski-Owczarek 60f11b58bf
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
2024-03-12 00:39:34 +01:00

191 lines
4.9 KiB
JavaScript

"use strict";
module.exports = function( Release, files, complete ) {
const fs = require( "node:fs/promises" );
const shell = require( "shelljs" );
const inquirer = require( "inquirer" );
const pkg = require( `${ Release.dir.repo }/package.json` );
const distRemote = Release.remote
// For local and github dists
.replace( /jquery(\.git|$)/, "jquery-dist$1" );
// These files are included with the distribution
const extras = [
"src",
"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"
];
/**
* Clone the distribution repo
*/
function clone() {
Release.chdir( Release.dir.base );
Release.dir.dist = `${ Release.dir.base }/dist`;
console.log( "Using distribution repo: ", distRemote );
Release.exec( `git clone ${ distRemote } ${ Release.dir.dist }`,
"Error cloning repo." );
// Distribution always works on main
Release.chdir( Release.dir.dist );
Release.exec( "git checkout main", "Error checking out branch." );
console.log();
}
/**
* Generate bower file for jquery-dist
*/
function generateBower() {
return JSON.stringify( {
name: pkg.name,
main: pkg.main,
license: "MIT",
ignore: [
"package.json"
],
keywords: pkg.keywords
}, null, 2 );
}
/**
* Replace the version in the README
* @param {string} readme
* @param {string} blogPostLink
*/
function editReadme( readme, blogPostLink ) {
return readme
.replace( /@VERSION/g, Release.newVersion )
.replace( /@BLOG_POST_LINK/g, blogPostLink );
}
/**
* Copy necessary files over to the dist repo
*/
async function copy() {
const readme = await fs.readFile(
`${ Release.dir.repo }/build/fixtures/README.md`, "utf8" );
const rmIgnore = [ ...files, "node_modules" ]
.map( file => `${ Release.dir.dist }/${ file }` );
shell.config.globOptions = {
ignore: rmIgnore
};
const { blogPostLink } = await inquirer.prompt( [ {
type: "input",
name: "blogPostLink",
message: "Enter URL of the blog post announcing the jQuery release...\n"
} ] );
// Remove extraneous files before copy
shell.rm( "-rf", `${ Release.dir.dist }/**/*` );
// Copy dist files
shell.mkdir( "-p", `${ Release.dir.dist }/dist` );
shell.mkdir( "-p", `${ Release.dir.dist }/dist-module` );
files.forEach( function( file ) {
shell.cp(
"-f",
`${ Release.dir.repo }/${ file }`,
`${ Release.dir.dist }/${ file }`
);
} );
// Copy other files
extras.forEach( function( file ) {
shell.cp(
"-rf",
`${ Release.dir.repo }/${ file }`,
`${ Release.dir.dist }/${ file }`
);
} );
// Remove the wrapper & the ESLint config from the dist repo
shell.rm( "-f", `${ Release.dir.dist }/src/wrapper.js` );
shell.rm( "-f", `${ Release.dir.dist }/src/.eslintrc.json` );
// Write package.json
// Remove scripts and other superfluous properties,
// especially the prepare script, which fails on the dist repo
const packageJson = Object.assign( {}, pkg );
delete packageJson.scripts;
delete packageJson.devDependencies;
delete packageJson.dependencies;
delete packageJson.commitplease;
packageJson.version = Release.newVersion;
await fs.writeFile(
`${ Release.dir.dist }/package.json`,
JSON.stringify( packageJson, null, 2 )
);
// Write generated bower file
await fs.writeFile( `${ Release.dir.dist }/bower.json`, generateBower() );
await fs.writeFile( `${ Release.dir.dist }/README.md`,
editReadme( readme, blogPostLink ) );
console.log( "Files ready to add." );
}
/**
* Add, commit, and tag the dist files
*/
function commit() {
console.log( "Adding files to dist..." );
Release.exec( "git add -A", "Error adding files." );
Release.exec(
`git commit -m "Release ${ Release.newVersion }"`,
"Error committing files."
);
console.log();
console.log( "Tagging release on dist..." );
Release.exec( `git tag ${ Release.newVersion }`,
`Error tagging ${ Release.newVersion } on dist repo.` );
Release.tagTime = Release.exec( "git log -1 --format=\"%ad\"",
"Error getting tag timestamp." ).trim();
}
/**
* Push files to dist repo
*/
function push() {
Release.chdir( Release.dir.dist );
console.log( "Pushing release to dist repo..." );
Release.exec(
`git push ${
Release.isTest ? " --dry-run" : ""
} ${ distRemote } main --tags`,
"Error pushing main and tags to git repo."
);
// Set repo for npm publish
Release.dir.origRepo = Release.dir.repo;
Release.dir.repo = Release.dir.dist;
}
Release.walk( [
Release._section( "Copy files to distribution repo" ),
clone,
copy,
Release.confirmReview,
Release._section( "Add, commit, and tag files in distribution repo" ),
commit,
Release.confirmReview,
Release._section( "Pushing files to distribution repo" ),
push
], complete );
};