Skip to content

Commit 97df7e3

Browse files
committed
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. But the bigger problem is that we generate ES2015+ minified code even for 1.13.3 releases which support IE. Closes jquerygh-629 Ref mishoo/UglifyJS#5967 Ref jquery#629
1 parent 2a2b884 commit 97df7e3

File tree

7 files changed

+277
-238
lines changed

7 files changed

+277
-238
lines changed

Diff for: Gruntfile.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ grunt.loadNpmTasks( "grunt-check-modules" );
1111
grunt.loadNpmTasks( "grunt-contrib-clean" );
1212
grunt.loadNpmTasks( "grunt-contrib-copy" );
1313
grunt.loadNpmTasks( "grunt-contrib-handlebars" );
14-
grunt.loadNpmTasks( "grunt-contrib-uglify" );
1514
grunt.loadNpmTasks( "grunt-eslint" );
1615

1716
grunt.initConfig( {
@@ -61,10 +60,7 @@ grunt.initConfig( {
6160
dest: "app/dist"
6261
}
6362
},
64-
uglify: {
65-
options: {
66-
preserveComments: "some"
67-
},
63+
minify: {
6864

6965
// DownloadBuilder minified frontend bundle
7066
download: {
@@ -87,6 +83,9 @@ grunt.initConfig( {
8783
}
8884
} );
8985

86+
// local tasks
87+
grunt.loadTasks( "grunt-tasks" );
88+
9089
function log( callback, successMsg, errorMsg ) {
9190
return function( error, result, code ) {
9291
if ( error && errorMsg ) {
@@ -279,7 +278,7 @@ function buildPackages( folder, callback ) {
279278

280279
grunt.registerTask( "default", [ "check-modules", "eslint", "test" ] );
281280

282-
grunt.registerTask( "build-app", [ "clean", "handlebars", "copy", "uglify" ] );
281+
grunt.registerTask( "build-app", [ "clean", "handlebars", "copy", "minify" ] );
283282

284283
grunt.registerTask( "build-packages", "Builds zip package of each jQuery UI release specified in config file with all components and lightness theme, inside the given folder", function( folder ) {
285284
var done = this.async();

Diff for: grunt-tasks/minify.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"use strict";
2+
3+
const swc = require( "@swc/core" );
4+
const swcOptions = require( "../lib/swc-options" );
5+
6+
module.exports = function( grunt ) {
7+
8+
grunt.registerMultiTask( "minify", async function() {
9+
const done = this.async();
10+
11+
for ( const file of this.files ) {
12+
const contents = file.src
13+
.map( singleFile => grunt.file.read( singleFile ) )
14+
.join( "\n" );
15+
16+
const { code } = await swc.minify( contents, swcOptions );
17+
18+
grunt.file.write( file.dest, code );
19+
20+
grunt.log.writeln( `File ${ file.dest } created.` );
21+
}
22+
23+
done();
24+
} );
25+
26+
};

Diff for: lib/jquery-ui-files.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ var stripBanner, glob, noDirectory,
55
fs = require( "node:fs" ),
66
path = require( "node:path" ),
77
sqwish = require( "sqwish" ),
8-
UglifyJS = require( "uglify-js" ),
8+
swc = require( "@swc/core" ),
9+
swcOptions = require( "./swc-options" ),
910
util = require( "./util" ),
1011
Files = require( "./files" ),
1112
filesCache = {};
@@ -91,7 +92,10 @@ JqueryUiFiles.prototype = {
9192
path: file.path.replace( /\.([^.]*)$/, ".min.$1" )
9293
};
9394
if ( ( /\.js$/i ).test( file.path ) ) {
94-
minified[ file.path ].data = UglifyJS.minify( file.data.toString( "utf8" ) ).code;
95+
minified[ file.path ].data = swc.minifySync(
96+
file.data.toString( "utf8" ),
97+
swcOptions
98+
).code;
9599
} else if ( ( /\.css$/i ).test( file.path ) ) {
96100
minified[ file.path ].data = sqwish.minify( file.data.toString( "utf8" ) );
97101
}

Diff for: lib/package.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ var indexTemplate,
1111
sqwish = require( "sqwish" ),
1212
ThemeRoller = require( "./themeroller" ),
1313
semver = require( "semver" ),
14-
UglifyJS = require( "uglify-js" );
14+
swc = require( "@swc/core" ),
15+
swcOptions = require( "./swc-options" );
1516

1617
indexTemplate = handlebars.compile( fs.readFileSync( __dirname + "/../template/zip/index.html", "utf8" ) );
1718

@@ -340,13 +341,8 @@ extend( Package.prototype, {
340341
}
341342

342343
this.jsBundle.promise.then( function( js ) {
343-
var minJs;
344344
var _banner = banner( pkgJson, jsFileNames, { minify: true } );
345-
var uglifyResult = UglifyJS.minify( js );
346-
if ( uglifyResult.error ) {
347-
throw uglifyResult.error;
348-
}
349-
minJs = uglifyResult.code;
345+
var minJs = swc.minifySync( js, swcOptions ).code;
350346
callback( null, _banner + minJs );
351347
} ).catch( callback );
352348
},

Diff for: lib/swc-options.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"use strict";
2+
3+
module.exports = {
4+
compress: {
5+
ecma: 5,
6+
hoist_funs: false,
7+
loops: false
8+
},
9+
format: {
10+
ecma: 5,
11+
asciiOnly: true,
12+
13+
// That only preserves license comments.
14+
// See https://swc.rs/docs/configuration/minification#note-about-comments
15+
comments: true
16+
},
17+
inlineSourcesContent: false,
18+
mangle: true,
19+
module: false,
20+
sourceMap: false
21+
};

0 commit comments

Comments
 (0)