-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.ts
113 lines (105 loc) · 2.64 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
import * as path from "path";
import react from "@vitejs/plugin-react";
import { resolve } from "path";
import { defineConfig } from "vite";
import tsconfigPaths from "vite-tsconfig-paths";
import banner from "vite-plugin-banner";
import styleInject from "./plugins/style-inject";
import packageJson from "./package.json";
const getPackageName = () => {
return packageJson.name;
};
const getPackageNameCamelCase = () => {
try {
return getPackageName().replace(/-./g, (char) => char[1].toUpperCase());
} catch (err) {
throw new Error("Name property in package.json is missing.");
}
};
const fileNames = {
es: `${getPackageName()}.es.js`,
umd: `${getPackageName()}.umd.js`,
iife: `${getPackageName()}.iife.js`,
};
const pkgInfo = `/**
* name: ${packageJson.name}
* version: ${packageJson.version}
* description: ${packageJson.description}
* author: ${packageJson.author}
* homepage: ${packageJson.homepage}
* repository: ${packageJson.repository.url}
*/`;
// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
/*
// when command line: vite
if (command === "serve") {
// do something
}
// when command line: vite build
else if (command === "build") {
// do something
// fs.rmdirSync("./dist", { recursive: true });
}
// such as command line: vite --mode development
if (mode === "development") {
// do something
}
// such as command line: vite build --mode production
else if (mode === "production") {
// do something
}
*/
if(mode === "lib") {
return {
base: "./",
build: {
lib: {
entry: path.resolve(__dirname, "src/lib/index.tsx"),
name: getPackageNameCamelCase(),
formats: ["es", "iife", "umd"],
fileName: (format) => fileNames[format],
},
rollupOptions: {
external: ["react", "react-dom"],
output: {
assetFileNames: `${getPackageName()}.[ext]`,
globals: {
react: "React",
"react-dom": "ReactDOM",
},
exports: "named",
},
},
emptyOutDir: true,
assetsDir: "assets",
},
plugins: [
react({}),
banner(pkgInfo),
styleInject(),
// dts({
// insertTypesEntry: true,
// }),
],
resolve: {
alias: {
"@/*": path.resolve(__dirname, "src"),
},
},
}
}
return {
plugins: [react(), tsconfigPaths()],
server: {
port: 3030,
https: false,
open: true
},
resolve: {
alias: {
"@/*": resolve(__dirname, "src"),
},
},
}
});