forked from microbit-foundation/cctd-ml-machine
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vite.config.ts
105 lines (100 loc) · 2.82 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
/// <reference types="vitest" />
/**
* (c) 2023, Center for Computational Thinking and Design at Aarhus University and contributors
* Modifications (c) 2024, Micro:bit Educational Foundation and contributors
*
* SPDX-License-Identifier: MIT
*/
import react from "@vitejs/plugin-react";
import ejs from "ejs";
import fs from "node:fs";
import path from "node:path";
import {
IndexHtmlTransformContext,
IndexHtmlTransformResult,
Plugin,
UserConfig,
defineConfig,
loadEnv,
} from "vite";
import svgr from "vite-plugin-svgr";
import { configDefaults } from "vitest/config";
interface TemplateStrings {
appNameFull: string;
ogDescription: undefined | string;
metaDescription: undefined | string;
}
// Support optionally pulling in external branding if the module is installed.
const theme = "@microbit-foundation/ml-trainer-microbit";
const external = `node_modules/${theme}`;
const internal = "src/deployment/default";
const themePackageExternal = fs.existsSync(external);
const themePackageAlias = themePackageExternal
? theme
: path.resolve(__dirname, internal);
const viteEjsPlugin = (data: ejs.Data): Plugin => {
return {
name: "ejs",
transformIndexHtml: {
order: "pre",
handler: (
html: string,
_ctx: IndexHtmlTransformContext
): IndexHtmlTransformResult => ejs.render(html, data),
},
};
};
export default defineConfig(async ({ mode }): Promise<UserConfig> => {
const commonEnv = loadEnv(mode, process.cwd(), "");
const strings: TemplateStrings = themePackageExternal
? // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
((await import(themePackageAlias)).default({}) as TemplateStrings)
: {
appNameFull: "ml-trainer",
ogDescription: undefined,
metaDescription: undefined,
};
return {
base: process.env.BASE_URL ?? "/",
plugins: [viteEjsPlugin(strings), react(), svgr()],
assetsInclude: ["**/*.hex"],
define: {
"import.meta.env.VITE_APP_VERSION": JSON.stringify(
process.env.npm_package_version
),
},
build: {
target: "es2017",
rollupOptions: {
input: "index.html",
},
},
server: commonEnv.API_PROXY
? {
port: 5173,
proxy: {
"/api/v1": {
target: commonEnv.API_PROXY,
changeOrigin: true,
},
},
}
: undefined,
test: {
globals: true,
environment: "jsdom",
exclude: [...configDefaults.exclude, "**/e2e/**"],
poolOptions: {
threads: {
// threads disabled for now due to https://github.com/vitest-dev/vitest/issues/1982
singleThread: true,
},
},
},
resolve: {
alias: {
"theme-package": themePackageAlias,
},
},
};
});