2023-09-13 16:49:22 +02:00
|
|
|
import { preact, type PreactPluginOptions as VitePreactPluginOptions } from '@preact/preset-vite';
|
2023-08-24 14:40:44 +00:00
|
|
|
import type { AstroIntegration, AstroRenderer, ViteUserConfig } from 'astro';
|
2023-08-11 10:05:02 -04:00
|
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
|
|
|
|
const babelCwd = new URL('../', import.meta.url);
|
2022-03-18 15:35:45 -07:00
|
|
|
|
2022-09-07 20:49:07 +02:00
|
|
|
function getRenderer(development: boolean): AstroRenderer {
|
2022-03-18 15:35:45 -07:00
|
|
|
return {
|
|
|
|
name: '@astrojs/preact',
|
2022-09-07 20:49:07 +02:00
|
|
|
clientEntrypoint: development ? '@astrojs/preact/client-dev.js' : '@astrojs/preact/client.js',
|
2022-03-24 12:28:50 -04:00
|
|
|
serverEntrypoint: '@astrojs/preact/server.js',
|
2022-03-18 15:35:45 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-08-11 08:14:20 -07:00
|
|
|
export type Options = Pick<VitePreactPluginOptions, 'include' | 'exclude'> & { compat?: boolean };
|
2023-08-11 10:05:02 -04:00
|
|
|
|
2023-08-11 08:14:20 -07:00
|
|
|
export default function ({ include, exclude, compat }: Options = {}): AstroIntegration {
|
2022-03-18 15:35:45 -07:00
|
|
|
return {
|
2022-06-29 17:42:47 +02:00
|
|
|
name: '@astrojs/preact',
|
2023-08-11 10:05:02 -04:00
|
|
|
hooks: {
|
|
|
|
'astro:config:setup': ({ addRenderer, updateConfig, command }) => {
|
|
|
|
const preactPlugin = preact({
|
|
|
|
include,
|
|
|
|
exclude,
|
|
|
|
babel: {
|
2023-08-11 08:14:20 -07:00
|
|
|
cwd: fileURLToPath(babelCwd),
|
|
|
|
},
|
2023-08-11 10:05:02 -04:00
|
|
|
});
|
2022-06-29 17:42:47 +02:00
|
|
|
|
2023-08-11 10:05:02 -04:00
|
|
|
const viteConfig: ViteUserConfig = {
|
|
|
|
optimizeDeps: {
|
|
|
|
include: ['@astrojs/preact/client.js', 'preact', 'preact/jsx-runtime'],
|
|
|
|
exclude: ['@astrojs/preact/server.js'],
|
|
|
|
},
|
|
|
|
};
|
2022-06-29 15:48:55 +00:00
|
|
|
|
2023-08-11 10:05:02 -04:00
|
|
|
// If not compat, delete the plugin that does it
|
2023-08-11 08:14:20 -07:00
|
|
|
if (!compat) {
|
|
|
|
const pIndex = preactPlugin.findIndex((p) => p.name == 'preact:config');
|
2023-08-11 10:05:02 -04:00
|
|
|
if (pIndex >= 0) {
|
|
|
|
preactPlugin.splice(pIndex, 1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
viteConfig.optimizeDeps!.include!.push(
|
|
|
|
'preact/compat',
|
|
|
|
'preact/test-utils',
|
2023-08-11 08:14:20 -07:00
|
|
|
'preact/compat/jsx-runtime'
|
2023-08-11 10:05:02 -04:00
|
|
|
);
|
|
|
|
viteConfig.resolve = {
|
2023-08-11 08:14:20 -07:00
|
|
|
alias: [{ find: 'react/jsx-runtime', replacement: 'preact/jsx-runtime' }],
|
2023-08-11 10:05:02 -04:00
|
|
|
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'],
|
|
|
|
};
|
|
|
|
}
|
2022-06-29 15:48:55 +00:00
|
|
|
|
2023-08-11 10:05:02 -04:00
|
|
|
viteConfig.plugins = [preactPlugin];
|
2022-03-18 15:35:45 -07:00
|
|
|
|
2023-08-11 10:05:02 -04:00
|
|
|
addRenderer(getRenderer(command === 'dev'));
|
2022-04-21 15:13:09 -05:00
|
|
|
updateConfig({
|
2023-08-11 10:05:02 -04:00
|
|
|
vite: viteConfig,
|
2022-04-21 20:13:54 +00:00
|
|
|
});
|
2022-03-18 15:35:45 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|