-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
index.ts
76 lines (66 loc) · 2.08 KB
/
index.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
import { fileURLToPath } from 'node:url';
import { type PreactPluginOptions as VitePreactPluginOptions, preact } from '@preact/preset-vite';
import type { AstroIntegration, AstroRenderer, ContainerRenderer, ViteUserConfig } from 'astro';
const babelCwd = new URL('../', import.meta.url);
function getRenderer(development: boolean): AstroRenderer {
return {
name: '@astrojs/preact',
clientEntrypoint: development ? '@astrojs/preact/client-dev.js' : '@astrojs/preact/client.js',
serverEntrypoint: '@astrojs/preact/server.js',
};
}
export function getContainerRenderer(): ContainerRenderer {
return {
name: '@astrojs/preact',
serverEntrypoint: '@astrojs/preact/server.js',
};
}
export interface Options extends Pick<VitePreactPluginOptions, 'include' | 'exclude'> {
compat?: boolean;
devtools?: boolean;
}
export default function ({ include, exclude, compat, devtools }: Options = {}): AstroIntegration {
return {
name: '@astrojs/preact',
hooks: {
'astro:config:setup': ({ addRenderer, updateConfig, command, injectScript }) => {
const preactPlugin = preact({
reactAliasesEnabled: compat ?? false,
include,
exclude,
babel: {
cwd: fileURLToPath(babelCwd),
},
});
const viteConfig: ViteUserConfig = {
optimizeDeps: {
include: ['@astrojs/preact/client.js', 'preact', 'preact/jsx-runtime'],
exclude: ['@astrojs/preact/server.js'],
},
};
if (compat) {
viteConfig.optimizeDeps!.include!.push(
'preact/compat',
'preact/test-utils',
'preact/compat/jsx-runtime',
);
viteConfig.resolve = {
dedupe: ['preact/compat', 'preact'],
};
// noExternal React entrypoints to be bundled, resolved, and aliased by Vite
viteConfig.ssr = {
noExternal: ['react', 'react-dom', 'react-dom/test-utils', 'react/jsx-runtime'],
};
}
viteConfig.plugins = [preactPlugin];
addRenderer(getRenderer(command === 'dev'));
updateConfig({
vite: viteConfig,
});
if (command === 'dev' && devtools) {
injectScript('page', 'import "preact/debug";');
}
},
},
};
}