-
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
-
85
-
86
-
87
-
88
-
89
-
90
-
91
-
92
-
93
-
94
-
95
-
96
-
97
-
98
-
99
-
100
-
101
-
102
-
103
-
104
-
105
-
106
-
107
-
108
-
109
-
110
-
111
-
112
-
113
-
114
-
115
-
116
-
117
-
118
-
119
-
120
-
121
-
122
-
123
-
124
-
125
-
126
-
127
-
128
-
129
-
130
-
131
-
132
-
133
-
134
-
135
-
136
-
137
-
138
-
139
-
140
-
141
-
142
-
143
-
144
-
145
-
146
-
147
-
148
-
149
-
150
-
151
-
152
-
153
-
154
-
155
-
156
-
157
-
158
// This script bundles `esm/es2019/ef.js`, with variants.
// The resulted files can be found at `dist/`. This command also copies
// top-level `LICENSE` file into `dist/` for minified bundles. The result
// files are archived and saved as `bundle.zip`.
//
// This script reads JS files tsc emits, as Bun bundler does not perform
// any transform.
import chalk from "chalk";
import { mkdir } from "node:fs/promises";
import { basename, relative } from "node:path";
import JsZip from "jszip";
import { bytesToString } from "./_filesize.js";
import { getNetworkFilesize } from "./print-filesize.js";
import { version } from "../package.json";
import HEADER from "../HEADER.txt";
const rootDir = new URL("../", import.meta.url);
const outputDir = new URL("dist/", rootDir);
const jsDir = new URL("esm/es2019/", rootDir);
interface BundleOptions {
minify?: boolean;
header?: boolean;
}
export async function bundle({
minify = false,
header = false,
}: BundleOptions): Promise<Uint8Array> {
// ESBuild can both transform and bundle in single step but unnecessarily changes
// syntax too much. (e.g. `class Foo {}` -> `var Foo = class {}`)
const result = await Bun.build({
entrypoints: [new URL("ef.js", jsDir).pathname],
format: "esm",
minify,
});
const [file] = result.outputs;
if (!file) {
throw new Error("`Bun.build` returned zero-length array for `outputs`");
}
if (result.outputs.length > 1) {
throw new Error(
"Expected a single JS file built, but recieved " +
result.outputs.length +
" files",
);
}
if (!header) {
return new Uint8Array(await file.arrayBuffer());
}
const enc = new TextEncoder();
return enc.encode(HEADER + (await file.text()));
}
function humanBytes(bytes: number): string {
const label = bytesToString(bytes);
if (bytes >= 10_000) {
return chalk.bold(chalk.red(label));
}
if (bytes >= 5_000) {
return chalk.yellow(label);
}
return chalk.blue(label);
}
interface BundledFile {
path: string;
content: Uint8Array;
}
async function bundleAndWrite(
outName: string,
opts: BundleOptions,
): Promise<BundledFile> {
console.error(chalk.gray(`Bundling "%s"...`), outName);
const content = await bundle(opts);
const outPath = relative(process.cwd(), new URL(outName, outputDir).pathname);
console.error(chalk.gray(`Writing to "%s"...`), outPath);
await Bun.write(outPath, content);
const size = getNetworkFilesize(content);
console.error(
chalk.white(`%s: written %s (gzip: %s)`),
outName,
humanBytes(size.raw),
humanBytes(size.gzip),
);
return {
path: outPath,
content,
};
}
if (import.meta.main) {
await mkdir(outputDir, { recursive: true });
const files: BundledFile[] = [];
for (const minify of [true, false]) {
for (const header of [true, false]) {
const modifier = [minify ? "m" : "", header ? "h" : ""].join("");
files.push(
await bundleAndWrite(`ef-${version}${modifier}.js`, {
header,
minify,
}),
);
}
}
const licenseSrc = new URL("LICENSE", rootDir);
const licenseDst = new URL("LICENSE", outputDir);
console.error(
chalk.gray(`Copying "%s" to "%s"...`),
relative(process.cwd(), licenseSrc.pathname),
relative(process.cwd(), licenseDst.pathname),
);
const licenseText = await Bun.file(licenseSrc);
await Bun.write(licenseDst, licenseText);
console.error(chalk.white("LICENSE: copied"));
const zip = new JsZip();
const folder = zip.folder("ef")!;
folder.file("LICENSE", await licenseText.arrayBuffer());
for (const file of files) {
folder.file(basename(file.path), file.content);
}
const zipFile = await zip.generateAsync({ type: "uint8array" });
await Bun.write(new URL("bundle.zip", rootDir), zipFile);
}