forked from jquery/jquery
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Build: Switch form Terser to SWC for JS minification (jquery#5286)
Also, as part of this, fix the `file` & `sources` properties of the source map file. Fixes jquerygh-5285 Closes jquerygh-5286 Ref jquerygh-5258
- Loading branch information
Showing
5 changed files
with
88 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters