-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvite.build.ts
83 lines (78 loc) · 2.03 KB
/
vite.build.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
import { resolve } from "path";
import { defineConfig, LibraryFormats } from "vite";
import react from "@vitejs/plugin-react";
import dts from "vite-plugin-dts";
export const pkg = require("./package.json");
// ============================ config area ============================
const entry = resolve("lib/index.ts");
export const name = "HeTreeReact"; // for umd, iife
export const banner = `
/*!
* ${pkg.name}
* Author: ${pkg.author}
* Released under the ${pkg.license} License.
*/`.trim();
/**
* globals are externals for iife format
*/
export const globals = {
react: "React",
"react-dom": "ReactDOM",
"react/jsx-runtime": "jsxRuntime", // Just a typescript definition library. This line is to remove build warning.
};
// ============================ config area end
// https://vitejs.dev/config/
export const isIIFE = detectIIFE();
export const formats: LibraryFormats[] = !isIIFE ? ["es", "cjs"] : ["iife"];
export default defineConfig({
plugins: [
react(),
!isIIFE &&
dts({
insertTypesEntry: true, // Generate entry of types. Entry path is 'types' in package.json
}),
],
build: {
sourcemap: isIIFE,
emptyOutDir: !isIIFE,
lib: {
entry,
name,
fileName: "index",
formats,
},
rollupOptions: {
external: externalFunction,
output: {
banner,
exports: "auto",
globals,
},
},
},
});
export const esmExternals = [
...Object.keys(pkg["dependencies"] || {}),
...Object.keys(pkg["peerDependencies"] || {}),
];
export const iifeExternals = [
...Object.keys(globals),
...Object.keys(pkg["peerDependencies"] || {}),
];
export function externalFunction(id) {
id = id.replace(/\\/g, "/");
const externals = isIIFE ? iifeExternals : esmExternals;
for (const name of externals) {
if (id.startsWith(name)) {
return true;
}
}
return false;
}
function detectIIFE() {
let index = process.argv.indexOf("--");
if (index > -1 && process.argv.indexOf("--iife", index + 1) > -1) {
return true;
}
return false;
}