-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvite.config.base.ts
More file actions
111 lines (106 loc) · 2.65 KB
/
Copy pathvite.config.base.ts
File metadata and controls
111 lines (106 loc) · 2.65 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
import { defineConfig, UserConfig } from "vite";
import { resolve } from "path";
import dts from "vite-plugin-dts";
import { bundle_analysis_plugin } from "./scripts/vite-plugin-bundle-analysis";
export const isProd = process.env.TIMELESS_PROD === "1";
export interface BuildOptions {
entry: string;
name?: string;
globalName?: string;
external?: string[];
globals?: Record<string, string>;
define?: Record<string, string>;
formats?: ("es" | "cjs" | "umd")[];
fileName?: (format: string) => string;
minify?: boolean;
sourcemap?: boolean;
dts?: boolean;
alias?: Record<string, string>;
footer?: string;
rollupConfig?: {};
bundle_analysis?: Parameters<typeof bundle_analysis_plugin>[0];
}
export function buildLibName(name?: string, globalName?: string) {
return (
globalName ||
(name ? name.charAt(0).toUpperCase() + name.slice(1) : undefined)
);
}
export function createLibConfig(options: BuildOptions): UserConfig {
const {
entry,
name,
globalName,
external = [],
globals = {},
define = {},
formats = ["es", "cjs"],
fileName,
minify = false,
sourcemap = true,
dts: need_generate_dts = true,
alias = {},
footer,
bundle_analysis,
} = options;
// 如果没有指定 globalName,则将 name 首字母大写作为全局变量名
const plugins = [];
if (need_generate_dts) {
plugins.push(
dts({
insertTypesEntry: true,
rollupTypes: true,
}),
);
}
if (bundle_analysis) {
plugins.push(bundle_analysis_plugin(bundle_analysis));
}
return defineConfig({
define,
resolve: {
alias: Object.entries(alias).map(([find, replacement]) => ({
find,
replacement,
})),
},
build: {
lib: {
entry,
name: buildLibName(name, globalName),
formats,
fileName: (format) => {
if (fileName) return fileName(format);
if (format === "es") {
return name ? `${name}.esm.js` : "index.esm.js";
}
if (format === "cjs") {
return "index.js";
}
if (format === "umd") {
return name ? `${name}.umd.min.js` : "index.umd.js";
}
return `index.${format}.js`;
},
},
minify: isProd ? "terser" : minify ? "terser" : false,
...(isProd && {
terserOptions: {
compress: {
drop_console: true,
},
},
}),
sourcemap: isProd ? false : sourcemap,
rollupOptions: {
external,
output: {
globals,
footer,
},
...options.rollupConfig,
},
},
plugins,
});
}