-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvite.config.ts
More file actions
48 lines (45 loc) · 1.76 KB
/
Copy pathvite.config.ts
File metadata and controls
48 lines (45 loc) · 1.76 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
import { sveltekit } from '@sveltejs/kit/vite';
import { svelte, vitePreprocess } from '@sveltejs/vite-plugin-svelte';
import tailwindcss from '@tailwindcss/vite';
import { loadEnv, defineConfig, type Plugin } from 'vite';
/**
* Workaround for https://github.com/sveltejs/kit/issues/12394.
* Rolldown's worker bundler skips SvelteKit's plugin chain, so `__sveltekit/environment`
* and `$env/*` are unresolvable. Shims them with safe defaults (workers are always
* browser-side, never auth/env-aware).
*/
function skitWorkerShim(publicEnv: Record<string, string>): Plugin {
const publicEnvExports =
Object.entries(publicEnv)
.filter(([k]) => k.startsWith('PUBLIC_'))
.map(([k, v]) => `export const ${k} = ${JSON.stringify(v)};`)
.join('\n') || `export {};`;
return {
name: 'sveltekit-worker-shim',
resolveId(id) {
if (id === '__sveltekit/environment' || id.startsWith('$env/')) return `\0sveltekit-shim:${id}`;
},
load(id) {
if (id === '\0sveltekit-shim:__sveltekit/environment') {
return `export const browser = true; export const building = false; export const dev = false; export const version = '';`;
}
if (id === '\0sveltekit-shim:$env/static/public' || id === '\0sveltekit-shim:$env/dynamic/public') {
return publicEnvExports;
}
if (id.startsWith('\0sveltekit-shim:$env/')) {
return `export {};`;
}
},
};
}
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), '');
return {
plugins: [tailwindcss(), sveltekit()],
worker: {
// `svelte()` is also needed here so .svelte files pulled in transitively (e.g. icon
// components imported by data files) compile instead of being parsed as plain JS.
plugins: () => [skitWorkerShim(env), svelte({ preprocess: vitePreprocess() })],
},
};
});