2022-03-18 17:35:45 -05:00
|
|
|
import type { AstroIntegration, AstroRenderer } from 'astro';
|
|
|
|
import { svelte } from '@sveltejs/vite-plugin-svelte';
|
|
|
|
import preprocess from 'svelte-preprocess';
|
2022-04-22 12:59:20 -05:00
|
|
|
import type { Options } from '@sveltejs/vite-plugin-svelte';
|
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',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-04-22 12:59:20 -05:00
|
|
|
function getViteConfiguration(isDev: boolean, options?: Options | OptionsCallback) {
|
|
|
|
const defaultOptions = {
|
|
|
|
emitCss: true,
|
|
|
|
compilerOptions: { dev: isDev, hydratable: true },
|
|
|
|
preprocess: [
|
|
|
|
preprocess({
|
|
|
|
less: true,
|
|
|
|
sass: { renderSync: true },
|
|
|
|
scss: { renderSync: true },
|
|
|
|
stylus: true,
|
|
|
|
typescript: true,
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
// Ignore default preprocessor if the user provided their own
|
|
|
|
preprocess: options.preprocess ?? defaultOptions.preprocess,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-03-18 17:35:45 -05:00
|
|
|
return {
|
|
|
|
optimizeDeps: {
|
|
|
|
include: ['@astrojs/svelte/client.js', 'svelte', 'svelte/internal'],
|
|
|
|
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
|
|
|
|
'astro:config:setup': ({ command, updateConfig, addRenderer }) => {
|
|
|
|
addRenderer(getRenderer());
|
2022-04-22 12:59:20 -05:00
|
|
|
updateConfig({ vite: getViteConfiguration(command === 'dev', options) });
|
2022-03-18 17:35:45 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|