-
Notifications
You must be signed in to change notification settings - Fork 86
/
bundle.mjs
79 lines (74 loc) · 1.98 KB
/
bundle.mjs
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
import chalk from "chalk";
import { build } from "esbuild";
import ignorePlugin from "esbuild-plugin-ignore";
import { stat } from "node:fs/promises";
import { parse } from "node:path";
import { performance } from "perf_hooks";
import readPackageJson from "read-package-json";
const name = "d3-dag";
const start = performance.now();
const today = new Date();
async function wrapper(options) {
const res = await build(options);
const { outfile } = options;
const { size } = await stat(outfile);
const { dir, base } = parse(outfile);
console.log(
chalk.white(`\n ${dir}/`) + chalk.bold(`${base}`),
chalk.cyan(` ${(size / 1024).toFixed(1)}kb`),
);
return res;
}
// read package.json and dat to get info for preamble
const pkg = await new Promise((res, rej) =>
readPackageJson("./package.json", (err, dat) => (err ? rej(err) : res(dat))),
);
const config = {
entryPoints: ["src/index.ts"],
tsconfig: "tsconfig.build.json",
target: "es6", // needs to be defined despite tsconfig
bundle: true,
minify: true,
plugins: [
ignorePlugin([
{ resourceRegExp: /^fs$/ },
{ resourceRegExp: /^child_process$/ },
]),
],
banner: {
js: `// ${pkg.name} Version ${
pkg.version
}. Copyright ${today.getFullYear()} ${pkg.author.name}.`,
},
};
await Promise.all([
// build iife
wrapper({
...config,
platform: "browser",
outfile: `bundle/${name}.iife.min.js`,
// NOTE special commands to update d3
globalName: "d3",
banner: {
js: `${config.banner.js}\nvar d3 = Object.assign(d3 || {}, (() => {`,
},
footer: {
js: "return d3; })())",
},
}),
// build cjs
wrapper({
...config,
platform: "node",
outfile: `bundle/${name}.cjs.min.js`,
}),
// build esm
wrapper({
...config,
platform: "neutral",
outfile: `bundle/${name}.esm.min.js`,
mainFields: ["module", "main"],
}),
]);
const elapsed = Math.round(performance.now() - start);
console.log("\n⚡", chalk.green(`Done in ${elapsed}ms`));