aboutsummaryrefslogtreecommitdiffstats
path: root/build/tasks/minify.js
blob: 147b9a89d99af8b5d6ace99519a23de8900a515c (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import fs from "node:fs/promises";
import path from "node:path";
import UglifyJS from "uglify-js";
import processForDist from "./dist.js";
import getTimestamp from "./lib/getTimestamp.js";

const rjs = /\.js$/;

export default async function minify( { dir, filename } ) {
	const filepath = path.join( dir, filename );
	const contents = await fs.readFile( filepath, "utf8" );
	const version = /jQuery JavaScript Library ([^\n]+)/.exec( contents )[ 1 ];
	const banner = `/*! jQuery ${ version }` +
		" | (c) OpenJS Foundation and other contributors" +
		" | jquery.org/license */";

	const minFilename = filename.replace( rjs, ".min.js" );
	const mapFilename = filename.replace( rjs, ".min.map" );

	const { code, error, map: incompleteMap, warning } = UglifyJS.minify(
		contents,
		{
			compress: {
				hoist_funs: false,
				loops: false,

				// Support: IE <11
				// typeofs transformation is unsafe for IE9-10
				// See https://github.com/mishoo/UglifyJS2/issues/2198
				typeofs: false
			},
			output: {
				ascii_only: true,

				// Support: Android 4.0 only
				// UglifyJS 3 breaks Android 4.0 if this option is not enabled.
				// This is in lieu of setting ie for all of mangle, compress, and output
				ie8: true,
				preamble: banner
			},
			sourceMap: {
				filename: minFilename
			}
		}
	);

	if ( error ) {
		throw new Error( error );
	}

	if ( warning ) {
		console.warn( warning );
	}

	// The map's `sources` property is set to an array index.
	// Fix it by setting it to the correct filename.
	const map = JSON.stringify( {
		...JSON.parse( incompleteMap ),
		file: minFilename,
		sources: [ filename ]
	} );

	await Promise.all( [
		fs.writeFile(
			path.join( dir, minFilename ),
			code
		),
		fs.writeFile(
			path.join( dir, mapFilename ),
			map
		)
	] );

	// Always process files for dist
	// Doing it here avoids extra file reads
	processForDist( contents, filename );
	processForDist( code, minFilename );
	processForDist( map, mapFilename );

	console.log( `[${ getTimestamp() }] ${ minFilename } ${ version } with ${
		mapFilename
	} created.` );
}