-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.ts
118 lines (107 loc) · 2.89 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
import fs from "fs";
import path from "path";
import { defineConfig } from "vite";
import banner from "vite-plugin-banner";
// import dts from "vite-plugin-dts";
import packageJson from "./package.json";
import styleInject from "./plugins/style-inject";
// node version
const major = process.version.match(/v([0-9]*).([0-9]*)/)[1];
const minor = process.version.match(/v([0-9]*).([0-9]*)/)[2];
/**
* cpSync
* @param {string} source
* @param {string} destination
*/
const cpSync = (source, destination) => {
if (Number(major) < 16 || (Number(major) == 16 && Number(minor) < 7)) {
if (fs.existsSync(destination)) {
fs.rmSync(destination, { recursive: true });
}
fs.mkdirSync(destination, { recursive: true });
const rd = fs.readdirSync(source);
for (const fd of rd) {
const sourceFullName = source + "/" + fd;
const destFullName = destination + "/" + fd;
const lstatRes = fs.lstatSync(sourceFullName);
if (lstatRes.isFile()) {
fs.copyFileSync(sourceFullName, destFullName);
}
if (lstatRes.isDirectory()) {
cpSync(sourceFullName, destFullName);
}
}
} else {
fs.cpSync(source, destination, { force: true, recursive: true });
}
};
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 fileName = {
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}
*/`;
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
}
return {
base: "./",
server: {
port: 8080,
https: false,
open: true
},
build: {
lib: {
entry: path.resolve(__dirname, "src/index.ts"),
name: getPackageNameCamelCase(),
formats: ["es", "umd", "iife"],
fileName: (format) => fileName[format],
},
},
plugins: [
banner(pkgInfo),
styleInject(),
// dts({
// insertTypesEntry: true,
// }),
],
resolve: {
alias: {
"@/*": path.resolve(__dirname, "src"),
},
},
};
});