-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.lib.config.ts
More file actions
94 lines (84 loc) · 2.73 KB
/
Copy pathvite.lib.config.ts
File metadata and controls
94 lines (84 loc) · 2.73 KB
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
import react from "@vitejs/plugin-react-swc";
import fs from "node:fs";
import path from "node:path";
import { defineConfig, type UserConfig } from "vite";
type DependencyMap = Record<string, string>;
type PackageJson = Readonly<{
dependencies?: DependencyMap;
peerDependencies?: DependencyMap;
}>;
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null;
}
function asDependencyMap(value: unknown): DependencyMap | undefined {
if (!isRecord(value)) return undefined;
const out: Record<string, string> = {};
for (const [k, v] of Object.entries(value)) {
if (typeof v === "string") out[k] = v;
}
return out;
}
function readPackageJson(): PackageJson {
const pkgPath = path.resolve(__dirname, "package.json");
const raw = JSON.parse(fs.readFileSync(pkgPath, "utf-8")) as unknown;
if (!isRecord(raw)) return {};
return {
dependencies: asDependencyMap(raw.dependencies),
peerDependencies: asDependencyMap(raw.peerDependencies),
};
}
/**
* Library build config (ESM-only) for `@devbrock/comic-ui`.
*
* This is separate from the demo app Vite config (`vite.config.ts`) so the
* demo can keep its own dev-server settings/plugins.
*/
export default defineConfig(() => {
const pkg = readPackageJson();
const external = new Set<string>([
...Object.keys(pkg.dependencies ?? {}),
...Object.keys(pkg.peerDependencies ?? {}),
"react/jsx-runtime",
]);
const config = {
plugins: [react()],
publicDir: false as const,
resolve: {
alias: {
"@": path.resolve(__dirname, "./src"),
},
},
build: {
lib: {
entry: path.resolve(__dirname, "src/index.ts"),
formats: ["es"],
fileName: () => "index.js",
},
outDir: "dist",
emptyOutDir: true,
sourcemap: true,
copyPublicDir: false,
rollupOptions: {
external: Array.from(external),
output: {
/**
* Preserve module boundaries so consumers (especially Next.js RSC)
* don't end up importing a single aggregated entry file that hoists
* client-only imports (e.g. `react-hook-form`) into `dist/index.js`.
*/
preserveModules: true,
preserveModulesRoot: path.resolve(__dirname, "src"),
/**
* Mark the library entry/chunks as client modules for Next.js App Router.
*
* Without this, Next may attempt to compile the package in a Server
* Components context and resolve `react-hook-form` to its `react-server`
* entrypoint (which does not export `Controller`).
*/
banner: '"use client";',
},
},
},
} satisfies UserConfig;
return config;
});