-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathvite.config.ts
65 lines (62 loc) · 2.37 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
import { defineConfig } from "vite";
import pkg from "./package.json" with { type: "json" };
const indexInput = "src/index.ts";
const tokenInput = "src/token.ts";
const globalVarName = pkg.name;
export default defineConfig(({ mode }) => {
const isCJSBuild = mode === "production";
return {
build: {
// for UMD build we do not want to empty directory, so previous builds stay intact
emptyOutDir: isCJSBuild,
// don't minify CJS build, Node.js doesn't benefit from it
minify: !isCJSBuild,
sourcemap: true,
// UMD build should target the lowest level ES,
// while CJS the lowest Node.js LTS compatible version
target: isCJSBuild ? "es2022" : "es6",
lib: {
// leave out token export from UMD build
entry: isCJSBuild ? [indexInput, tokenInput] : indexInput,
name: isCJSBuild ? undefined : globalVarName,
formats: isCJSBuild ? ["cjs"] : ["umd"],
fileName: (format, entryName) => {
switch (format) {
case "umd":
return `umd/${entryName}.min.js`;
case "cjs":
return `cjs/${entryName}.cjs`;
default:
throw new Error(`unsupported format ${format}`);
}
},
},
rollupOptions: isCJSBuild
? {
// make sure external imports that should not be bundled are listed here for CJS build
external: ["node:crypto"],
}
: // the following code enables Vite in UMD mode to extend the global object with all of
// the exports, and not just a property of it ( https://github.com/vitejs/vite/issues/11624 )
// TODO: Remove this in the future ( https://github.com/meilisearch/meilisearch-js/issues/1806 )
{
output: {
footer: `(function () {
if (typeof self !== "undefined") {
var clonedGlobal = Object.assign({}, self.${globalVarName});
delete clonedGlobal.default;
Object.assign(self, clonedGlobal);
}
})();`,
},
},
},
test: {
include: ["tests/**/*.test.ts"],
exclude: ["tests/env/**"],
fileParallelism: false,
testTimeout: 100_000, // 100 seconds
coverage: { include: ["src/**/*.ts"] },
},
};
});