🌐 AI搜索 & 代理 主页
Skip to content

Commit 7d0a8f6

Browse files
committed
Build: Auto-convert sources to AMD
jQuery source has been migrated in jquerygh-4541 from AMD to ES modules. To maintain support for consumers of our AMD modules, this commits adds a task transpiling the ES modules sources in `src/` to AMD in `amd/`.
1 parent 44ac8c8 commit 7d0a8f6

File tree

11 files changed

+99
-13
lines changed

11 files changed

+99
-13
lines changed

.eslintignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
external
1+
amd
22
node_modules
33
*.min.js
44
dist/**

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ npm-debug.log*
1616
/dist/*
1717
!/dist/.eslintrc.json
1818

19+
/amd
20+
1921
/node_modules
2022

2123
/test/data/core/jquery-iterability-transpiled.js

Gruntfile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ module.exports = function( grunt ) {
178178

179179
{ pattern: "dist/jquery.*", included: false, served: true },
180180
{ pattern: "src/**", type: "module", included: false, served: true },
181+
{ pattern: "amd/**", included: false, served: true },
181182
{ pattern: "node_modules/**", included: false, served: true },
182183
{
183184
pattern: "test/**/*.@(js|css|jpg|html|xml|svg)",

build/release.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,23 @@ module.exports = function( Release ) {
2222

2323
npmTags = Release.npmTags;
2424

25+
function setSrcVersion( filepath ) {
26+
var contents = fs.readFileSync( filepath, "utf8" );
27+
contents = contents.replace( /@VERSION/g, Release.newVersion );
28+
fs.writeFileSync( filepath, contents, "utf8" );
29+
}
30+
2531
Release.define( {
2632
npmPublish: true,
2733
issueTracker: "github",
2834

2935
/**
30-
* Set the version in the src folder for distributing AMD
36+
* Set the version in the src folder for distributing ES modules
37+
* and in the amd folder for AMD.
3138
*/
3239
_setSrcVersion: function() {
33-
var corePath = __dirname + "/../src/core.js",
34-
contents = fs.readFileSync( corePath, "utf8" );
35-
contents = contents.replace( /@VERSION/g, Release.newVersion );
36-
fs.writeFileSync( corePath, contents, "utf8" );
40+
setSrcVersion( `${ __dirname }/../src/core.js` );
41+
setSrcVersion( `${ __dirname }/../amd/core.js` );
3742
},
3843

3944
/**
@@ -45,7 +50,7 @@ module.exports = function( Release ) {
4550
generateArtifacts: function( callback ) {
4651
Release.exec( "grunt", "Grunt command failed" );
4752
Release.exec(
48-
"grunt custom:-ajax,-effects --filename=jquery.slim.js && " +
53+
"grunt custom:-ajax,-callbacks,-deferred,-effects --filename=jquery.slim.js && " +
4954
"grunt remove_map_comment --filename=jquery.slim.js",
5055
"Grunt custom failed"
5156
);

build/release/dist.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module.exports = function( Release, files, complete ) {
1313

1414
// These files are included with the distribution
1515
extras = [
16+
"amd",
1617
"src",
1718
"LICENSE.txt",
1819
"AUTHORS.txt",

build/tasks/amd.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Compiles sources from ES Modules in `src/` to AMD in `amd/`.
3+
*/
4+
5+
"use strict";
6+
7+
module.exports = function( grunt ) {
8+
const path = require( "path" );
9+
const rimraf = require( "rimraf" );
10+
const rollup = require( "rollup" );
11+
const srcFolder = path.resolve( __dirname, "..", "..", "src" );
12+
const amdFolder = path.resolve( srcFolder, "..", "amd" );
13+
const inputFileName = "jquery.js";
14+
15+
const inputRollupOptions = {
16+
input: `${ srcFolder }/${ inputFileName }`,
17+
preserveModules: true
18+
};
19+
20+
const outputRollupOptions = {
21+
format: "amd",
22+
dir: "amd"
23+
};
24+
25+
grunt.registerTask(
26+
"amd",
27+
"Convert ES modules from `src/` to AMD modules in `amd/`",
28+
async function() {
29+
const done = this.async();
30+
31+
try {
32+
grunt.verbose.writeln( "Removing the 'amd' directory..." );
33+
rimraf( amdFolder, async function() {
34+
const bundle = await rollup.rollup( inputRollupOptions );
35+
await bundle.write( outputRollupOptions );
36+
grunt.log.ok( "Sources from 'src' converted to AMD in 'amd'." );
37+
done();
38+
} );
39+
} catch ( err ) {
40+
done( err );
41+
}
42+
} );
43+
};

build/tasks/build.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,11 @@ module.exports = function( grunt ) {
6969
* Indicates we're walking a directory
7070
*/
7171
const excludeList = ( list, prepend ) => {
72+
console.log( "excludeList", list, prepend );
7273
if ( list ) {
7374
prepend = prepend ? `${ prepend }/` : "";
7475
list.forEach( function( module ) {
76+
console.log( "module", module );
7577

7678
// Exclude var modules as well
7779
if ( module === "var" ) {
@@ -108,6 +110,7 @@ module.exports = function( grunt ) {
108110
* whether it should included or excluded
109111
*/
110112
const excluder = flag => {
113+
console.log( "excluder", flag );
111114
let additional;
112115
const m = /^(\+|\-|)([\w\/-]+)$/.exec( flag );
113116
const exclude = m[ 1 ] === "-";
@@ -225,17 +228,23 @@ module.exports = function( grunt ) {
225228

226229
// Replace excluded modules with empty sources.
227230
for ( const module of excluded ) {
231+
console.log( "Erasing... ", `${ srcFolder }/${ module }.js` );
228232
rollupHypotheticalOptions.files[ `${ srcFolder }/${ module }.js` ] = "";
229233
}
230234
}
231235

236+
console.log( "included", included );
237+
console.log( "excluded", excluded );
238+
232239
// Turn off opt-in if necessary
233240
if ( !optIn ) {
234241

235242
// Remove the default inclusions, they will be overwritten with the explicitly
236243
// included ones.
237244
rollupHypotheticalOptions.files[ inputRollupOptions.input ] = "";
238245

246+
console.log( `1 rollupHypotheticalOptions.files[ ${ inputRollupOptions.input } ]`,
247+
rollupHypotheticalOptions.files[ inputRollupOptions.input ] );
239248
}
240249

241250
// Import the explicitly included modules.
@@ -245,6 +254,9 @@ module.exports = function( grunt ) {
245254
.join( "\n" );
246255
}
247256

257+
console.log( `2 rollupHypotheticalOptions.files[ ${ inputRollupOptions.input } ]`,
258+
rollupHypotheticalOptions.files[ inputRollupOptions.input ] );
259+
248260
const bundle = await rollup.rollup( {
249261
...inputRollupOptions,
250262
plugins: [ rollupHypothetical( rollupHypotheticalOptions ) ]

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@
6060
"qunit": "2.9.2",
6161
"raw-body": "2.3.3",
6262
"requirejs": "2.3.6",
63-
"rollup": "1.25.2",
63+
"rimraf": "3.0.0",
64+
"rollup": "1.27.5",
6465
"rollup-plugin-hypothetical": "2.1.0",
6566
"sinon": "7.3.1",
6667
"strip-json-comments": "2.0.1",

test/data/testinit.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,9 @@ QUnit.testUnlessIE = QUnit.isIE ? QUnit.skip : QUnit.test;
300300
this.loadTests = function() {
301301

302302
// Directly load tests that need synchronous evaluation
303-
if ( !QUnit.urlParams.esmodules || document.readyState === "loading" ) {
303+
if ( ( !QUnit.urlParams.esmodules && !QUnit.urlParams.amd ) ||
304+
document.readyState === "loading" ) {
305+
304306
document.write( "<script src='" + parentUrl + "test/unit/ready.js'><\x2Fscript>" );
305307
} else {
306308
QUnit.module( "ready", function() {

test/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<!-- See testinit for the list of tests -->
2020
<script src="data/testinit.js"></script>
2121

22-
<!-- A script that includes jQuery min, dev, or ES modules -->
22+
<!-- A script that includes jQuery min, dev, ES modules or AMD -->
2323
<!-- Adds "basic" URL option, even to iframes -->
2424
<!-- iframes will not load AMD as loading needs to be synchronous for some tests -->
2525
<!-- Also executes the function above to load tests -->
@@ -29,7 +29,7 @@
2929
// Load tests if they have not been loaded
3030
// This is in a different script tag to ensure that
3131
// jQuery is on the page when the testrunner executes
32-
if ( !QUnit.urlParams.esmodules ) {
32+
if ( !QUnit.urlParams.esmodules && !QUnit.urlParams.amd ) {
3333
loadTests();
3434
}
3535
</script>

0 commit comments

Comments
 (0)