]> source.dussan.org Git - jquery.git/commitdiff
Build: Switch form Terser to SWC for JS minification (#5286)
authorMichał Gołębiowski-Owczarek <m.goleb@gmail.com>
Mon, 10 Jul 2023 16:23:07 +0000 (18:23 +0200)
committerGitHub <noreply@github.com>
Mon, 10 Jul 2023 16:23:07 +0000 (18:23 +0200)
Also, as part of this, fix the `file` & `sources` properties of the source map
file.

Fixes gh-5285
Closes gh-5286
Ref gh-5258

Gruntfile.js
build/tasks/build.js
build/tasks/minify.js [new file with mode: 0644]
build/tasks/sourcemap.js
package.json

index 65aec9648a0df07deaab22b377a16baeadacd509..b5cbc1bf3ff0a90b7108edbb4d596bfd382e2468 100644 (file)
@@ -312,27 +312,41 @@ module.exports = function( grunt ) {
                        files: [ "<%= eslint.dev.src %>" ],
                        tasks: [ "dev" ]
                },
-               terser: {
+               minify: {
                        all: {
                                files: {
                                        "dist/<%= grunt.option('filename').replace('.js', '.min.js') %>":
                                                "dist/<%= grunt.option('filename') %>"
                                },
                                options: {
-                                       ecma: 5,
                                        sourceMap: {
-                                               filename: "dist/<%= grunt.option('filename').replace('.js', '.min.map') %>"
-                                       },
-                                       format: {
-                                               ascii_only: true,
-                                               comments: false,
-                                               preamble: "/*! jQuery v<%= pkg.version %> | " +
-                                                       "(c) OpenJS Foundation and other contributors | " +
-                                                       "jquery.org/license */"
+                                               filename: "dist/<%= grunt.option('filename').replace('.js', '.min.map') %>",
+
+                                               // The map's `files` & `sources` property are set incorrectly, fix
+                                               // them via overrides from the task config.
+                                               // See https://github.com/swc-project/swc/issues/7588#issuecomment-1624345254
+                                               overrides: {
+                                                       file: "jquery.min.js",
+                                                       sources: [
+                                                               "jquery.js"
+                                                       ]
+                                               }
                                        },
-                                       compress: {
-                                               hoist_funs: false,
-                                               loops: false
+                                       swc: {
+                                               format: {
+                                                       ecma: 5,
+                                                       asciiOnly: true,
+                                                       comments: false,
+                                                       preamble: "/*! jQuery v4.0.0-pre | " +
+                                                               "(c) OpenJS Foundation and other contributors | " +
+                                                               "jquery.org/license */\n"
+                                               },
+                                               compress: {
+                                                       ecma: 5,
+                                                       hoist_funs: false,
+                                                       loops: false
+                                               },
+                                               mangle: true
                                        }
                                }
                        }
@@ -400,7 +414,7 @@ module.exports = function( grunt ) {
        grunt.registerTask( "dev", [
                "build:*:*",
                runIfNewNode( "newer:eslint:dev" ),
-               "newer:terser",
+               "newer:minify",
                "remove_map_comment",
                "dist:*",
                "qunit_fixture",
@@ -410,7 +424,7 @@ module.exports = function( grunt ) {
        grunt.registerTask( "default", [
                runIfNewNode( "eslint:dev" ),
                "build:*:*",
-               "terser",
+               "minify",
                "remove_map_comment",
                "dist:*",
                "test:prepare",
index 98d95cbe5c8c3760d70d181802bd6797b57efaac..e1dd39993ec09b92f5e7386f3db31ebcd105881a 100644 (file)
@@ -339,6 +339,6 @@ module.exports = function( grunt ) {
                        "";
 
                grunt.log.writeln( "Creating custom build...\n" );
-               grunt.task.run( [ "build:*:*" + ( modules ? ":" + modules : "" ), "terser", "dist" ] );
+               grunt.task.run( [ "build:*:*" + ( modules ? ":" + modules : "" ), "minify", "dist" ] );
        } );
 };
diff --git a/build/tasks/minify.js b/build/tasks/minify.js
new file mode 100644 (file)
index 0000000..1a6ddbd
--- /dev/null
@@ -0,0 +1,56 @@
+/**
+ * Minify JavaScript using SWC.
+ */
+
+"use strict";
+
+module.exports = ( grunt ) => {
+       const swc = require( "@swc/core" );
+
+       grunt.registerMultiTask(
+               "minify",
+               "Minify JavaScript using SWC",
+               async function() {
+                       const done = this.async();
+                       const options = this.options();
+                       const sourceMapFilename = options.sourceMap && options.sourceMap.filename;
+                       const sourceMapOverrides = options.sourceMap && options.sourceMap.overrides || {};
+
+                       await Promise.all( this.files.map( async( { src, dest } ) => {
+                               if ( src.length !== 1 ) {
+                                       grunt.fatal( "The minify task requires a single source per destination" );
+                               }
+
+                               const { code, map: incompleteMap } = await swc.minify(
+                                       grunt.file.read( src[ 0 ] ),
+                                       {
+                                               ...options.swc,
+                                               inlineSourcesContent: false,
+                                               sourceMap: sourceMapFilename ?
+                                                       {
+                                                               filename: sourceMapFilename
+                                                       } :
+                                                       false
+                                       }
+                               );
+
+                               grunt.file.write( dest, code );
+
+                               if ( sourceMapFilename ) {
+
+                                       // Apply map overrides if needed. See the task config description
+                                       // for more details.
+                                       const mapObject = {
+                                               ...JSON.parse( incompleteMap ),
+                                               ...sourceMapOverrides
+                                       };
+                                       const map = JSON.stringify( mapObject );
+
+                                       grunt.file.write( sourceMapFilename, map );
+                               }
+                       } ) );
+
+                       done();
+               }
+       );
+};
index f6ef5698e3bd3173ac490d973511a46b91c00969..8b9c248e07faf2d7be06fc378b938a6a00bf3d0d 100644 (file)
@@ -3,7 +3,7 @@
 var fs = require( "fs" );
 
 module.exports = function( grunt ) {
-       var config = grunt.config( "terser.all.files" );
+       var config = grunt.config( "minify.all.files" );
        grunt.registerTask( "remove_map_comment", function() {
                var minLoc = grunt.config.process( Object.keys( config )[ 0 ] );
 
index dd92954d323e9e4d262e9346a2b2d69e79a229fe..e02aa9b80a33a15a7d0f1ac7b0b44cfc7d8aa053 100644 (file)
@@ -26,6 +26,7 @@
   "devDependencies": {
     "@babel/core": "7.10.5",
     "@babel/plugin-transform-for-of": "7.10.4",
+    "@swc/core": "1.3.66",
     "colors": "1.4.0",
     "commitplease": "3.2.0",
     "core-js-bundle": "3.6.5",
@@ -42,7 +43,6 @@
     "grunt-karma": "4.0.2",
     "grunt-newer": "1.3.0",
     "grunt-npmcopy": "0.2.0",
-    "grunt-terser": "2.0.0",
     "gzip-js": "0.3.2",
     "husky": "4.2.5",
     "jsdom": "19.0.0",
@@ -67,7 +67,6 @@
     "rollup": "2.21.0",
     "sinon": "7.3.1",
     "strip-json-comments": "3.1.1",
-    "terser": "5.17.6",
     "testswarm": "1.1.2"
   },
   "scripts": {