-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.ts
84 lines (77 loc) · 2.37 KB
/
build.ts
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
84
import fs, { readdirSync } from "node:fs";
import prettyBytes from "pretty-bytes";
import { resolve, dirname } from "pathe";
import UglifyJS from "uglify-js";
// Prepare
const pkgDir = resolve(__dirname);
const libDir = resolve(pkgDir, "./lib");
try {
fs.rmSync(libDir, { recursive: true });
} catch {
// Ignore
}
fs.mkdirSync(libDir, { recursive: true });
// TS package info
const tsPkgPath = require.resolve("typescript/package.json");
// eslint-disable-next-line @typescript-eslint/no-require-imports
const tsPkg = require(tsPkgPath);
const tsPkgDir = dirname(tsPkgPath);
// Copy license
for (const licenseFile of ["LICENSE.txt", "ThirdPartyNoticeText.txt"]) {
console.log(`Copying ${licenseFile}...`);
fs.copyFileSync(resolve(tsPkgDir, licenseFile), resolve(pkgDir, licenseFile));
}
// Copy lib
const libFiles = readdirSync(resolve(tsPkgDir, "./lib"), {
withFileTypes: true,
})
.filter((f) => !f.isDirectory())
.map((f) => f.name);
for (const file of libFiles) {
const filePath = resolve(tsPkgDir, "lib", file);
const fileSize = fs.statSync(filePath).size;
if (
fileSize > 250_000 &&
!file.startsWith("lib.") &&
!file.endsWith(".d.ts")
) {
console.log(`- Minifying lib/${file} (${prettyBytes(fileSize)})`);
const source = fs.readFileSync(filePath, "utf8");
// https://github.com/mishoo/UglifyJS#minify-options
const minified = UglifyJS.minify(source, {
// mangle: false,
// warnings: true,
// keep_fnames: true,
// output: {
// beautify: true,
// },
});
if (minified.error) {
throw minified.error;
}
if (minified.warnings) {
console.log("⚠️ UglifyJS warnings:", minified.warnings.join("\n"));
}
console.log(
`\u001B[1A- Copied lib/${file} (Minified from ${prettyBytes(
fileSize,
)} to ${prettyBytes(minified.code.length)} 💾)`,
);
fs.writeFileSync(resolve(libDir, file), minified.code);
} else {
console.log(
`- Copying lib/${file} (${prettyBytes(fileSize)}${
fileSize > 250_000 ? " 🤯" : ""
})`,
);
fs.copyFileSync(filePath, resolve(libDir, file));
}
}
// Sync version
// eslint-disable-next-line @typescript-eslint/no-require-imports
const pkg = require(resolve(pkgDir, "./package.json"));
pkg.version = tsPkg.version;
fs.writeFileSync(
resolve(pkgDir, "./package.json"),
JSON.stringify(pkg, undefined, 2),
);