forked from jquery/jquery-migrate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminify.js
71 lines (60 loc) · 1.61 KB
/
minify.js
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
import UglifyJS from "uglify-js";
import path from "node:path";
import { getTimestamp } from "./lib/getTimestamp.js";
import { readFile, writeFile } from "node:fs/promises";
import { processForDist } from "./dist.js";
const rjs = /\.js$/;
export async function minify( { dir, filename, version } ) {
// Prepend migratemute.js to the minified file
const muteFilename = "migratemute.js";
const muteContents = await readFile( path.join( "src", muteFilename ), "utf8" );
const contents = await readFile( path.join( dir, filename ), "utf8" );
const banner =
`/*! jQuery Migrate v${ version }` +
" | (c) OpenJS Foundation and other contributors" +
" | jquery.com/license */";
const minFilename = filename.replace( rjs, ".min.js" );
const mapFilename = filename.replace( rjs, ".min.map" );
const {
code,
error,
map,
warning
} = UglifyJS.minify(
{
"../src/migratemute.js": muteContents,
[ filename ]: contents
},
{
compress: {
hoist_funs: false,
loops: false
},
output: {
ascii_only: true,
preamble: banner
},
sourceMap: {
filename: minFilename,
url: mapFilename
}
}
);
if ( error ) {
throw new Error( error );
}
if ( warning ) {
console.warn( warning );
}
await Promise.all( [
writeFile( path.join( dir, minFilename ), code ),
writeFile( path.join( dir, mapFilename ), map )
] );
processForDist( muteContents, muteFilename );
processForDist( contents, filename );
processForDist( code, minFilename );
processForDist( map, mapFilename );
console.log(
`[${ getTimestamp() }] ${ minFilename } ${ version } with ${ mapFilename } created.`
);
}