Skip to content

Build: Switch from UglifyJS to SWC minify, make the minified file ES5 #2335

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Ref mishoo/UglifyJS#5967
Ref jquery/download.jqueryui.com#629
  • Loading branch information
mgol committed Mar 2, 2025
commit df2763431832918046bdac7830b3ef2b166876f2
11 changes: 4 additions & 7 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ const cssFiles = [

// minified files
const minify = {
options: {
preserveComments: false
},
main: {
options: {
banner: createBanner( uiFiles )
Expand Down Expand Up @@ -174,7 +171,7 @@ grunt.initConfig( {
}
},

uglify: minify,
minify,
htmllint: {
good: {
options: {
Expand Down Expand Up @@ -403,9 +400,9 @@ grunt.registerTask( "lint", [
"csslint",
"htmllint"
] );
grunt.registerTask( "build", [ "requirejs", "concat" ] );
grunt.registerTask( "build", [ "requirejs", "concat", "minify:main" ] );
grunt.registerTask( "default", [ "lint", "build" ] );
grunt.registerTask( "sizer", [ "requirejs:js", "uglify:main", "compare_size:all" ] );
grunt.registerTask( "sizer_all", [ "requirejs:js", "uglify", "compare_size" ] );
grunt.registerTask( "sizer", [ "requirejs:js", "minify:main", "compare_size:all" ] );
grunt.registerTask( "sizer_all", [ "requirejs:js", "minify", "compare_size" ] );

};
53 changes: 53 additions & 0 deletions build/tasks/minify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"use strict";

const swc = require( "@swc/core" );

module.exports = function( grunt ) {

grunt.registerMultiTask( "minify", async function() {
const done = this.async();
const options = this.options();

for ( const file of this.files ) {
if ( file.src.length === 0 ) {
grunt.log.writeln(
`No source file found, skipping minification to "${ file.dest }".` );
continue;
}
if ( file.src.length !== 1 ) {
grunt.fail.warn( "Minifying multiple source files into one " +
"destination file not supported" );
}

const contents = grunt.file.read( file.src[ 0 ] );

const { code } = await swc.minify(
contents,
{
compress: {
ecma: 5,
hoist_funs: false,
loops: false
},
format: {
ecma: 5,
asciiOnly: true,
comments: false,
preamble: options.banner
},
inlineSourcesContent: false,
mangle: true,
module: false,
sourceMap: false
}
);

grunt.file.write( file.dest, code );

grunt.log.writeln( `File ${ file.dest } created.` );
}

done();
} );

};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"jquery": ">=1.12.0 <5.0.0"
},
"devDependencies": {
"@swc/core": "1.11.5",
"commitplease": "3.2.0",
"eslint-config-jquery": "3.0.2",
"grunt": "1.6.1",
Expand All @@ -63,7 +64,6 @@
"grunt-contrib-concat": "2.1.0",
"grunt-contrib-csslint": "2.0.0",
"grunt-contrib-requirejs": "1.0.0",
"grunt-contrib-uglify": "5.2.2",
"grunt-eslint": "24.0.1",
"grunt-git-authors": "3.2.0",
"grunt-html": "17.1.0",
Expand Down
Loading