Build: replace CRLF with LF during minify
[jquery.git] / build / tasks / minify.js
blob6d3c6c568b758d338a7e1c8a482ad0aad04cc5ec
1 /**
2  * Minify JavaScript using SWC.
3  */
5 "use strict";
7 module.exports = ( grunt ) => {
8         const swc = require( "@swc/core" );
10         grunt.registerMultiTask(
11                 "minify",
12                 "Minify JavaScript using SWC",
13                 async function() {
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" );
22                                 }
24                                 const { code, map: incompleteMap } = await swc.minify(
25                                         grunt.file.read( src[ 0 ] ),
26                                         {
27                                                 ...options.swc,
28                                                 inlineSourcesContent: false,
29                                                 sourceMap: sourceMapFilename ?
30                                                         {
31                                                                 filename: sourceMapFilename
32                                                         } :
33                                                         false
34                                         }
35                                 );
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
43                                         // for more details.
44                                         const mapObject = {
45                                                 ...JSON.parse( incompleteMap ),
46                                                 ...sourceMapOverrides
47                                         };
48                                         const map = JSON.stringify( mapObject );
50                                         grunt.file.write( sourceMapFilename, map );
51                                 }
52                         } ) );
54                         done();
55                 }
56         );