2023-08-11 14:05:02 +00:00
|
|
|
import type { AstroIntegration, AstroRenderer } from 'astro';
|
|
|
|
import solid, { type Options as ViteSolidPluginOptions } from 'vite-plugin-solid';
|
2022-03-18 22:35:45 +00:00
|
|
|
|
2023-08-11 15:14:20 +00:00
|
|
|
async function getViteConfiguration(isDev: boolean, { include, exclude }: Options = {}) {
|
2022-03-18 22:35:45 +00:00
|
|
|
// https://github.com/solidjs/vite-plugin-solid
|
2022-12-19 11:00:00 +00:00
|
|
|
// We inject the dev mode only if the user explicitly wants it or if we are in dev (serve) mode
|
2022-03-18 22:35:45 +00:00
|
|
|
const nestedDeps = ['solid-js', 'solid-js/web', 'solid-js/store', 'solid-js/html', 'solid-js/h'];
|
|
|
|
return {
|
|
|
|
resolve: {
|
|
|
|
conditions: ['solid', ...(isDev ? ['development'] : [])],
|
|
|
|
dedupe: nestedDeps,
|
|
|
|
alias: [{ find: /^solid-refresh$/, replacement: '/@solid-refresh' }],
|
|
|
|
},
|
|
|
|
optimizeDeps: {
|
2023-08-11 14:05:02 +00:00
|
|
|
include: [...nestedDeps],
|
|
|
|
exclude: ['@astrojs/solid-js/server.js'],
|
2022-03-18 22:35:45 +00:00
|
|
|
},
|
2023-08-11 14:05:02 +00:00
|
|
|
plugins: [
|
|
|
|
solid({ include, exclude, dev: isDev, ssr: true }),
|
|
|
|
{
|
|
|
|
name: '@astrojs/solid:config-overrides',
|
|
|
|
enforce: 'post',
|
|
|
|
config() {
|
|
|
|
return {
|
|
|
|
esbuild: {
|
|
|
|
// To support using alongside other JSX frameworks, still let
|
|
|
|
// esbuild compile stuff. Solid goes first anyways.
|
2023-08-11 15:14:20 +00:00
|
|
|
include: /\.(m?ts|[jt]sx)$/,
|
2023-08-11 14:05:02 +00:00
|
|
|
},
|
2023-08-11 15:14:20 +00:00
|
|
|
};
|
2023-08-11 14:05:02 +00:00
|
|
|
},
|
2023-08-11 15:14:20 +00:00
|
|
|
},
|
2023-08-11 14:05:02 +00:00
|
|
|
],
|
2022-03-18 22:35:45 +00:00
|
|
|
ssr: {
|
2023-08-11 14:05:02 +00:00
|
|
|
external: ['babel-preset-solid'],
|
2022-03-18 22:35:45 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-08-11 14:05:02 +00:00
|
|
|
function getRenderer(): AstroRenderer {
|
|
|
|
return {
|
|
|
|
name: '@astrojs/solid-js',
|
|
|
|
clientEntrypoint: '@astrojs/solid-js/client.js',
|
|
|
|
serverEntrypoint: '@astrojs/solid-js/server.js',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export type Options = Pick<ViteSolidPluginOptions, 'include' | 'exclude'>;
|
|
|
|
|
|
|
|
export default function (opts: Options = {}): AstroIntegration {
|
2022-03-18 22:35:45 +00:00
|
|
|
return {
|
|
|
|
name: '@astrojs/solid-js',
|
|
|
|
hooks: {
|
2023-08-11 14:05:02 +00:00
|
|
|
'astro:config:setup': async ({ command, addRenderer, updateConfig }) => {
|
2022-03-18 22:35:45 +00:00
|
|
|
addRenderer(getRenderer());
|
2023-08-11 14:05:02 +00:00
|
|
|
updateConfig({
|
|
|
|
vite: await getViteConfiguration(command === 'dev', opts),
|
|
|
|
});
|
2022-03-18 22:35:45 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|