diff options
Diffstat (limited to 'build/tasks/minify.js')
-rw-r--r-- | build/tasks/minify.js | 56 |
1 files changed, 56 insertions, 0 deletions
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(); + } + ); +}; |