Skip to content

Commit 568e1f7

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. But the bigger problem is that we generate ES2015+ minified code even for `1.13.3` releases which support IE. This change switches the minfied to SWC and ensures ES5 output. Tests ensuring this behavior have been added. Also, GitHub Actions were updated, as the previous versions stopped working. To avoid this happening in the future, automatic dependabot PRs updating actions are now enabled. Fixes gh-629 Closes gh-630 Ref mishoo/UglifyJS#5967 Ref #629
1 parent 2a2b884 commit 568e1f7

File tree

10 files changed

+388
-263
lines changed

10 files changed

+388
-263
lines changed

.github/dependabot.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: github-actions
4+
directory: "/"
5+
schedule:
6+
interval: monthly
7+
8+
# Group all dependabot version update PRs into one
9+
groups:
10+
github-actions:
11+
applies-to: version-updates
12+
patterns:
13+
- "*"

.github/workflows/node.js.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ jobs:
1515
NODE_VERSION: [18.x, 20.x, 22.x]
1616
steps:
1717
- name: Checkout
18-
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
18+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
1919

20-
- name: Install xsltproc
20+
- name: Install xsltproc & ImageMagick
2121
run: |
22-
sudo apt-get install xsltproc
22+
sudo apt-get install xsltproc imagemagick
2323
2424
- name: Use Node.js ${{ matrix.NODE_VERSION }}
25-
uses: actions/setup-node@1e60f620b9541d16bece96c5465dc8ee9832be0b # v4.0.3
25+
uses: actions/setup-node@1d0ff469b7ec7b3cb9d8673fde0c81c44821de2a # v4.2.0
2626
with:
2727
node-version: ${{ matrix.NODE_VERSION }}
2828

2929
- name: Cache
30-
uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4.0.2
30+
uses: actions/cache@d4323d4df104b026a6aa633fdb11d772146be0bf # v4.2.2
3131
with:
3232
path: ~/.npm
3333
key: ${{ runner.os }}-node-${{ matrix.NODE_VERSION }}-npm-lock-${{ hashFiles('**/package-lock.json') }}

Gruntfile.js

Lines changed: 5 additions & 6 deletions
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();

grunt-tasks/minify.js

Lines changed: 26 additions & 0 deletions
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+
};

lib/jquery-ui-files.js

Lines changed: 6 additions & 2 deletions
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
}

lib/package.js

Lines changed: 3 additions & 7 deletions
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
},

lib/swc-options.js

Lines changed: 21 additions & 0 deletions
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)