-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
136 lines (120 loc) · 3.53 KB
/
Copy pathvite.config.ts
File metadata and controls
136 lines (120 loc) · 3.53 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
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
import { defineConfig } from "vite";
import solid from "vite-plugin-solid";
import { vanillaExtractPlugin } from "@vanilla-extract/vite-plugin";
import dts from "vite-plugin-dts";
import path from "node:path";
import { readdirSync } from "node:fs";
function resolve(str: string) {
return path.resolve(__dirname, str);
}
function getEntries() {
const dirs = [
path.resolve(__dirname, "packages/components"),
path.resolve(__dirname, "packages/themes"),
];
const entries: Record<string, string> = {
index: path.resolve(__dirname, "packages/index.ts"),
};
function walk(dir: string, parent: string, prefix = "") {
const items = readdirSync(dir, { withFileTypes: true });
items.forEach((item) => {
const fullPath = path.resolve(dir, item.name);
const relativePath = prefix ? `${prefix}/${item.name}` : item.name;
if (item.isDirectory()) {
walk(fullPath, parent, relativePath);
} else if (
item.isFile() &&
item.name.endsWith(".ts") &&
!item.name.endsWith(".d.ts") &&
!item.name.endsWith(".types.ts")
) {
const name = relativePath.replace(/\.ts$/, "");
entries[`${parent}/${name}`] = fullPath;
}
});
}
try {
dirs.forEach((dir) => {
const parent = path.basename(dir);
walk(dir, parent);
});
} catch (error) {
console.warn("Components directory not found", error);
}
return entries;
}
export default defineConfig(({ mode }) => ({
resolve: {
alias: {
"~/": `${resolve("packages")}/`,
},
},
plugins: [
solid(),
vanillaExtractPlugin({
identifiers: mode === "staging" ? "debug" : "short",
}),
dts({
tsconfigPath: "./tsconfig.app.json",
insertTypesEntry: true,
rollupTypes: false,
}),
{
name: "vite:import-css",
apply: "build",
enforce: "post",
renderChunk(code, chunk) {
const { fileName } = chunk;
if (fileName.startsWith("components/")) {
const dir = path.dirname(fileName);
const componentDir = path.basename(dir);
const componentFileName = `${componentDir.charAt(0).toUpperCase() + componentDir.slice(1)}`;
if (
!fileName.toLocaleLowerCase().includes("context") &&
fileName.endsWith(`${componentFileName}.js`)
) {
const importCode = `import "./styles/${componentFileName}.css"`;
const addedCssCode = `${importCode}\n${code}`;
return { code: addedCssCode, map: null };
}
}
if (fileName === "index.js") {
const importCode = `import "./themes/styles/var.css"\nimport "./themes/styles/theme.css"`;
const addedCssCode = `${importCode}\n${code}`;
return { code: addedCssCode, map: null };
}
},
},
],
build: {
cssCodeSplit: true,
cssMinify: true,
outDir: "lib",
lib: {
entry: getEntries(),
formats: ["es"],
name: "fluent-solid",
},
rollupOptions: {
external: [
"tabster",
/^@vanilla-extract\/.*$/,
/^solid-js(\/.*)?$/,
/^solid-icons\/.*$/,
],
output: {
entryFileNames: "[name].js",
dir: "lib",
preserveModules: true,
preserveModulesRoot: "packages",
assetFileNames: ({ names }) => {
const filename = path
.basename(names[0])
.replace(".ts.vanilla.css", "");
const dir = path.parse(names[0]).dir;
return `${dir}/styles/${filename}`;
},
},
},
},
}));