Repository navigation
Expand file tree
/
Copy pathvite.config.ts
More file actions
124 lines (117 loc) · 4.23 KB
/
Copy pathvite.config.ts
File metadata and controls
124 lines (117 loc) · 4.23 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
import { rmSync } from 'node:fs';
import adapter from '@sveltejs/adapter-cloudflare';
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
import tailwindcss from '@tailwindcss/vite';
import type { Plugin } from 'vite';
import { defineConfig } from 'vitest/config';
import { sveltekit } from '@sveltejs/kit/vite';
import { ORIGIN } from './src/lib/origin.ts';
// Matches the Open Collective and Google OAuth callback URLs registered for local development.
const DEV_PORT = 5317;
/** Warns when Vite falls back to another port, since the OAuth callbacks only work on {@link DEV_PORT}. */
function warnOnPortFallback(): Plugin {
return {
name: 'warn-on-port-fallback',
apply: 'serve',
configureServer(server) {
server.httpServer?.once('listening', () => {
const address = server.httpServer?.address();
if (typeof address === 'object' && address && address.port !== DEV_PORT) {
server.config.logger.warn(
`Port ${DEV_PORT} is in use, so the dev server started on ${address.port}. Connecting Open Collective and signing in with Google will fail because their OAuth callbacks are registered for http://localhost:${DEV_PORT}.`
);
}
});
}
};
}
/**
* Loads the Node builds of the share image's renderers in dev. The adapter resolves packages as
* workerd does, but dev runs the server in Node, which can't load the Worker builds' WebAssembly.
*/
function nodeImageRenderers(): Plugin {
const renderers = new Set(['@cf-wasm/resvg', '@cf-wasm/satori']);
return {
name: 'node-image-renderers',
apply: 'serve',
enforce: 'pre',
resolveId(id, importer, options) {
if (this.environment.name !== 'ssr' || !renderers.has(id)) return;
return this.resolve(`${id}/node`, importer, { ...options, skipSelf: true });
}
};
}
/**
* Removes the redirect the adapter writes after a build, which makes `wrangler deploy` deploy its
* Worker in place of `worker.ts`, without the schedule or the KV namespace in `wrangler.jsonc`.
*/
function deployFromWranglerConfig(): Plugin {
return {
name: 'deploy-from-wrangler-config',
apply: 'build',
buildApp: {
order: 'post',
async handler(builder) {
rmSync(`${builder.config.root}/.wrangler/deploy/config.json`, { force: true });
}
}
};
}
export default defineConfig({
server: { port: DEV_PORT },
// Found only once the work log renders, which made the first visit bundle them and then reload the page.
optimizeDeps: { include: ['layerchart', 'layerchart/svg'] },
plugins: [
warnOnPortFallback(),
nodeImageRenderers(),
tailwindcss(),
sveltekit({
paths: { origin: ORIGIN },
preprocess: vitePreprocess(),
compilerOptions: { experimental: { async: true } },
experimental: { remoteFunctions: true },
// The page holds users GitHub token and work log, so ensure only this app's own scripts may run.
csp: {
directives: {
'default-src': ['self'],
'script-src': ['self'],
// Inline styles come from transitions and positioned popovers.
'style-src': ['self', 'unsafe-inline'],
'font-src': ['self'],
'img-src': ['self', 'data:', 'https://images.opencollective.com'],
// GitHub is asked from the browser, with your token.
'connect-src': ['self', 'https://api.github.com'],
'form-action': ['self'],
'base-uri': ['self'],
'object-src': ['none'],
// The invoice PDF, made in the browser, is previewed from a blob URL.
'frame-src': ['self', 'blob:'],
// Only this app may frame its pages, which its own PDF previews need: a blob takes on this policy.
'frame-ancestors': ['self']
}
},
// Built from its own config, as `wrangler.jsonc` deploys `worker.ts`, which wraps the build.
adapter: adapter({
config: 'wrangler.kit.jsonc',
platformProxy: { configPath: 'wrangler.jsonc' }
})
}),
// After SvelteKit, whose adapter reads the redirect while it builds.
deployFromWranglerConfig()
],
test: {
expect: { requireAssertions: true },
projects: [
{
extends: './vite.config.ts',
test: {
name: 'server',
// TODO: use workerd once it supports vitest 5 https://developers.cloudflare.com/workers/testing/vitest-integration/
environment: 'node',
include: ['src/**/*.{test,spec}.{js,ts}'],
exclude: ['src/**/*.svelte.{test,spec}.{js,ts}']
}
}
]
}
});