forked from sinclairzx81/typebox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsup.config.ts
74 lines (64 loc) · 1.84 KB
/
tsup.config.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
import * as esbuild from "esbuild";
import glob from "fast-glob";
import fs from "fs";
import json5 from "json5";
import { defineConfig } from "tsup";
const outDir = "dist";
mkdirSync(outDir);
export default defineConfig({
entry: glob.sync("src/**/*.ts"),
outDir,
format: ["esm", "cjs"],
outExtension: (ctx) => ({
js: ctx.format === "cjs" ? ".js" : ".mjs",
dts: ".d.ts",
}),
bundle: false,
esbuildPlugins: [createPackageJson(), createSubPackages()],
});
function createPackageJson(): esbuild.Plugin {
return createPostPlugin("package-json", () => {
const pkgString = fs.readFileSync("package.json", "utf8");
const pkg = JSON.parse(pkgString);
delete pkg.devDependencies;
delete pkg.scripts;
pkg.main = "index.js";
pkg.module = "index.mjs";
fs.writeFileSync(`${outDir}/package.json`, JSON.stringify(pkg, null, 2));
fs.copyFileSync("license", `${outDir}/license`);
fs.copyFileSync("readme.md", `${outDir}/readme.md`);
});
}
function createSubPackages(): esbuild.Plugin {
return createPostPlugin("sub-packages", () => {
const tsconfigString = fs.readFileSync("tsconfig.json", "utf8");
const tsconfig = json5.parse(tsconfigString);
const { paths } = tsconfig.compilerOptions;
delete paths["@sinclair/typebox"];
for (const name in paths) {
const dir = `${outDir}/${name.replace("@sinclair/typebox/", "")}`;
mkdirSync(dir);
const pkg = {
name,
main: "index.js",
module: "index.mjs",
};
fs.writeFileSync(`${dir}/package.json`, JSON.stringify(pkg, null, 2));
}
});
}
function createPostPlugin(name: string, fn: () => void): esbuild.Plugin {
return {
name,
setup(build) {
build.onEnd(() => {
setTimeout(fn, 1);
});
},
};
}
function mkdirSync(name: string) {
try {
fs.mkdirSync(name);
} catch {}
}