-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsup-defaults.ts
72 lines (64 loc) · 1.67 KB
/
tsup-defaults.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
import { defineConfig } from "tsup";
interface ExportDef {
node: string;
import: string;
require: string;
types: string;
}
interface PkgJson {
name: string;
description: string;
version: string;
author: string;
license: string;
main?: string;
module?: string;
types?: string;
since: number;
exports?: ExportDef | ExportDef[];
optionalDependencies?: Record<string, string>;
}
function arrayWrap<T>(it: T): T extends any[] ? T : T[] {
return Array.isArray(it) ? (it as any) : [it];
}
function entries(pkg: PkgJson) {
const exports = arrayWrap(
pkg.exports ?? {
node: "./",
import: pkg.module,
require: pkg.main,
types: pkg.types,
}
);
return exports.map(({ types }) => types.replace("dist", "src").replace(".d.ts", ".ts"));
}
function externals(pkg: PkgJson) {
return Object.keys(pkg.optionalDependencies ?? {});
}
function dateRange(since: number) {
const now = new Date().getFullYear();
if (now === since) return since;
return `${since} - ${now}`;
}
function banner(pkg: PkgJson) {
const lines = [pkg.name, pkg.description, `© ${dateRange(pkg.since)} ${pkg.author}`, `@license ${pkg.license}`]
.filter((it) => it)
.map(String);
return `/**!${lines.map((line) => `\n * ${line}`)}\n */`;
}
export function tsupDefaults(pkg: PkgJson) {
const external = externals(pkg);
const entry = entries(pkg);
return defineConfig(async ({ watch }) => ({
entry,
target: "node18",
external,
dts: !watch,
minify: false,
format: watch ? ["cjs"] : ["esm", "cjs"],
sourcemap: !watch,
onSuccess: watch ? "pnpm run start" : undefined,
banner: { js: banner(pkg) },
clean: true,
}));
}