-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.js
78 lines (68 loc) · 2.4 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
import { resolve, basename, extname, dirname } from "path";
import { defineConfig } from "vite";
import { readdirSync, statSync } from "fs";
import dts from "vite-plugin-dts";
const srcDir = resolve(__dirname, "src");
// Generate an entry point for each TypeScript file in `src`, including files in subdirectories
const entries = readdirSync(srcDir).reduce((entryPoints, fileOrDir) => {
const fullPath = resolve(srcDir, fileOrDir);
const isDirectory = statSync(fullPath).isDirectory();
if (isDirectory) {
// For directories, add the `index.ts` file inside each as an entry
entryPoints[fileOrDir] = resolve(fullPath, "index.ts");
} else if (extname(fileOrDir) === ".ts" && !fileOrDir.endsWith(".d.ts")) {
// For standalone TypeScript files in `src` (like `config.ts`), add them directly
entryPoints[basename(fileOrDir, ".ts")] = fullPath;
}
return entryPoints;
}, {});
const getFileName = (chunk, extension) => {
// Files directly in `src` should go to the root of `dist` (e.g., config.mjs)
// Files in subdirectories should go to their own folders (e.g., moduleA/index.mjs)
// Check if targetPath is within srcDir
const targetPath = chunk.facadeModuleId;
// Resolve both paths to their absolute forms to ensure consistency
const absoluteFilePath = resolve(targetPath);
const absoluteParentFolderPath = resolve(srcDir);
// Get the directory containing the file
const fileDirectory = dirname(absoluteFilePath);
// Compare the file's directory with the parent folder path
const isInSrcDir = fileDirectory === absoluteParentFolderPath;
return isInSrcDir
? `${chunk.name}.${extension}`
: `${chunk.name}/index.${extension}`;
};
export default defineConfig({
build: {
sourcemap: true,
lib: {
entry: entries,
},
rollupOptions: {
input: entries,
output: [
{
// ESM format
format: "es",
entryFileNames: (chunk) => getFileName(chunk, "mjs"),
dir: "dist",
preserveModules: true,
preserveModulesRoot: "src",
},
{
// CJS format
format: "cjs",
entryFileNames: (chunk) => getFileName(chunk, "cjs"),
dir: "dist",
preserveModules: true,
preserveModulesRoot: "src",
},
],
},
},
test: {
globals: true,
setupFiles: "vitest.setup.ts",
},
plugins: [dts({ exclude: "**/*.(test|bench).ts" })],
});