2022-06-06 16:49:53 +00:00
|
|
|
import type { Options } from '@sveltejs/vite-plugin-svelte';
|
2022-03-18 22:35:45 +00:00
|
|
|
import { svelte } from '@sveltejs/vite-plugin-svelte';
|
2022-06-22 23:55:00 +00:00
|
|
|
import type { AstroConfig, AstroIntegration, AstroRenderer } from 'astro';
|
2022-03-18 22:35:45 +00:00
|
|
|
import preprocess from 'svelte-preprocess';
|
2022-06-22 23:55:00 +00:00
|
|
|
import type { UserConfig } from 'vite';
|
2022-03-18 22:35:45 +00:00
|
|
|
|
|
|
|
function getRenderer(): AstroRenderer {
|
|
|
|
return {
|
|
|
|
name: '@astrojs/svelte',
|
|
|
|
clientEntrypoint: '@astrojs/svelte/client.js',
|
|
|
|
serverEntrypoint: '@astrojs/svelte/server.js',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-06-22 23:52:32 +00:00
|
|
|
type ViteConfigurationArgs = {
|
|
|
|
isDev: boolean;
|
|
|
|
options?: Options | OptionsCallback;
|
|
|
|
postcssConfig: AstroConfig['style']['postcss'];
|
2022-06-22 23:55:00 +00:00
|
|
|
};
|
2022-06-22 23:52:32 +00:00
|
|
|
|
2022-06-22 23:55:00 +00:00
|
|
|
function getViteConfiguration({
|
|
|
|
options,
|
|
|
|
postcssConfig,
|
|
|
|
isDev,
|
|
|
|
}: ViteConfigurationArgs): UserConfig {
|
2022-06-22 23:52:32 +00:00
|
|
|
const defaultOptions: Partial<Options> = {
|
2022-04-22 17:59:20 +00:00
|
|
|
emitCss: true,
|
|
|
|
compilerOptions: { dev: isDev, hydratable: true },
|
|
|
|
preprocess: [
|
|
|
|
preprocess({
|
|
|
|
less: true,
|
2022-06-22 23:52:32 +00:00
|
|
|
postcss: postcssConfig,
|
2022-04-22 17:59:20 +00:00
|
|
|
sass: { renderSync: true },
|
|
|
|
scss: { renderSync: true },
|
|
|
|
stylus: true,
|
|
|
|
typescript: true,
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2022-07-26 18:04:26 +00:00
|
|
|
// Disable hot mode during the build
|
|
|
|
if(!isDev) {
|
|
|
|
defaultOptions.hot = false;
|
|
|
|
}
|
|
|
|
|
2022-04-22 17:59:20 +00: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,
|
|
|
|
},
|
|
|
|
// Ignore default preprocessor if the user provided their own
|
|
|
|
preprocess: options.preprocess ?? defaultOptions.preprocess,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-03-18 22:35:45 +00:00
|
|
|
return {
|
|
|
|
optimizeDeps: {
|
|
|
|
include: ['@astrojs/svelte/client.js', 'svelte', 'svelte/internal'],
|
|
|
|
exclude: ['@astrojs/svelte/server.js'],
|
|
|
|
},
|
2022-04-22 18:00:08 +00:00
|
|
|
plugins: [svelte(resolvedOptions)],
|
2022-03-18 22:35:45 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-04-22 17:59:20 +00:00
|
|
|
type OptionsCallback = (defaultOptions: Options) => Options;
|
|
|
|
export default function (options?: Options | OptionsCallback): AstroIntegration {
|
2022-03-18 22:35:45 +00:00
|
|
|
return {
|
|
|
|
name: '@astrojs/svelte',
|
|
|
|
hooks: {
|
|
|
|
// Anything that gets returned here is merged into Astro Config
|
2022-06-22 23:52:32 +00:00
|
|
|
'astro:config:setup': ({ command, updateConfig, addRenderer, config }) => {
|
2022-03-18 22:35:45 +00:00
|
|
|
addRenderer(getRenderer());
|
2022-06-22 23:52:32 +00:00
|
|
|
updateConfig({
|
|
|
|
vite: getViteConfiguration({
|
|
|
|
options,
|
|
|
|
isDev: command === 'dev',
|
|
|
|
postcssConfig: config.style.postcss,
|
2022-06-22 23:55:00 +00:00
|
|
|
}),
|
2022-06-22 23:52:32 +00:00
|
|
|
});
|
2022-03-18 22:35:45 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|