| 1 | "use strict"; |
| 2 | |
| 3 | const swc = require( "@swc/core" ); |
| 4 | |
| 5 | module.exports = function( grunt ) { |
| 6 | |
| 7 | grunt.registerMultiTask( "minify", async function() { |
| 8 | const done = this.async(); |
| 9 | const options = this.options(); |
| 10 | |
| 11 | for ( const file of this.files ) { |
| 12 | if ( file.src.length === 0 ) { |
| 13 | grunt.log.writeln( |
| 14 | `No source file found, skipping minification to "${ file.dest }".` ); |
| 15 | continue; |
| 16 | } |
| 17 | if ( file.src.length !== 1 ) { |
| 18 | grunt.fail.warn( "Minifying multiple source files into one " + |
| 19 | "destination file not supported" ); |
| 20 | } |
| 21 | |
| 22 | const contents = grunt.file.read( file.src[ 0 ] ); |
| 23 | |
| 24 | const { code } = await swc.minify( |
| 25 | contents, |
| 26 | { |
| 27 | compress: { |
| 28 | ecma: 5, |
| 29 | hoist_funs: false, |
| 30 | loops: false |
| 31 | }, |
| 32 | format: { |
| 33 | ecma: 5, |
| 34 | asciiOnly: true, |
| 35 | comments: false, |
| 36 | preamble: options.banner |
| 37 | }, |
| 38 | inlineSourcesContent: false, |
| 39 | mangle: true, |
| 40 | module: false, |
| 41 | sourceMap: false |
| 42 | } |
| 43 | ); |
| 44 | |
| 45 | grunt.file.write( file.dest, code ); |
| 46 | |
| 47 | grunt.log.writeln( `File ${ file.dest } created.` ); |
| 48 | } |
| 49 | |
| 50 | done(); |
| 51 | } ); |
| 52 | |
| 53 | }; |