2022-11-11 06:22:43 +00:00
|
|
|
import type { AstroConfig, AstroIntegration, AstroRenderer } from 'astro';
|
2022-10-28 15:30:54 +00:00
|
|
|
import { getSolidPkgsConfig } from './dependencies.js';
|
2022-03-18 22:35:45 +00:00
|
|
|
|
|
|
|
function getRenderer(): AstroRenderer {
|
|
|
|
return {
|
|
|
|
name: '@astrojs/solid-js',
|
|
|
|
clientEntrypoint: '@astrojs/solid-js/client.js',
|
|
|
|
serverEntrypoint: '@astrojs/solid-js/server.js',
|
|
|
|
jsxImportSource: 'solid-js',
|
|
|
|
jsxTransformOptions: async ({ ssr }) => {
|
|
|
|
// @ts-expect-error types not found
|
|
|
|
const [{ default: solid }] = await Promise.all([import('babel-preset-solid')]);
|
|
|
|
const options = {
|
|
|
|
presets: [solid({}, { generate: ssr ? 'ssr' : 'dom', hydratable: true })],
|
|
|
|
plugins: [],
|
2022-06-02 17:35:03 +00:00
|
|
|
// Otherwise, babel will try to consume the source map generated by esbuild
|
2022-06-02 17:35:51 +00:00
|
|
|
// This causes unexpected issues with newline characters: https://github.com/withastro/astro/issues/3371
|
2022-06-02 17:35:03 +00:00
|
|
|
// Note "vite-plugin-solid" does the same: https://github.com/solidjs/vite-plugin-solid/blob/master/src/index.ts#L344-L345
|
|
|
|
inputSourceMap: false as any,
|
2022-03-18 22:35:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return options;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-11-11 06:22:43 +00:00
|
|
|
async function getViteConfiguration(isDev: boolean, astroConfig: AstroConfig) {
|
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'];
|
2022-11-11 06:22:43 +00:00
|
|
|
const solidPkgsConfig = await getSolidPkgsConfig(!isDev, astroConfig);
|
2022-03-18 22:35:45 +00:00
|
|
|
return {
|
|
|
|
/**
|
|
|
|
* We only need esbuild on .ts or .js files.
|
|
|
|
* .tsx & .jsx files are handled by us
|
|
|
|
*/
|
|
|
|
esbuild: { include: /\.ts$/ },
|
|
|
|
resolve: {
|
|
|
|
conditions: ['solid', ...(isDev ? ['development'] : [])],
|
|
|
|
dedupe: nestedDeps,
|
|
|
|
alias: [{ find: /^solid-refresh$/, replacement: '/@solid-refresh' }],
|
|
|
|
},
|
|
|
|
optimizeDeps: {
|
2022-10-28 15:30:54 +00:00
|
|
|
include: [...nestedDeps, ...solidPkgsConfig.optimizeDeps.include],
|
|
|
|
exclude: ['@astrojs/solid-js/server.js', ...solidPkgsConfig.optimizeDeps.exclude],
|
2022-03-18 22:35:45 +00:00
|
|
|
},
|
|
|
|
ssr: {
|
2022-10-28 15:30:54 +00:00
|
|
|
external: ['babel-preset-solid', ...solidPkgsConfig.ssr.external],
|
2023-05-02 17:23:28 +00:00
|
|
|
noExternal: [...solidPkgsConfig.ssr.noExternal],
|
2022-03-18 22:35:45 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function (): AstroIntegration {
|
|
|
|
return {
|
|
|
|
name: '@astrojs/solid-js',
|
|
|
|
hooks: {
|
2022-10-28 15:30:54 +00:00
|
|
|
'astro:config:setup': async ({ command, addRenderer, updateConfig, config }) => {
|
2022-03-18 22:35:45 +00:00
|
|
|
addRenderer(getRenderer());
|
2022-11-11 06:22:43 +00:00
|
|
|
updateConfig({ vite: await getViteConfiguration(command === 'dev', config) });
|
2022-03-18 22:35:45 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|