2022-03-18 22:35:45 +00:00
|
|
|
import { AstroIntegration } from 'astro';
|
|
|
|
|
|
|
|
function getRenderer() {
|
|
|
|
return {
|
|
|
|
name: '@astrojs/preact',
|
2022-03-24 16:28:50 +00:00
|
|
|
clientEntrypoint: '@astrojs/preact/client.js',
|
|
|
|
serverEntrypoint: '@astrojs/preact/server.js',
|
2022-03-18 22:35:45 +00:00
|
|
|
jsxImportSource: 'preact',
|
|
|
|
jsxTransformOptions: async () => {
|
|
|
|
const {
|
|
|
|
default: { default: jsx },
|
|
|
|
// @ts-expect-error types not found
|
|
|
|
} = await import('@babel/plugin-transform-react-jsx');
|
|
|
|
return {
|
|
|
|
plugins: [jsx({}, { runtime: 'automatic', importSource: 'preact' })],
|
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function getViteConfiguration() {
|
|
|
|
return {
|
|
|
|
optimizeDeps: {
|
2022-03-24 16:28:50 +00:00
|
|
|
include: [
|
|
|
|
'@astrojs/preact/client.js',
|
|
|
|
'preact',
|
|
|
|
'preact/jsx-runtime',
|
|
|
|
'preact-render-to-string',
|
|
|
|
],
|
|
|
|
exclude: ['@astrojs/preact/server.js'],
|
2022-03-18 22:35:45 +00:00
|
|
|
},
|
|
|
|
ssr: {
|
|
|
|
external: ['preact-render-to-string'],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function (): AstroIntegration {
|
|
|
|
return {
|
|
|
|
name: '@astrojs/preact',
|
|
|
|
hooks: {
|
2022-04-21 20:13:09 +00:00
|
|
|
'astro:config:setup': ({ addRenderer, updateConfig }) => {
|
2022-03-18 22:35:45 +00:00
|
|
|
addRenderer(getRenderer());
|
2022-04-21 20:13:09 +00:00
|
|
|
updateConfig({
|
2022-03-18 22:35:45 +00:00
|
|
|
vite: getViteConfiguration(),
|
2022-04-21 20:13:54 +00:00
|
|
|
});
|
2022-03-18 22:35:45 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|