-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvite.config.js
147 lines (138 loc) · 4.67 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { defineConfig } from "vite";
import { fileURLToPath } from "node:url";
import { visualizer } from "rollup-plugin-visualizer";
import dts from "vite-plugin-dts";
import svgr from "vite-plugin-svgr";
import react from "@vitejs/plugin-react";
import { execSync } from "child_process";
import path from "path";
import { readFileSync } from "fs";
import ts from "typescript";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const pkg = JSON.parse(readFileSync(new URL("./package.json", import.meta.url)));
const input = {
"index" : "src/index.tsx",
"components/index" : "src/components/index.tsx",
};
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) {
console.log("Generating color system...");
execSync("node src/scripts/generateColourClasses.js", { stdio : "inherit" });
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;"`,
},
],
external : [...Object.keys(pkg.peerDependencies)],
},
},
plugins : [
generateColors(),
svgr(),
preserveUseClient(),
createVisualizer(),
react({
babel : {
presets : [
["@babel/preset-env", { modules : false }],
["@babel/preset-react"],
],
plugins : [
["transform-react-remove-prop-types", { removeImport : true }],
],
},
}),
dts({
insertTypesEntry : true,
include : ["src/**/*"],
outputDir : "dist/types",
skipDiagnostics : false,
compilerOptions : {
baseUrl : ".",
paths : {
"@/*" : ["./src/*"],
"components/*" : ["./src/components/*"],
},
esModuleInterop : true,
allowJs : true,
declaration : true,
emitDeclarationOnly : true,
},
entryRoot : "src",
beforeWriteFile : (filePath, content) => {
// Replace any React imports with the simpler form
content = content.replace(
/import\s*{\s*default\s+as\s+React\s*}\s+from\s+['"]react['"];?/g,
"import React from \"react\";",
);
// Add React import if it's missing and needed
if (!content.includes("import") && content.includes("React.")) {
content = "import React from \"react\";\n" + content;
}
return {
filePath,
content,
};
},
}),
],
});