Skip to content

Commit 8be4c0e

Browse files
authored
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
1 parent 65b8503 commit 8be4c0e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+887
-308
lines changed

.eslintignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ node_modules
44
dist/**
55
!dist/jquery.js
66
!dist/jquery.min.js
7+
!dist/jquery.slim.js
8+
!dist/jquery.slim.min.js
9+
dist-module/**
10+
!dist-module/jquery.module.js
11+
!dist-module/jquery.module.min.js
12+
!dist-module/jquery.slim.module.js
13+
!dist-module/jquery.slim.module.min.js
714
test/data/jquery-1.9.1.js
815
test/data/badcall.js
916
test/data/badjson.js

.gitignore

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,18 @@ package-lock.json
1212

1313
npm-debug.log*
1414

15-
# Ignore everything in dist folder except for eslint config
15+
# Ignore everything in `dist` folder except for the ESLint config
1616
/dist/*
1717
!/dist/.eslintrc.json
18+
!/dist/package.json
19+
20+
# Ignore everything in the `dist-module` folder except for the ESLint config,
21+
# package.json & Node module wrapper files
22+
/dist-module/*
23+
!/dist-module/.eslintrc.json
24+
!/dist-module/package.json
25+
!/dist-module/jquery.node-module-wrapper.js
26+
!/dist-module/jquery.node-module-wrapper.slim.js
1827

1928
/external
2029
/node_modules

.npmignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
/external
1010
/speed
1111
/test
12-
/Gruntfile.js
12+
/Gruntfile.cjs

Gruntfile.js renamed to Gruntfile.cjs

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ module.exports = function( grunt ) {
1313
}
1414

1515
const fs = require( "fs" );
16+
const { spawn } = require( "child_process" );
1617
const gzip = require( "gzip-js" );
17-
const nodeV14OrNewer = !/^v1[0-3]\./.test( process.version );
18+
const nodeV16OrNewer = !/^v1[0-5]\./.test( process.version );
1819
const nodeV17OrNewer = !/^v1[0-6]\./.test( process.version );
1920
const customBrowsers = process.env.BROWSERS && process.env.BROWSERS.split( "," );
2021

21-
// Support: Node.js <14
22-
// Skip running tasks that dropped support for Node.js 10 or 12
23-
// in this Node version.
22+
// Support: Node.js <16
23+
// Skip running tasks that dropped support for old Node.js in these Node versions.
2424
function runIfNewNode( task ) {
25-
return nodeV14OrNewer ? task : "print_old_node_message:" + task;
25+
return nodeV16OrNewer ? task : "print_old_node_message:" + task;
2626
}
2727

28-
if ( nodeV14OrNewer ) {
28+
if ( nodeV16OrNewer ) {
2929
const playwright = require( "playwright-webkit" );
3030
process.env.WEBKIT_HEADLESS_BIN = playwright.webkit.executablePath();
3131
}
@@ -34,11 +34,27 @@ module.exports = function( grunt ) {
3434
grunt.option( "filename", "jquery.js" );
3535
}
3636

37+
grunt.option( "dist-folder", grunt.option( "esm" ) ? "dist-module" : "dist" );
38+
39+
const builtJsFiles = [
40+
"dist/jquery.js",
41+
"dist/jquery.min.js",
42+
"dist/jquery.slim.js",
43+
"dist/jquery.slim.min.js",
44+
"dist-module/jquery.module.js",
45+
"dist-module/jquery.module.min.js",
46+
"dist-module/jquery.slim.module.js",
47+
"dist-module/jquery.slim.module.min.js"
48+
];
49+
50+
const builtJsMinFiles = builtJsFiles
51+
.filter( filepath => filepath.endsWith( ".min.js" ) );
52+
3753
grunt.initConfig( {
3854
pkg: grunt.file.readJSON( "package.json" ),
3955
dst: readOptionalJSON( "dist/.destination.json" ),
4056
compare_size: {
41-
files: [ "dist/jquery.js", "dist/jquery.min.js" ],
57+
files: builtJsMinFiles,
4258
options: {
4359
compress: {
4460
gz: function( contents ) {
@@ -122,7 +138,7 @@ module.exports = function( grunt ) {
122138
// We have to explicitly declare "src" property otherwise "newer"
123139
// task wouldn't work properly :/
124140
dist: {
125-
src: [ "dist/jquery.js", "dist/jquery.min.js" ]
141+
src: builtJsFiles
126142
},
127143
dev: {
128144
src: [
@@ -142,9 +158,10 @@ module.exports = function( grunt ) {
142158
`!${ filePath }`
143159
),
144160

145-
// Explicitly ignore `dist/` as it could be unignored by
146-
// the above `.eslintignore` parsing.
147-
"!dist/**/*.js"
161+
// Explicitly ignore `dist/` & `dist-module/` as it could be unignored
162+
// by the above `.eslintignore` parsing.
163+
"!dist/**/*.js",
164+
"!dist-module/**/*.js"
148165
]
149166
}
150167
},
@@ -195,7 +212,7 @@ module.exports = function( grunt ) {
195212
{
196213
"middleware:mockserver": [
197214
"factory",
198-
require( "./test/middleware-mockserver.js" )
215+
require( "./test/middleware-mockserver.cjs" )
199216
]
200217
}
201218
],
@@ -319,12 +336,15 @@ module.exports = function( grunt ) {
319336
minify: {
320337
all: {
321338
files: {
322-
"dist/<%= grunt.option('filename').replace('.js', '.min.js') %>":
323-
"dist/<%= grunt.option('filename') %>"
339+
[ "<%= grunt.option('dist-folder') %>/" +
340+
"<%= grunt.option('filename').replace(/\\.js$/, '.min.js') %>" ]:
341+
"<%= grunt.option('dist-folder') %>/<%= grunt.option('filename') %>"
324342
},
325343
options: {
326344
sourceMap: {
327-
filename: "dist/<%= grunt.option('filename').replace('.js', '.min.map') %>",
345+
filename: "<%= grunt.option('dist-folder') %>/" +
346+
"<%= grunt.option('filename')" +
347+
".replace(/\\.js$/, '.min.map') %>",
328348

329349
// The map's `files` & `sources` property are set incorrectly, fix
330350
// them via overrides from the task config.
@@ -338,15 +358,15 @@ module.exports = function( grunt ) {
338358
},
339359
swc: {
340360
format: {
341-
ecma: 5,
361+
ecma: grunt.option( "esm" ) ? 2015 : 5,
342362
asciiOnly: true,
343363
comments: false,
344364
preamble: "/*! jQuery v4.0.0-pre | " +
345365
"(c) OpenJS Foundation and other contributors | " +
346366
"jquery.org/license */\n"
347367
},
348368
compress: {
349-
ecma: 5,
369+
ecma: grunt.option( "esm" ) ? 2015 : 5,
350370
hoist_funs: false,
351371
loops: false
352372
},
@@ -359,7 +379,7 @@ module.exports = function( grunt ) {
359379

360380
// Load grunt tasks from NPM packages
361381
require( "load-grunt-tasks" )( grunt, {
362-
pattern: nodeV14OrNewer ? [ "grunt-*" ] : [ "grunt-*", "!grunt-eslint" ]
382+
pattern: nodeV16OrNewer ? [ "grunt-*" ] : [ "grunt-*", "!grunt-eslint" ]
363383
} );
364384

365385
// Integrate jQuery specific tasks
@@ -370,6 +390,20 @@ module.exports = function( grunt ) {
370390
grunt.log.writeln( "Old Node.js detected, running the task \"" + task + "\" skipped..." );
371391
} );
372392

393+
grunt.registerTask( "build-all-variants",
394+
"Build all variants of the full/slim build & a script/ESM one",
395+
function() {
396+
const done = this.async();
397+
398+
spawn( "npm run build-all-variants", {
399+
stdio: "inherit",
400+
shell: true
401+
} )
402+
.on( "close", code => {
403+
done( code === 0 );
404+
} );
405+
} );
406+
373407
grunt.registerTask( "print_jsdom_message", () => {
374408
grunt.log.writeln( "Node.js 17 or newer detected, skipping jsdom tests..." );
375409
} );
@@ -393,7 +427,7 @@ module.exports = function( grunt ) {
393427
runIfNewNode( "newer:eslint:dist" )
394428
] );
395429

396-
grunt.registerTask( "test:fast", runIfNewNode( "node_smoke_tests" ) );
430+
grunt.registerTask( "test:fast", [ "node_smoke_tests:commonjs:jquery" ] );
397431
grunt.registerTask( "test:slow", [
398432
runIfNewNode( "promises_aplus_tests" ),
399433

@@ -419,18 +453,14 @@ module.exports = function( grunt ) {
419453
"build:*:*",
420454
runIfNewNode( "newer:eslint:dev" ),
421455
"newer:minify",
422-
"remove_map_comment",
423456
"dist:*",
424457
"qunit_fixture",
425458
"compare_size"
426459
] );
427460

428461
grunt.registerTask( "default", [
429462
runIfNewNode( "eslint:dev" ),
430-
"build:*:*",
431-
"minify",
432-
"remove_map_comment",
433-
"dist:*",
463+
"build-all-variants",
434464
"test:prepare",
435465
runIfNewNode( "eslint:dist" ),
436466
"test:fast",

README.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ The build process shows a message for each dependent module it excludes or inclu
111111

112112
##### AMD name
113113

114-
As an option, you can set the module name for jQuery's AMD definition. By default, it is set to "jquery", which plays nicely with plugins and third-party libraries, but there may be cases where you'd like to change this. Simply set the `"amd"` option:
114+
As an option, you can set the module name for jQuery's AMD definition. By default, it is set to "jquery", which plays nicely with plugins and third-party libraries, but there may be cases where you'd like to change this. Simply pass it to the `--amd` parameter:
115115

116116
```bash
117117
grunt custom --amd="custom-name"
@@ -123,6 +123,30 @@ Or, to define anonymously, set the name to an empty string.
123123
grunt custom --amd=""
124124
```
125125

126+
##### File name
127+
128+
The default name for the built jQuery file is `jquery.js`; it is placed under the `dist/` directory. It's possible to change the file name using the `--filename` parameter:
129+
130+
```bash
131+
grunt custom:slim --filename="jquery.slim.js"
132+
```
133+
134+
This would create a slim version of jQuery and place it under `dist/jquery.slim.js`. In fact, this is exactly the command we use to generate the slim jQuery during the release process.
135+
136+
##### ECMAScript Module (ESM) mode
137+
138+
By default, jQuery generates a regular script JavaScript file. You can also generate an ECMAScript module exporting `jQuery` as the default export using the `--esm` parameter:
139+
140+
```bash
141+
grunt custom --esm
142+
```
143+
144+
The default is `script` but you can also pass it explicitly via `--no-esm`:
145+
146+
```bash
147+
grunt custom --no-esm
148+
```
149+
126150
#### Custom Build Examples
127151

128152
To create a custom build, first check out the version:

0 commit comments

Comments
 (0)