-
Notifications
You must be signed in to change notification settings - Fork 0
/
vitePluginThrelteSsrStub.ts
62 lines (51 loc) · 1.42 KB
/
vitePluginThrelteSsrStub.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
/* eslint-disable @typescript-eslint/no-unsafe-argument */
import type { Plugin } from 'vite';
const js = (strings: TemplateStringsArray, ...values: any[]) =>
String.raw({ raw: strings }, ...values);
const SHIM_SSR_GENERIC = js`
import { create_ssr_component } from 'svelte/internal';
export default create_ssr_component(
(_, __, ___, slots) => slots.default?.({}) ?? '',
);
`.trim();
const SHIM_SSR_T = js`
import { create_ssr_component } from 'svelte/internal';
const NoopComponent = create_ssr_component(
(_, __, ___, slots) => slots.default?.({}) ?? '',
);
export const extend = () => {};
export const T = new Proxy(class {}, {
construct: () => new NoopComponent(),
get: () => NoopComponent,
});
`.trim();
export const vitePluginThrelteSsrStub = () =>
({
name: 'threlte ssr stub',
resolveId(id) {
if (id === 'use-unsafe-threlte') return `\0${id}`;
},
load(id) {
if (id !== '\0use-unsafe-threlte') return;
return {
code: '',
};
},
transform(code, id, { ssr = false } = {}) {
if (!ssr) return;
if (
id.endsWith('.svelte') &&
!id.includes('node_modules') &&
code.includes('@threlte') &&
!code.includes('use-unsafe-threlte')
)
return {
code: SHIM_SSR_GENERIC,
};
if (
id.includes('node_modules/@threlte/core') &&
id.endsWith('/T.js')
)
return { code: SHIM_SSR_T };
},
}) satisfies Plugin;