Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add preact/compat support to @astrojs/preact #3712

Merged
merged 17 commits into from
Jun 29, 2022
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 37 additions & 5 deletions packages/integrations/preact/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AstroIntegration } from 'astro';
import { AstroIntegration, AstroRenderer, ViteUserConfig } from 'astro';

function getRenderer() {
function getRenderer(): AstroRenderer {
return {
name: '@astrojs/preact',
clientEntrypoint: '@astrojs/preact/client.js',
Expand All @@ -18,8 +18,26 @@ function getRenderer() {
};
}

function getViteConfiguration() {
function getCompatRenderer(): AstroRenderer {
return {
name: '@astrojs/preact/compat',
clientEntrypoint: '@astrojs/preact/client.js',
serverEntrypoint: '@astrojs/preact/server.js',
natemoo-re marked this conversation as resolved.
Show resolved Hide resolved
jsxImportSource: 'react',
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/compat' })],
};
},
};
}

function getViteConfiguration(compat?: boolean): ViteUserConfig {
const viteConfig: ViteUserConfig = {
optimizeDeps: {
include: [
'@astrojs/preact/client.js',
Expand All @@ -33,16 +51,30 @@ function getViteConfiguration() {
external: ['preact-render-to-string'],
},
};

if (compat) {
viteConfig.optimizeDeps!.include!.push('preact/compat');
viteConfig.resolve = {
alias: {
react: 'preact/compat',
'react-dom': 'preact/compat',
},
dedupe: ['react', 'react-dom'],
};
}

return viteConfig
}

export default function (): AstroIntegration {
export default function ({ compat }: { compat?: boolean } = {}): AstroIntegration {
natemoo-re marked this conversation as resolved.
Show resolved Hide resolved
return {
name: '@astrojs/preact',
hooks: {
'astro:config:setup': ({ addRenderer, updateConfig }) => {
addRenderer(getRenderer());
if (compat) addRenderer(getCompatRenderer());
updateConfig({
vite: getViteConfiguration(),
vite: getViteConfiguration(compat),
});
},
},
Expand Down