-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.ts
107 lines (100 loc) · 2.77 KB
/
vite.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { isAbsolute, resolve } from "path";
import { defineConfig } from "vite";
import packageJson from "./package.json";
import dts from "vite-plugin-dts";
import { pipe } from "fp-ts/lib/function";
import { globSync } from "glob";
import { A, O, R } from "./src/utils/functions";
import react from "@vitejs/plugin-react";
const peerDeps = Object.keys(packageJson.peerDependencies);
// const getPackageName = () => packageJson.name;
const fileName = {
es: `[name].mjs`,
cjs: `[name].cjs`,
};
const distEntries = {
index: resolve(__dirname, "src/index.ts"),
"worker-utils": resolve(__dirname, "src/worker-utils/index.ts"),
// Add more entry points as needed
};
// Convert matched paths to an object expected by Rollup input option
const examplesInputs = pipe(
globSync(resolve(__dirname, "examples/**/index.html")),
A.filterMap((entry) =>
pipe(
entry.match(/examples\/(.+)\/index.html$/),
O.fromNullable,
O.chain(A.lookup(1)),
O.map((name): [string, string] => [name, entry])
)
),
R.fromEntries
);
export default defineConfig(({ mode }) => {
switch (mode) {
case "examples":
return {
base: "./",
build: {
lib: {
entry: resolve(__dirname, "src/index.ts"),
name: "BuildX",
formats: ["es"],
fileName: "buildx-core",
},
rollupOptions: {
output: {
preserveModules: true,
},
input: {
...examplesInputs,
},
},
},
plugins: [
dts({ rollupTypes: true }),
react({ include: /\.(mdx|jsx|tsx)$/ }),
],
resolve: {
alias: [
{ find: "@", replacement: resolve(__dirname, "src") },
{ find: "@@", replacement: resolve(__dirname) },
],
},
define: {
"process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV),
},
};
default:
case "library":
return {
base: "./",
build: {
lib: {
entry: distEntries,
name: "BuildXCore",
formats: ["es", "cjs"],
fileName: (format, entryName) =>
`${fileName[format].replace("[name]", entryName)}`,
},
rollupOptions: {
external: (id) =>
peerDeps.includes(id) ||
(!id.startsWith("@") && !id.startsWith(".") && !isAbsolute(id)),
},
minify: "terser",
},
resolve: {
alias: [
{ find: "@", replacement: resolve(__dirname, "src") },
{ find: "@@", replacement: resolve(__dirname) },
],
},
plugins: [
dts({
rollupTypes: true,
}),
],
};
}
});