-
Notifications
You must be signed in to change notification settings - Fork 37
/
vite.config.ts
202 lines (197 loc) · 6.28 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import react from "@vitejs/plugin-react";
import ejs from "ejs";
import fs from "node:fs";
import path from "node:path";
import {
IndexHtmlTransformContext,
IndexHtmlTransformResult,
loadEnv,
Plugin,
} from "vite";
import { VitePWA } from "vite-plugin-pwa";
import svgr from "vite-plugin-svgr";
import { configDefaults, defineConfig, UserConfig } from "vitest/config";
// Support optionally pulling in external branding if the module is installed.
const theme = "@microbit-foundation/python-editor-v3-microbit";
const external = `node_modules/${theme}`;
const internal = "src/deployment/default";
const featurePwa = process.env.FEATURE_PWA === "true";
const pwaCacheId =
// v3 vs beta should have distinct caches
// for the moment we do this for all review stages too but likely that's too much over time
process.env.STAGE === "PRODUCTION" ||
process.env.STAGE === "STAGING" ||
process.env.STAGE === "REVIEW"
? process.env.BASE_URL.replaceAll("/", "")
: undefined;
// There are third-party options but seems better to just depend on ejs.
const viteEjsPlugin = ({ data }: { data: ejs.Data }): Plugin => ({
name: "ejs",
transformIndexHtml: {
order: "pre",
handler: (
html: string,
_ctx: IndexHtmlTransformContext
): IndexHtmlTransformResult => ejs.render(html, data),
},
});
// Removes webmanifest link tag from output index.html file.
// We readd this client side if PWA features are enabled via a feature flag.
// When that feature flag goes this plugin can be removed.
const viteRemoveManifestPlugin = (): Plugin => ({
name: "Manifest",
enforce: "post",
transformIndexHtml: {
order: "post",
handler: (
html: string,
_ctx: IndexHtmlTransformContext
): IndexHtmlTransformResult => {
const updated = html.replace(
`<link rel="manifest" href="${
process.env.BASE_URL ?? "/"
}manifest.webmanifest">`,
""
);
if (featurePwa && updated == html) {
throw new Error("Failed to remove web manifest");
}
return updated;
},
},
});
export default defineConfig(({ mode }) => {
process.env = { ...process.env, ...loadEnv(mode, process.cwd()) };
const unitTest: UserConfig["test"] = {
globals: true,
exclude: [...configDefaults.exclude, "**/e2e/**"],
environment: "jsdom",
setupFiles: "./src/setupTests.ts",
mockReset: true,
};
const config: UserConfig = {
base: process.env.BASE_URL ?? "/",
build: {
outDir: "build",
sourcemap: true,
},
server: {
port: 3000,
},
assetsInclude: ["**/*.hex"],
plugins: [
viteEjsPlugin({
data: loadEnv(mode, process.cwd(), "VITE_"),
}),
react(),
svgr(),
VitePWA({
disable: !featurePwa,
registerType: "autoUpdate",
// Remove the trailing slash so we can serve from e.g. /v/3 not just /v/3/
// This requires the corresponding service-worker-allowed header to be set.
// URLs are prefix matched so it's a compromise solution that could affect other
// paths sharing the same prefix
scope: process.env.BASE_URL?.replace(/\/$/, ""),
workbox: {
cacheId: pwaCacheId,
// Only precache language assets for the fallback language.
// Cache other languages at runtime.
globIgnores: [
"**/{typeshed.!(en*).js,pyright-locale-!(en*).js,search.worker.!(en*).js,ui.!(en*).js}",
],
maximumFileSizeToCacheInBytes: 3097152,
globPatterns: ["**/*.{js,css,html,ico,png,svg,gif,hex}"],
runtimeCaching: [
{
urlPattern: new RegExp(
`^https://${process.env.VITE_SANITY_PROJECT}.apicdn.sanity.io/.*`
),
handler: "NetworkFirst",
options: {
cacheName: `${pwaCacheId}-sanity-content-cache`,
expiration: {
maxEntries: 40,
},
cacheableResponse: {
statuses: [0, 200],
},
},
},
{
urlPattern: new RegExp(
`^https://cdn.sanity.io/images/${process.env.VITE_SANITY_PROJECT}/${process.env.VITE_SANITY_DATASET}/.*`
),
handler: "CacheFirst",
options: {
cacheName: `${pwaCacheId}-sanity-images-cache`,
expiration: {
maxEntries: 100,
},
cacheableResponse: {
statuses: [0, 200],
},
},
},
{
urlPattern: /^https:\/\/fonts.microbit.org\/.*/,
handler: "CacheFirst",
options: {
cacheName: `${pwaCacheId}-fonts-cache`,
expiration: {
maxEntries: 10,
},
cacheableResponse: {
statuses: [0, 200],
},
},
},
{
urlPattern:
/.*(?:pyright-locale|search\.worker|typeshed|ui\.).*\.js/,
handler: "CacheFirst",
options: {
cacheName: `${pwaCacheId}-lang-cache`,
expiration: {
maxEntries: 40,
},
cacheableResponse: {
statuses: [0, 200],
},
},
},
],
},
manifest: {
name: "micro:bit Python Editor",
short_name: "micro:bit Python Editor",
description:
"A Python Editor for the BBC micro:bit, built by the Micro:bit Educational Foundation and the global Python Community.",
theme_color: "#6c4bc1",
icons: [
{
src: `${process.env.BASE_URL ?? "/"}logo512.png`,
sizes: "512x512",
type: "image/png",
},
{
src: `${process.env.BASE_URL ?? "/"}logo192.png`,
sizes: "192x192",
type: "image/png",
},
],
},
}),
viteRemoveManifestPlugin(),
],
test: unitTest,
resolve: {
alias: {
"theme-package": fs.existsSync(external)
? theme
: path.resolve(__dirname, internal),
},
},
};
return config;
});