-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsup.config.ts
76 lines (67 loc) · 1.87 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
75
76
import childProcess from "node:child_process";
import fs from "node:fs";
import { readFile } from "node:fs/promises";
import path from "node:path";
import { type Options, defineConfig } from "tsup";
const common: Options = {
entry: ["src/index.ts", "src/styles.css"],
treeshake: false,
sourcemap: "inline",
minify: true,
clean: true,
dts: true,
splitting: false,
format: ["cjs", "esm"],
external: ["react"],
injectStyle: false,
};
const getPackageName = async () => {
try {
const packageJson = JSON.parse(
await readFile(path.join(__dirname, "package.json"), "utf-8"),
);
return packageJson.name;
} catch (_error) {
return "package-name";
}
};
const _addUseStatement = async (
basePath: string,
type: "server" | "client",
) => {
const fullPath = path.join(__dirname, basePath);
const files = fs.readdirSync(fullPath);
for (const file of files) {
if (file.endsWith(".js") || file.endsWith(".mjs")) {
const filePath = path.join(fullPath, file);
let content = await readFile(filePath, "utf-8");
content = `"use ${type}";\n${content}`;
fs.writeFileSync(filePath, content, "utf-8");
}
}
};
const linkSelf = async () => {
await new Promise((resolve) => {
childProcess.exec("pnpm link:self", (error, _stdout, _stderr) => {
if (error) {
// biome-ignore lint/suspicious/noConsole: <explanation>
console.error(`exec error: ${error}`);
return;
}
resolve(undefined);
});
});
// biome-ignore lint/suspicious/noConsoleLog: <explanation>
// biome-ignore lint/suspicious/noConsole: <explanation>
console.log(
`Run 'pnpm link ${await getPackageName()} --global' inside another project to consume this package.`,
);
};
export default defineConfig({
async onSuccess() {
// If you want need to add a use statement to files, you can use the following code:
await _addUseStatement("dist", "client");
await linkSelf();
},
...common,
});