2 * Minify JavaScript using SWC.
7 module.exports = ( grunt ) => {
8 const swc = require( "@swc/core" );
10 grunt.registerMultiTask(
12 "Minify JavaScript using SWC",
14 const done = this.async();
15 const options = this.options();
16 const sourceMapFilename = options.sourceMap && options.sourceMap.filename;
17 const sourceMapOverrides = options.sourceMap && options.sourceMap.overrides || {};
19 await Promise.all( this.files.map( async( { src, dest } ) => {
20 if ( src.length !== 1 ) {
21 grunt.fatal( "The minify task requires a single source per destination" );
24 const { code, map: incompleteMap } = await swc.minify(
25 grunt.file.read( src[ 0 ] ),
28 inlineSourcesContent: false,
29 sourceMap: sourceMapFilename ?
31 filename: sourceMapFilename
37 // Can't seem to get SWC to not use CRLF on Windows, so replace them with LF.
38 grunt.file.write( dest, code.replace( /\r\n/g, "\n" ) );
40 if ( sourceMapFilename ) {
42 // Apply map overrides if needed. See the task config description
45 ...JSON.parse( incompleteMap ),
48 const map = JSON.stringify( mapObject );
50 grunt.file.write( sourceMapFilename, map );