-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathvite.config.ts
More file actions
245 lines (239 loc) · 7.98 KB
/
Copy pathvite.config.ts
File metadata and controls
245 lines (239 loc) · 7.98 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
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";
// Browser-support floor (esbuild/lightningcss target syntax) for JS
// (build.target) and CSS (build.cssTarget). Keep in sync with the
// "browserslist" field in package.json, which is documentation — this is what
// the build actually uses.
//
// safari14.1/ios14.5 rather than 14/14 is deliberate: flexbox `gap` landed in
// exactly those versions, and the Panda stack patterns this app is built on
// depend on it. See postcss.config.cjs for the two Safari <15.4 fixes that go
// with this floor.
const BUILD_TARGETS = [
"chrome90",
"edge90",
"firefox88",
"safari14.1",
"ios14.5",
];
// 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,
alias: {
// vscode-jsonrpc@9's "./browser" export only declares a "browser"
// condition. The app build applies it, but vitest resolves in node mode
// and can't, so point it at the browser entry (as the build does).
"vscode-jsonrpc/browser": path.resolve(
__dirname,
"node_modules/vscode-jsonrpc/lib/browser/main.js"
),
},
};
const config: UserConfig = {
base: process.env.BASE_URL ?? "/",
build: {
outDir: "build",
sourcemap: true,
target: BUILD_TARGETS,
cssTarget: BUILD_TARGETS,
},
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),
// Resolve Panda's generated helpers for all importers, including
// @microbit/ui's source (consumed from node_modules). Mirrors the
// tsconfig `paths` entry.
"styled-system": path.resolve(__dirname, "styled-system"),
},
// @microbit/ui is consumed as source via a file: symlink, so its
// `import "react"` etc. would otherwise resolve to the ui monorepo's own
// copies — two Reacts → invalid-hook "useContext of null" crashes. Force
// a single copy (the app's) for React and the react-aria stack.
dedupe: [
"react",
"react-dom",
"react-aria-components",
"react-aria",
"react-stately",
],
},
};
return config;
});