-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvite.config.ts
148 lines (136 loc) · 4.55 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
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
148
import { defineConfig, type PluginOption } from "vite";
import { svelte } from "@sveltejs/vite-plugin-svelte";
import { transform } from "esbuild";
import * as pkg from "./package.json";
import sveltePreprocess from "svelte-preprocess";
import dts from "vite-plugin-dts";
import { readdirSync, readFileSync, writeFileSync } from "fs";
import * as fs from "fs";
const bundleComponents = process.env.BUNDLE_COMPONENTS ?? true;
export default defineConfig({
root: "./packages/lib/",
build: {
outDir: "../../dist",
lib: {
entry: "./index.ts",
// eslint-disable-next-line @typescript-eslint/no-explicit-any
formats: bundleComponents ? (["es", "esm", "umd"] as any) : ["es"],
name: pkg.name.replace(/-./g, (char) => char[1].toUpperCase()),
fileName: (format): string => {
const name = pkg.name.replace("@samply/", "");
switch (format) {
case "es":
return `${name}.js`;
case "esm":
return `${name}.js`;
case "umd":
return `${name}.umd.js`;
default:
return name;
}
},
},
rollupOptions: {
output: bundleComponents
? {}
: {
inlineDynamicImports: false,
chunkFileNames: "[name].js",
manualChunks: { svelte: ["svelte"] },
},
},
},
plugins: [
svelte({
preprocess: [sveltePreprocess({ typescript: true })],
compilerOptions: {
customElement: true,
},
}),
dts({
insertTypesEntry: true,
include: ["**/types/*.ts"],
afterBuild: afterBuild,
}),
minifyEs(),
],
});
/**
* Workaround for https://github.com/vitejs/vite/issues/6555
* Minify the es output
* @returns the minifyEs plugin
*/
function minifyEs(): PluginOption {
return {
name: "minifyEs",
renderChunk: {
order: "post" as const,
async handler(code, chunk, outputOptions) {
if (
outputOptions.format === "es" &&
(!bundleComponents || chunk.fileName.endsWith(".min.js"))
) {
return await transform(code, { minify: true });
}
return code;
},
},
};
}
/**
* runs after the dts plugin has finished
*/
function afterBuild(): void {
concatenateDeclarationFiles("./dist/src/types/");
}
/**
* Concatenate all declaration files into a single file
* @param folderPath the path where the declaration files are located
*/
function concatenateDeclarationFiles(folderPath: string): void {
const declarationFiles = readdirSync(folderPath)
.map((file) => readFileSync(`${folderPath}${file}`, "utf-8"))
.join("\n");
// Write the concatenated declaration files to a single file and remove the folder
writeFileSync("./dist/types.d.ts", declarationFiles);
removeImportLinesFromFile("./dist/types.d.ts");
fs.rmSync("./dist/src", { recursive: true, force: true });
}
/**
* removes all import statements from a file
* @param filePath the path of the file
*/
function removeImportLinesFromFile(filePath: string): void {
// Read the file content
fs.readFile(
filePath,
"utf8",
(err: NodeJS.ErrnoException | null, data: string) => {
if (err) {
console.error("Error reading file:", err);
return;
}
// Split the content into lines
const lines: string[] = data.split("\n");
// Filter out lines starting with 'import'
const filteredLines: string[] = lines.filter(
(line) => !line.trim().startsWith("import"),
);
// Join the remaining lines
const modifiedContent: string = filteredLines.join("\n");
// Write the modified content back to the file
fs.writeFile(
filePath,
modifiedContent,
"utf8",
(err: NodeJS.ErrnoException | null) => {
if (err) {
console.error("Error writing file:", err);
return;
}
console.log("File updated successfully.");
},
);
},
);
}