Skip to content

Commit 302b488

Browse files
authored
Build: Switch from UglifyJS to SWC minify, make the minified file ES5
More recent UglifyJS versions have started converting regular functions to arrow ones, making ES5 source file migrated to a ES2015+ minified one. We want to avoid that even in 1.14.x as long as we keep the source file in ES5. Closes gh-2335 Ref mishoo/UglifyJS#5967 Ref jquery/download.jqueryui.com#629
1 parent fd7dbcd commit 302b488

File tree

3 files changed

+58
-8
lines changed

3 files changed

+58
-8
lines changed

Gruntfile.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@ const cssFiles = [
5151

5252
// minified files
5353
const minify = {
54-
options: {
55-
preserveComments: false
56-
},
5754
main: {
5855
options: {
5956
banner: createBanner( uiFiles )
@@ -174,7 +171,7 @@ grunt.initConfig( {
174171
}
175172
},
176173

177-
uglify: minify,
174+
minify,
178175
htmllint: {
179176
good: {
180177
options: {
@@ -403,9 +400,9 @@ grunt.registerTask( "lint", [
403400
"csslint",
404401
"htmllint"
405402
] );
406-
grunt.registerTask( "build", [ "requirejs", "concat" ] );
403+
grunt.registerTask( "build", [ "requirejs", "concat", "minify:main" ] );
407404
grunt.registerTask( "default", [ "lint", "build" ] );
408-
grunt.registerTask( "sizer", [ "requirejs:js", "uglify:main", "compare_size:all" ] );
409-
grunt.registerTask( "sizer_all", [ "requirejs:js", "uglify", "compare_size" ] );
405+
grunt.registerTask( "sizer", [ "requirejs:js", "minify:main", "compare_size:all" ] );
406+
grunt.registerTask( "sizer_all", [ "requirejs:js", "minify", "compare_size" ] );
410407

411408
};

build/tasks/minify.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"use strict";
2+
3+
const swc = require( "@swc/core" );
4+
5+
module.exports = function( grunt ) {
6+
7+
grunt.registerMultiTask( "minify", async function() {
8+
const done = this.async();
9+
const options = this.options();
10+
11+
for ( const file of this.files ) {
12+
if ( file.src.length === 0 ) {
13+
grunt.log.writeln(
14+
`No source file found, skipping minification to "${ file.dest }".` );
15+
continue;
16+
}
17+
if ( file.src.length !== 1 ) {
18+
grunt.fail.warn( "Minifying multiple source files into one " +
19+
"destination file not supported" );
20+
}
21+
22+
const contents = grunt.file.read( file.src[ 0 ] );
23+
24+
const { code } = await swc.minify(
25+
contents,
26+
{
27+
compress: {
28+
ecma: 5,
29+
hoist_funs: false,
30+
loops: false
31+
},
32+
format: {
33+
ecma: 5,
34+
asciiOnly: true,
35+
comments: false,
36+
preamble: options.banner
37+
},
38+
inlineSourcesContent: false,
39+
mangle: true,
40+
module: false,
41+
sourceMap: false
42+
}
43+
);
44+
45+
grunt.file.write( file.dest, code );
46+
47+
grunt.log.writeln( `File ${ file.dest } created.` );
48+
}
49+
50+
done();
51+
} );
52+
53+
};

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"jquery": ">=1.12.0 <5.0.0"
5656
},
5757
"devDependencies": {
58+
"@swc/core": "1.11.5",
5859
"commitplease": "3.2.0",
5960
"eslint-config-jquery": "3.0.2",
6061
"grunt": "1.6.1",
@@ -63,7 +64,6 @@
6364
"grunt-contrib-concat": "2.1.0",
6465
"grunt-contrib-csslint": "2.0.0",
6566
"grunt-contrib-requirejs": "1.0.0",
66-
"grunt-contrib-uglify": "5.2.2",
6767
"grunt-eslint": "24.0.1",
6868
"grunt-git-authors": "3.2.0",
6969
"grunt-html": "17.1.0",

0 commit comments

Comments
 (0)