-
Notifications
You must be signed in to change notification settings - Fork 2
/
vite.config.ts
69 lines (54 loc) · 1.52 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
/// <reference types="vitest" />
import { UserConfig, defineConfig, mergeConfig, Plugin } from "vite";
import react from "@vitejs/plugin-react";
import dts from "vite-plugin-dts";
import preserveDirectives from "rollup-preserve-directives";
const injectCompiledCSS: Plugin = {
name: "inject-css",
enforce: "post", // Ensure this runs after Vite's internal CSS handling
generateBundle(_options, bundle) {
let css = "";
Object.values(bundle).forEach((file) => {
if (file.fileName.endsWith(".css") && file.type === "asset") {
css += file.source.toString();
}
});
const source = Object.values(bundle).find((file) => {
return file.type === "chunk" && file.fileName.endsWith("index.js");
})!;
if (source.type === "chunk") {
source.code = source.code.replace(`"INLINED_CSS_GOES_HERE"`, JSON.stringify(css));
}
},
};
const buildLibraryConfig: UserConfig = {
root: ".",
plugins: [dts({ rollupTypes: true }), preserveDirectives(), injectCompiledCSS],
build: {
outDir: "esm",
lib: {
entry: ["src/index.ts", "src/index_unstyled.ts"],
formats: ["es"],
},
rollupOptions: {
external: ["react"],
},
},
};
const devConfig: UserConfig = {
root: "web",
test: {
root: "src",
environment: "happy-dom",
},
build: {
target: "es2022",
},
plugins: [react()],
};
export default defineConfig((configEnv) => {
if (process.env.BUNDLE_LIBRARY) {
return mergeConfig(devConfig, buildLibraryConfig);
}
return devConfig;
});