-
Notifications
You must be signed in to change notification settings - Fork 3
/
vite.config.js
124 lines (117 loc) · 3.82 KB
/
vite.config.js
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { defineConfig } from "vite";
import { extname, relative } from "path";
import { fileURLToPath } from "node:url";
import { glob } from "glob";
import { visualizer } from "rollup-plugin-visualizer";
import dts from "vite-plugin-dts";
import svgr from "vite-plugin-svgr";
import { babel } from "@rollup/plugin-babel";
import { execSync } from "child_process";
import path from "path";
import { readFileSync } from "fs";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const pkg = JSON.parse(readFileSync(new URL("./package.json", import.meta.url)));
const input = Object.fromEntries(
glob
.sync(["src/index.tsx", "src/components/**/*.{ts,tsx}"], { ignore: "src/**/*.stories.{js,jsx,ts,tsx}" })
.map((file) => [
relative("src", file.slice(0, file.length - extname(file).length)),
fileURLToPath(new URL(file, import.meta.url)),
]),
);
function preserveUseClient() {
return {
name: "preserve-use-client",
enforce: "post",
generateBundle(options, bundle) {
Object.entries(bundle).forEach(([_, chunk]) => {
if (chunk.type === "chunk") {
chunk.code = chunk.code.replace(
/("use client";|('use client';))?/,
'"use client";\n'
);
}
});
},
};
}
function generateColors() {
return {
name: "generate-colors",
buildStart: {
sequential: true,
handler() {
console.log("Generating color system...");
execSync("node src/scripts/generateColourClasses.js", { stdio: "inherit" });
},
},
configureServer(server) {
// Generate on dev server start
console.log("Generating color system...");
execSync("node src/scripts/generateColourClasses.js", { stdio: "inherit" });
// Watch for changes in the script
server.watcher.add("src/scripts/generateColourClasses.js");
server.watcher.on("change", (path) => {
if (path.endsWith("generateColourClasses.js")) {
console.log("Color generation script changed, regenerating...");
execSync("node scripts/generateColourClasses.js", { stdio: "inherit" });
}
});
},
};
}
function createVisualizer() {
return {
...visualizer({ gzipSize: true }),
apply: "build",
};
}
export default defineConfig({
build: {
minify: "terser",
terserOptions: {
format: {
comments: false,
preserve_annotations: true,
},
},
lib: {
entry: input,
name: pkg.name,
fileName: "index",
},
rollupOptions: {
output: [
{
format: "es",
entryFileNames: "[name].js",
assetFileNames: "index.[ext]",
banner: `"use client;"`,
},
],
plugins: [
babel({
exclude: "node_modules/**",
babelHelpers: "bundled",
presets: [
["@babel/preset-env", {
useBuiltIns: "usage",
corejs: 3,
targets: {
esmodules: true,
},
}],
"@babel/preset-react",
],
}),
],
external: [...Object.keys(pkg.peerDependencies)],
},
},
plugins: [
generateColors(),
svgr(),
preserveUseClient(),
createVisualizer(),
],
});