-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.utils.ts
65 lines (59 loc) · 1.7 KB
/
build.utils.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
import { copyFile, writeFile } from "node:fs/promises";
import { resolve } from "node:path";
import type { LibraryOptions, PluginOption } from "vite";
import dts from "vite-plugin-dts";
export function generateDtsEntryPoints(entryPoints: string[]): PluginOption {
let hasGenerated = false;
return {
name: "generate-dts-entry-points",
async writeBundle(opts) {
if (hasGenerated) return;
hasGenerated = true;
await Promise.all(
entryPoints.map((file) =>
writeFile(`${opts.dir}/${file}.d.ts`, `export * from "./types/lib/${file}";`),
),
);
},
};
}
export function copyPackageJson(): PluginOption {
let hasGenerated = false;
return {
name: "copy-package-json",
async writeBundle() {
if (hasGenerated) return;
hasGenerated = true;
await copyFile("./package.json", "dist/package.json");
await copyFile("./README.md", "dist/README.md");
await copyFile("./LICENSE", "dist/LICENSE");
},
};
}
export function getLibEntry(entryPoints: string[]) {
return entryPoints.reduce(
(acc, file) => {
const ext = file.includes("react") ? "tsx" : "ts";
acc[file] = resolve(__dirname, `src/lib/${file}/index.${ext}`);
return acc;
},
{} as Extract<LibraryOptions["entry"], Record<string, unknown>>,
);
}
export function generateDts() {
return dts({ outputDir: "dist/types", exclude: ["documentation/**"] });
}
export const externalPackages = [
"react",
/^react\/.*/,
"react-dom",
/react-dom\/.*/,
"@types/react",
"@types/react-dom",
"@solana/web3.js",
"ethers",
];
export const pathAliases = {
"@/documentation": resolve(__dirname, "documentation"),
"@": resolve(__dirname, "src"),
};