diff options
author | Michał Gołębiowski-Owczarek <m.goleb@gmail.com> | 2023-07-10 18:23:07 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-10 18:23:07 +0200 |
commit | e24218758bb21bfdc296731d70f3d48ab786e5f5 (patch) | |
tree | 2453e401853a59bbd6f84f26dbe64dcdd1b6c025 /build | |
parent | 198b41c8c2cd726b875615023b2b37b213040ad3 (diff) | |
download | jquery-e24218758bb21bfdc296731d70f3d48ab786e5f5.tar.gz jquery-e24218758bb21bfdc296731d70f3d48ab786e5f5.zip |
Build: Switch form Terser to SWC for JS minification (#5286)
Also, as part of this, fix the `file` & `sources` properties of the source map
file.
Fixes gh-5285
Closes gh-5286
Ref gh-5258
Diffstat (limited to 'build')
-rw-r--r-- | build/tasks/build.js | 2 | ||||
-rw-r--r-- | build/tasks/minify.js | 56 | ||||
-rw-r--r-- | build/tasks/sourcemap.js | 2 |
3 files changed, 58 insertions, 2 deletions
diff --git a/build/tasks/build.js b/build/tasks/build.js index 98d95cbe5..e1dd39993 100644 --- a/build/tasks/build.js +++ b/build/tasks/build.js @@ -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 index 000000000..1a6ddbd4a --- /dev/null +++ b/build/tasks/minify.js @@ -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(); + } + ); +}; diff --git a/build/tasks/sourcemap.js b/build/tasks/sourcemap.js index f6ef5698e..8b9c248e0 100644 --- a/build/tasks/sourcemap.js +++ b/build/tasks/sourcemap.js @@ -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 ] ); |