jquery/build/fixtures
Michał Gołębiowski-Owczarek 8be4c0e4f8
Build: Add exports to package.json, export slim & esm builds
Summary of the changes:
* define the `exports` field in `package.json`; `jQuery` & `$` are also
  exported as named exports in ESM builds now
* declare `"type": "module"` globally except for the `build` folder
* add the `--esm` option to `grunt custom`, generating jQuery as an ECMAScript
  module into the `dist-module` folder
* expand `node_smoke_tests` to test the slim & ESM builds and their various
  combinations; also, test both jQuery loaded via a path to the file as well
  as from module specifiers that should be parsed via the `exports` feature
* add details about ESM usage to the release package README
* run `compare_size` on all built minified files; don't run it anymore on
  unminified files where they don't provide lots of value
* remove the remove_map_comment task; SWC doesn't insert the
`//# sourceMappingURL=` pragma by default so there's nothing to strip

Fixes gh-4592
Closes gh-5255
2023-07-10 19:14:08 +02:00
..
README.md Build: Add exports to package.json, export slim & esm builds 2023-07-10 19:14:08 +02:00

jQuery

jQuery is a fast, small, and feature-rich JavaScript library.

For information on how to get started and how to use jQuery, please see jQuery's documentation. For source files and issues, please visit the jQuery repo.

If upgrading, please see the blog post for @VERSION. This includes notable differences from the previous version and a more readable changelog.

Including jQuery

Below are some of the most common ways to include jQuery.

Browser

Script tag

<script src="https://code.jquery.com/jquery-@VERSION.min.js"></script>

or, to use the jQuery ECMAScript module:

<script type="module">
	import $ from "https://code.jquery.com/jquery-@VERSION.min.js";
</script>

Sometimes you dont need AJAX, or you prefer to use one of the many standalone libraries that focus on AJAX requests. And often it is simpler to use a combination of CSS, class manipulation or the Web Animations API. Similarly, many projects opt into relying on native browser promises instead of jQuery Deferreds. Along with the regular version of jQuery that includes the ajax, callbacks, deferred, effects & queue modules, weve released a “slim” version that excludes these modules. You can load it as a regular script:

<script src="https://code.jquery.com/jquery-@VERSION.slim.min.js"></script>

or as a module:

<script type="module">
	import $ from "https://code.jquery.com/jquery-@VERSION.slim.min.js";
</script>

Import maps

To avoid repeating long import paths that change on each jQuery release, you can use import maps - they are now supported in every modern browser. Put the following script tag before any <script type="module">:

<script type="importmap">
	{
		"imports": {
			"jquery": "https://code.jquery.com/jquery-@VERSION.min.js",
			"jquery/slim": "https://code.jquery.com/jquery-@VERSION.slim.min.js"
		}
	}
</script>

Now, the following will work to get the full version:

<script type="module">
	import $ from "jquery";
	// Use $ here
</script>

and the following to get the slim one:

<script type="module">
	import $ from "jquery/slim";
	// Use $ here
</script>

The advantage of these specific mappings is they match the ones embedded in the jQuery npm package, providing better interoperability between the environments.

You can also use jQuery from npm even in the browser setup. Read along for more details.

Using jQuery from npm

There are several ways to use jQuery from npm. One is to use a build tool like Webpack, Browserify or Babel. For more information on using these tools, please refer to the corresponding project's documentation.

Another way is to use jQuery directly in Node.js. See the Node.js pre-requisites section for more details on the Node.js-specific part of this.

To install the jQuery npm package, invoke:

npm install jquery

In the script, including jQuery will usually look like this:

import $ from "jquery";

If you need to use jQuery in a file that's not an ECMAScript module, you can use the CommonJS syntax:

const $ = require( "jquery" );

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.

Be aware that this is an advanced & low-level interface, and we don't consider it stable, even between minor or patch releases - this is especially the case for modules in subdirectories or src/. If you rely on it, verify your setup before updating jQuery.

All top-level modules, i.e. files directly in the src/ directory export jQuery. Importing multiple modules will all attach to the same jQuery instance.

Remember that some modules have other dependencies (e.g. the event module depends on the selector one) so in some cases you may get more than you expect.

Example usage:

import $ from "jquery/src/css.js"; // adds the `.css()` method
import "jquery/src/event.js"; // adds the `.on()` method; pulls "selector" as a dependency
$( ".toggle" ).on( "click", function() {
	$( this ).css( "color", "red" );
} );

AMD (Asynchronous Module Definition)

AMD is a module format built for the browser. For more information, we recommend require.js' documentation.

define( [ "jquery" ], function( $ ) {

} );

Node.js doesn't understand AMD natively so this method is mostly used in a browser setup.

Node.js pre-requisites

For jQuery to work in Node, a window with a document is required. Since no such window exists natively in Node, one can be mocked by tools such as jsdom. This can be useful for testing purposes.

jQuery checks for a window global with a document property and - if one is not present, as is the default in Node.js - it returns a factory accepting a window as a parameter instead.

To import jQuery using this factory, use the following:

import { JSDOM } from "jsdom";
const { window } = new JSDOM( "" );
import jQueryFactory from "jquery";
const $ = jQueryFactory( window );

or, if you use require:

const { JSDOM } = require( "jsdom" );
const { window } = new JSDOM( "" );
const $ = require( "jquery" )( window );

If the window global is present at the moment of the import or require of "jquery", it will resolve to a jQuery instance, as in the browser. You can set such a global manually to simulate the behavior; with import:

import { JSDOM } from "jsdom";
const { window } = new JSDOM( "" );
globalThis.window = window;
const { default: $ } = await import( "jquery" );

or with require:

const { JSDOM } = require( "jsdom" );
const { window } = new JSDOM( "" );
globalThis.window = window;
const $ = require( "jquery" );

Slim build in Node.js

To use the slim build of jQuery in Node.js, use "jquery/slim" instead of "jquery" in both require or import calls above.