The official Vite plugin for integrating Svelte components into Vite-based development workflows.
—
Utilities for loading and parsing Svelte configuration files in various build scenarios. This module provides functions to discover, load, and process Svelte configuration files that integrate with Vite's configuration system.
Load and parse Svelte configuration files with automatic discovery and validation.
/**
* Loads and parses Svelte configuration files
* @param viteConfig - Optional Vite user configuration for context
* @param inlineOptions - Optional inline plugin options that may affect config loading
* @returns Promise resolving to parsed Svelte config or undefined if no config found
*/
function loadSvelteConfig(
viteConfig?: UserConfig,
inlineOptions?: Partial<Options>
): Promise<Partial<SvelteConfig> | undefined>;Usage Examples:
import { loadSvelteConfig } from '@sveltejs/vite-plugin-svelte';
// Basic usage - auto-discover config file
const config = await loadSvelteConfig();
console.log(config?.compilerOptions);
// With Vite config context
const viteConfig = { root: './src' };
const config = await loadSvelteConfig(viteConfig);
// With inline options
const config = await loadSvelteConfig(viteConfig, {
configFile: './custom.svelte.config.js'
});
// Disable config file loading
const config = await loadSvelteConfig(viteConfig, {
configFile: false
});The system automatically discovers Svelte configuration files in the following order:
const knownSvelteConfigNames: string[] = [
'svelte.config.js',
'svelte.config.ts',
'svelte.config.mjs',
'svelte.config.mts'
];File Discovery Process:
configFile in inline optionsSpecify custom configuration file locations:
// Relative to Vite root
const config = await loadSvelteConfig(viteConfig, {
configFile: './config/svelte.config.js'
});
// Absolute path
const config = await loadSvelteConfig(viteConfig, {
configFile: '/path/to/project/svelte.config.js'
});
// Disable config loading entirely
const config = await loadSvelteConfig(viteConfig, {
configFile: false
});// svelte.config.js
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
/** @type {import('@sveltejs/vite-plugin-svelte').SvelteConfig} */
export default {
extensions: ['.svelte'],
preprocess: vitePreprocess({
style: true
}),
compilerOptions: {
runes: true,
dev: process.env.NODE_ENV === 'development'
},
vitePlugin: {
emitCss: true,
inspector: true
},
onwarn(warning, defaultHandler) {
// Custom warning handling
if (warning.code === 'css-unused-selector') return;
defaultHandler(warning);
}
};// svelte.config.ts
import type { SvelteConfig } from '@sveltejs/vite-plugin-svelte';
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
const config: SvelteConfig = {
extensions: ['.svelte'],
preprocess: vitePreprocess({
style: true,
script: false
}),
compilerOptions: {
runes: true,
hydratable: true
},
vitePlugin: {
emitCss: true,
hot: true,
inspector: {
holdMode: true,
showToggleButton: 'always'
}
}
};
export default config;// svelte.config.mjs
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
export default {
preprocess: vitePreprocess(),
compilerOptions: {
runes: true
}
};// svelte.config.js
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
const isDev = process.env.NODE_ENV === 'development';
const isProd = process.env.NODE_ENV === 'production';
export default {
preprocess: vitePreprocess({
style: {
css: {
devSourcemap: isDev,
minify: isProd
}
}
}),
compilerOptions: {
dev: isDev,
hydratable: isProd,
runes: true
},
vitePlugin: {
emitCss: true,
hot: isDev,
inspector: isDev,
prebundleSvelteLibraries: isDev
},
onwarn(warning, defaultHandler) {
// Ignore certain warnings in production
if (isProd && ['css-unused-selector', 'a11y-missing-attribute'].includes(warning.code)) {
return;
}
defaultHandler(warning);
}
};The configuration loader includes comprehensive error handling:
try {
const config = await loadSvelteConfig(viteConfig, options);
if (!config) {
console.log('No Svelte config found, using defaults');
}
} catch (error) {
console.error('Failed to load Svelte config:', error.message);
// Handle configuration loading errors
}The loader validates configuration structure and provides meaningful error messages for common issues:
Integrate configuration loading into custom build tools:
// Custom build script
import { loadSvelteConfig } from '@sveltejs/vite-plugin-svelte';
async function buildProject() {
const viteConfig = await loadViteConfig();
const svelteConfig = await loadSvelteConfig(viteConfig);
// Use configuration to customize build process
const shouldMinify = !svelteConfig?.compilerOptions?.dev;
const preprocessors = svelteConfig?.preprocess || [];
// Custom build logic here
}Load configuration in test environments:
// test-utils.js
import { loadSvelteConfig } from '@sveltejs/vite-plugin-svelte';
export async function setupSvelteTest() {
const config = await loadSvelteConfig({
root: process.cwd()
}, {
configFile: './svelte.config.test.js'
});
return config;
}Use configuration loading in development tools:
// dev-tool.js
import { loadSvelteConfig } from '@sveltejs/vite-plugin-svelte';
async function analyzeSvelteProject() {
const config = await loadSvelteConfig();
// Analyze project based on configuration
const extensions = config?.extensions || ['.svelte'];
const hasPreprocessors = Boolean(config?.preprocess);
const isRunesEnabled = config?.compilerOptions?.runes;
return {
extensions,
hasPreprocessors,
isRunesEnabled
};
}The configuration system merges options from multiple sources in priority order:
This allows for flexible configuration overrides while maintaining sensible defaults.
// Example of configuration precedence
const finalConfig = {
// Defaults
extensions: ['.svelte'],
emitCss: true,
// Overridden by svelte.config.js
...svelteConfigFile,
// Overridden by inline options
...inlineOptions
};Install with Tessl CLI
npx tessl i tessl/npm-sveltejs--vite-plugin-svelte