Skip to content

Commit ac233f9

Browse files
authored
Build: Migrate sources to ES modules, use a Rollup-based build system
The new Rollup-based build system is a very simplified version of the jQuery one, without all the complex custom compilation aspects. Also: * add grunt-compare-size * deduplicate camelCase implementations Closes gh-343
1 parent 5241ccd commit ac233f9

30 files changed

Lines changed: 1091 additions & 214 deletions

.eslintignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,5 @@ coverage
22
external
33
node_modules
44
*.min.js
5-
src/intro.js
6-
src/outro.js
75
test/data/jquery-*.js
86
test/data/jquery.mobile-*.js

.eslintrc-browser.json

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@
1818
"env": {},
1919

2020
"globals": {
21-
"window": true,
22-
"define": true,
23-
"module": true
21+
"window": true
2422
},
2523

2624
"rules": {
27-
"one-var": ["error", {"var": "always"}],
28-
"strict": ["error", "function"]
25+
"one-var": [ "error", { "var": "always" } ],
26+
"strict": [ "error", "function" ]
2927
}
3028
}

.eslintrc-node.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"extends": "jquery",
55

66
"parserOptions": {
7-
"ecmaVersion": 2017
7+
"ecmaVersion": 2018
88
},
99

1010
"env": {

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CDN
77
*.patch
88
/*.html
99
.DS_Store
10+
.sizecache.json
1011

1112
# Ignore everything in dist folder except for eslint config
1213
/dist/*

Gruntfile.js

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,23 @@
44

55
module.exports = function( grunt ) {
66

7-
var isTravis = process.env.TRAVIS;
7+
const gzip = require( "gzip-js" );
8+
const isTravis = process.env.TRAVIS;
89

910
// Project configuration.
1011
grunt.initConfig( {
1112
pkg: grunt.file.readJSON( "package.json" ),
12-
files: [
13-
"src/intro.js",
14-
"src/version.js",
15-
"src/compareVersions.js",
16-
"src/migrate.js",
17-
"src/core.js",
18-
"src/ajax.js",
19-
"src/attributes.js",
20-
"src/css.js",
21-
"src/data.js",
22-
"src/effects.js",
23-
"src/event.js",
24-
"src/manipulation.js",
25-
"src/offset.js",
26-
"src/serialize.js",
27-
"src/traversing.js",
28-
"src/deferred.js",
29-
"src/outro.js"
30-
],
13+
compare_size: {
14+
files: [ "dist/jquery-migrate.js", "dist/jquery-migrate.min.js" ],
15+
options: {
16+
compress: {
17+
gz: function( contents ) {
18+
return gzip.zip( contents, {} ).length;
19+
}
20+
},
21+
cache: "build/.sizecache.json"
22+
}
23+
},
3124
tests: {
3225
jquery: [
3326
"dev+git",
@@ -58,6 +51,12 @@ module.exports = function( grunt ) {
5851
dest: "dist/<%= pkg.name %>.js"
5952
}
6053
},
54+
build: {
55+
all: {
56+
src: "src/migrate.js",
57+
dest: "dist/jquery-migrate.js"
58+
}
59+
},
6160
qunit: {
6261
files: [ "test/**/index.html" ]
6362
},
@@ -125,7 +124,7 @@ module.exports = function( grunt ) {
125124
files: [
126125
"https://code.jquery.com/jquery-3.x-git.min.js",
127126
"dist/jquery-migrate.min.js",
128-
"src/compareVersions.js",
127+
"test/data/compareVersions.js",
129128

130129
"test/testinit.js",
131130
"test/migrate.js",
@@ -203,10 +202,15 @@ module.exports = function( grunt ) {
203202
"eslint:dev",
204203
"eslint:dist"
205204
] );
206-
grunt.registerTask( "build", [ "concat", "uglify", "lint" ] );
207205

208-
grunt.registerTask( "default", [ "build", "test" ] );
206+
grunt.registerTask( "default", [
207+
"build",
208+
"uglify",
209+
"lint",
210+
"compare_size",
211+
"test"
212+
] );
209213

210214
// For CI
211-
grunt.registerTask( "ci", [ "build", "test" ] );
215+
grunt.registerTask( "ci", [ "default" ] );
212216
};

build/tasks/build.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* A build task that compiles jQuery Migrate JS modules into one bundle.
3+
*/
4+
5+
"use strict";
6+
7+
module.exports = function( grunt ) {
8+
const path = require( "path" );
9+
const rollup = require( "rollup" );
10+
const rootFolder = path.resolve( `${ __dirname }/../..` );
11+
const srcFolder = path.resolve( `${ rootFolder }/src` );
12+
const read = function( fileName ) {
13+
return grunt.file.read( `${ srcFolder }/${ fileName }` );
14+
};
15+
16+
// Catch `// @CODE` and subsequent comment lines event if they don't start
17+
// in the first column.
18+
const wrapper = read( "wrapper.js" )
19+
.split( /[\x20\t]*\/\/ @CODE\n(?:[\x20\t]*\/\/[^\n]+\n)*/ );
20+
21+
const inputRollupOptions = {};
22+
const outputRollupOptions = {
23+
24+
// The ESM format is not actually used as we strip it during
25+
// the build; it's just that it doesn't generate any extra
26+
// wrappers so there's nothing for us to remove.
27+
format: "esm",
28+
29+
intro: wrapper[ 0 ]
30+
.replace( /\n*$/, "" ),
31+
outro: wrapper[ 1 ]
32+
.replace( /^\n*/, "" )
33+
};
34+
35+
grunt.registerMultiTask(
36+
"build",
37+
"Build jQuery Migrate ECMAScript modules, embed date/version",
38+
async function() {
39+
const done = this.async();
40+
41+
try {
42+
const version = grunt.config( "pkg.version" );
43+
const dest = this.files[ 0 ].dest;
44+
const src = this.files[ 0 ].src[ 0 ];
45+
46+
inputRollupOptions.input = path.resolve( `${ rootFolder }/${ src }` );
47+
48+
const bundle = await rollup.rollup( inputRollupOptions );
49+
50+
const { output: [ { code } ] } = await bundle.generate( outputRollupOptions );
51+
52+
const compiledContents = code
53+
54+
// Embed Version
55+
.replace( /@VERSION/g, version )
56+
57+
// Embed Date
58+
// yyyy-mm-ddThh:mmZ
59+
.replace(
60+
/@DATE/g,
61+
( new Date() ).toISOString()
62+
.replace( /:\d+\.\d+Z$/, "Z" )
63+
);
64+
65+
grunt.file.write( `${ rootFolder }/${ dest }`, compiledContents );
66+
grunt.log.ok( `File '${ dest }' created.` );
67+
done();
68+
} catch ( err ) {
69+
done( err );
70+
}
71+
} );
72+
};

dist/.eslintrc.json

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
{
2-
"extends": "../src/.eslintrc.json",
32
"root": true,
3+
4+
"extends": "../src/.eslintrc.json",
5+
6+
"parserOptions": {
7+
"ecmaVersion": 5,
8+
"sourceType": "script"
9+
},
10+
11+
"globals": {
12+
"define": false,
13+
"module": true,
14+
"Proxy": false,
15+
"Reflect": false
16+
},
17+
418
"rules": {
519
// That is okay for the built version
620
"no-multiple-empty-lines": "off",

0 commit comments

Comments
 (0)