aboutsummaryrefslogtreecommitdiffstats
path: root/build/tasks/minify.js
blob: 6d83831ee3cfd10a9b7eaa6e7f757dc226218963 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"use strict";

const swc = require( "@swc/core" );

module.exports = function( grunt ) {

grunt.registerMultiTask( "minify", async function() {
	const done = this.async();
	const options = this.options();

	for ( const file of this.files ) {
		if ( file.src.length === 0 ) {
			grunt.log.writeln(
				`No source file found, skipping minification to "${ file.dest }".` );
			continue;
		}
		if ( file.src.length !== 1 ) {
			grunt.fail.warn( "Minifying multiple source files into one " +
				"destination file not supported" );
		}

		const contents = grunt.file.read( file.src[ 0 ] );

		const { code } = await swc.minify(
			contents,
			{
				compress: {
					ecma: 5,
					hoist_funs: false,
					loops: false
				},
				format: {
					ecma: 5,
					asciiOnly: true,
					comments: false,
					preamble: options.banner
				},
				inlineSourcesContent: false,
				mangle: true,
				module: false,
				sourceMap: false
			}
		);

		grunt.file.write( file.dest, code );

		grunt.log.writeln( `File ${ file.dest } created.` );
	}

	done();
} );

};