This repository has been archived by the owner on Apr 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
vite.config.ts
115 lines (108 loc) · 2.97 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
/// <reference types="vite/client" />
/// <reference types="vitest" />
import { readFileSync } from "fs";
import { resolve } from "path";
import yaml from "@rollup/plugin-yaml";
import react from "@vitejs/plugin-react";
import { readEnv } from "read-env";
import { defineConfig, loadEnv, type Plugin } from "vite";
import cesium from "vite-plugin-cesium";
import tsconfigPaths from "vite-tsconfig-paths";
import { configDefaults } from "vitest/config";
export default defineConfig({
envPrefix: "REEARTH_WEB_",
plugins: [react(), yaml(), cesium(), serverHeaders(), config(), tsconfigPaths()],
define: {
"process.env.QTS_DEBUG": "false", // quickjs-emscripten
},
server: {
port: 3000,
},
build: {
assetsDir: "static", // avoid conflicts with backend asset endpoints
rollupOptions: {
input: {
main: resolve(__dirname, "index.html"),
published: resolve(__dirname, "published.html"),
},
},
},
resolve: {
alias: [
{ find: "crypto", replacement: "crypto-js" }, // quickjs-emscripten
{ find: "csv-parse", replacement: "csv-parse/browser/esm" },
],
},
test: {
environment: "jsdom",
setupFiles: ["src/test/setup.ts"],
exclude: [...configDefaults.exclude, "e2e/*"],
coverage: {
all: true,
include: ["src/**/*.ts", "src/**/*.tsx"],
exclude: [
"src/**/*.d.ts",
"src/**/*.cy.tsx",
"src/**/*.stories.tsx",
"src/gql/graphql-client-api.tsx",
"src/test/**/*",
],
reporter: ["text", "json", "lcov"],
},
alias: [
{ find: "crypto", replacement: "crypto" }, // reset setting for quickjs-emscripten
{ find: "csv-parse", replacement: "csv-parse" },
],
},
});
function serverHeaders(): Plugin {
return {
name: "server-headers",
configureServer(server) {
server.middlewares.use((_req, res, next) => {
res.setHeader("Service-Worker-Allowed", "/");
next();
});
},
};
}
function config(): Plugin {
return {
name: "reearth-config",
async configureServer(server) {
const configRes = JSON.stringify(
{
api: "http://localhost:8080/api",
published: "/published.html?alias={}",
...readEnv("REEARTH_WEB", {
source: loadEnv(
server.config.mode,
server.config.envDir ?? process.cwd(),
server.config.envPrefix,
),
}),
...loadJSON("./reearth-config.json"),
},
null,
2,
);
server.middlewares.use((req, res, next) => {
if (req.method === "GET" && req.url === "/reearth_config.json") {
res.statusCode = 200;
res.setHeader("Content-Type", "application/json");
res.write(configRes);
res.end();
} else {
next();
}
});
},
};
}
function loadJSON(path: string): any {
try {
return JSON.parse(readFileSync(path, "utf8")) || {};
} catch (err) {
return {};
}
}