-
-
Notifications
You must be signed in to change notification settings - Fork 140
/
vite.config.ts
132 lines (119 loc) · 4.79 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/// <reference types="vitest" />
import { existsSync, readFileSync } from "fs";
import path from "path";
import { defineConfig } from "vite";
import { nodePolyfills } from "vite-plugin-node-polyfills";
import { BaseSequencer } from "vitest/node";
import packageJson from "./package.json";
import { testRpcPlugin } from "./test/rpc/rpc-server";
const cubismSubmodule = path.resolve(__dirname, "cubism");
const cubism2Core = path.resolve(__dirname, "core/live2d.min.js");
const cubism4Core = path.resolve(__dirname, "core/live2dcubismcore.js");
if (!existsSync(cubismSubmodule) || !existsSync(path.resolve(cubismSubmodule, "package.json"))) {
throw new Error(
"Cubism submodule not found. Please run `git submodule update --init` to download them. If you have trouble downloading the submodule, please check out DEVELOPMENT.md for possible solutions.",
);
}
if (!existsSync(cubism2Core) || !existsSync(cubism4Core)) {
throw new Error("Cubism Core not found. Please run `npm run setup` to download them.");
}
export default defineConfig(({ command, mode }) => {
const isDev = command === "serve";
const isTest = mode === "test";
return {
define: {
__DEV__: isDev,
__VERSION__: JSON.stringify(packageJson.version),
// test env
__HEADLESS__: process.env.CI === "true",
},
resolve: {
alias: {
"@": path.resolve(__dirname, "src"),
"@cubism": path.resolve(__dirname, "cubism/src"),
},
},
server: {
open: !isTest && "/playground/index.html",
},
build: {
target: "es6",
lib: {
entry: "",
name: "PIXI.live2d",
},
rollupOptions: {
external(id, parentId, isResolved) {
if (id === "pixi.js") {
throw new Error("do not import pixi.js, import @pixi/* instead");
}
return id.startsWith("@pixi/");
},
output: {
extend: true,
globals(id: string) {
if (id.startsWith("@pixi/")) {
const packageJsonPath = path.resolve(
__dirname,
`./node_modules/${id}/package.json`,
);
const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf-8"));
return packageJson.namespace || "PIXI";
}
},
},
},
minify: false,
},
plugins: [
// pixi.js imports a polyfill package named "url", which breaks Vitest
// see https://github.com/vitest-dev/vitest/issues/4535
isTest && nodePolyfills(),
isTest && testRpcPlugin(),
isTest && {
name: "load-cubism-core",
enforce: "post" as const,
transform(code, id) {
if (id.includes("test/load-cores.ts")) {
code = code.replace(
"__CUBISM2_CORE_SOURCE__",
readFileSync(cubism2Core, "utf-8"),
);
code = code.replace(
"__CUBISM4_CORE_SOURCE__",
readFileSync(cubism4Core, "utf-8"),
);
return { code };
}
},
},
],
test: {
include: ["**/*.test.ts", "**/*.test.js"],
browser: {
enabled: true,
name: "chrome",
slowHijackESM: false,
},
setupFiles: ["./test/setup.ts"],
sequence: {
sequencer: class MySequencer extends BaseSequencer {
// use the default sorting, then put bundle tests at the end
// to make sure they will not pollute the environment for other tests
override async sort(files: Parameters<BaseSequencer["sort"]>[0]) {
files = await super.sort(files);
const bundleTestFiles: typeof files = [];
files = files.filter(([project, file]) => {
if (file.includes("bundle")) {
bundleTestFiles.push([project, file]);
return false;
}
return true;
});
return [...files, ...bundleTestFiles];
}
},
},
},
};
});