2022-06-06 11:49:53 -05:00
|
|
|
import type { Options } from '@sveltejs/vite-plugin-svelte';
|
2023-01-03 13:24:49 -05:00
|
|
|
import { svelte, vitePreprocess } from '@sveltejs/vite-plugin-svelte';
|
|
|
|
import type { AstroIntegration, AstroRenderer } from 'astro';
|
2023-01-19 08:13:40 -05:00
|
|
|
import { fileURLToPath } from 'url';
|
2023-01-19 08:15:53 -05:00
|
|
|
import type { UserConfig } from 'vite';
|
2022-03-18 17:35:45 -05:00
|
|
|
|
|
|
|
function getRenderer(): AstroRenderer {
|
|
|
|
return {
|
|
|
|
name: '@astrojs/svelte',
|
|
|
|
clientEntrypoint: '@astrojs/svelte/client.js',
|
|
|
|
serverEntrypoint: '@astrojs/svelte/server.js',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-01-19 08:13:40 -05:00
|
|
|
async function svelteConfigHasPreprocess(root: URL) {
|
|
|
|
const svelteConfigFiles = ['./svelte.config.js', './svelte.config.cjs', './svelte.config.mjs'];
|
|
|
|
for (const file of svelteConfigFiles) {
|
|
|
|
const filePath = fileURLToPath(new URL(file, root));
|
|
|
|
try {
|
|
|
|
const config = (await import(filePath)).default;
|
|
|
|
return !!config.preprocess;
|
|
|
|
} catch {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-22 18:52:32 -05:00
|
|
|
type ViteConfigurationArgs = {
|
|
|
|
isDev: boolean;
|
|
|
|
options?: Options | OptionsCallback;
|
2023-01-19 08:13:40 -05:00
|
|
|
root: URL;
|
2022-06-22 18:55:00 -05:00
|
|
|
};
|
2022-06-22 18:52:32 -05:00
|
|
|
|
2023-01-19 08:15:53 -05:00
|
|
|
async function getViteConfiguration({
|
|
|
|
options,
|
|
|
|
isDev,
|
|
|
|
root,
|
|
|
|
}: ViteConfigurationArgs): Promise<UserConfig> {
|
2022-06-22 18:52:32 -05:00
|
|
|
const defaultOptions: Partial<Options> = {
|
2022-04-22 12:59:20 -05:00
|
|
|
emitCss: true,
|
|
|
|
compilerOptions: { dev: isDev, hydratable: true },
|
|
|
|
};
|
|
|
|
|
2022-07-26 13:04:26 -05:00
|
|
|
// Disable hot mode during the build
|
2022-07-26 13:06:03 -05:00
|
|
|
if (!isDev) {
|
2022-07-26 13:04:26 -05:00
|
|
|
defaultOptions.hot = false;
|
|
|
|
}
|
|
|
|
|
2022-04-22 12:59:20 -05:00
|
|
|
let resolvedOptions: Partial<Options>;
|
|
|
|
|
|
|
|
if (!options) {
|
|
|
|
resolvedOptions = defaultOptions;
|
|
|
|
} else if (typeof options === 'function') {
|
|
|
|
resolvedOptions = options(defaultOptions);
|
|
|
|
} else {
|
|
|
|
resolvedOptions = {
|
|
|
|
...options,
|
|
|
|
...defaultOptions,
|
|
|
|
compilerOptions: {
|
|
|
|
...options.compilerOptions,
|
|
|
|
// Always use dev and hydratable from defaults
|
|
|
|
...defaultOptions.compilerOptions,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-01-19 08:13:40 -05:00
|
|
|
if (!resolvedOptions.preprocess && !(await svelteConfigHasPreprocess(root))) {
|
|
|
|
resolvedOptions.preprocess = vitePreprocess();
|
|
|
|
}
|
|
|
|
|
2022-03-18 17:35:45 -05:00
|
|
|
return {
|
|
|
|
optimizeDeps: {
|
2023-01-03 13:24:49 -05:00
|
|
|
include: ['@astrojs/svelte/client.js'],
|
2022-03-18 17:35:45 -05:00
|
|
|
exclude: ['@astrojs/svelte/server.js'],
|
|
|
|
},
|
2022-04-22 13:00:08 -05:00
|
|
|
plugins: [svelte(resolvedOptions)],
|
2022-03-18 17:35:45 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-04-22 12:59:20 -05:00
|
|
|
type OptionsCallback = (defaultOptions: Options) => Options;
|
|
|
|
export default function (options?: Options | OptionsCallback): AstroIntegration {
|
2022-03-18 17:35:45 -05:00
|
|
|
return {
|
|
|
|
name: '@astrojs/svelte',
|
|
|
|
hooks: {
|
|
|
|
// Anything that gets returned here is merged into Astro Config
|
2023-01-19 08:13:40 -05:00
|
|
|
'astro:config:setup': async ({ command, updateConfig, addRenderer, config }) => {
|
2022-03-18 17:35:45 -05:00
|
|
|
addRenderer(getRenderer());
|
2022-06-22 18:52:32 -05:00
|
|
|
updateConfig({
|
2023-01-19 08:13:40 -05:00
|
|
|
vite: await getViteConfiguration({
|
2022-06-22 18:52:32 -05:00
|
|
|
options,
|
|
|
|
isDev: command === 'dev',
|
2023-01-19 08:13:40 -05:00
|
|
|
root: config.root,
|
2022-06-22 18:55:00 -05:00
|
|
|
}),
|
2022-06-22 18:52:32 -05:00
|
|
|
});
|
2022-03-18 17:35:45 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2023-01-19 08:13:40 -05:00
|
|
|
|
|
|
|
export { vitePreprocess };
|