0
# Configuration Management
1
2
Configuration loading and validation for Svelte projects with support for multiple config file formats.
3
4
## Capabilities
5
6
### Load Config Function
7
8
Loads and validates Svelte configuration from svelte.config.js or svelte.config.ts files.
9
10
```typescript { .api }
11
/**
12
* Loads and validates Svelte config file
13
* @param options - Configuration loading options
14
* @returns Promise resolving to Svelte configuration
15
*/
16
function load_config(options?: { cwd?: string }): Promise<Config>;
17
18
interface Config {
19
extensions?: string[];
20
kit?: {
21
alias?: Record<string, string>;
22
files?: {
23
lib?: string;
24
};
25
outDir?: string;
26
};
27
preprocess?: PreprocessorGroup;
28
}
29
```
30
31
**Usage Examples:**
32
33
```typescript
34
import { load_config } from "@sveltejs/package/src/config.js";
35
36
// Load config from current directory
37
const config = await load_config();
38
39
// Load config from specific directory
40
const config = await load_config({ cwd: "/path/to/project" });
41
42
// Use config with build
43
import { build } from "@sveltejs/package/src/index.js";
44
await build({
45
cwd: process.cwd(),
46
input: config.kit?.files?.lib ?? "src/lib",
47
output: "dist",
48
preserve_output: false,
49
types: true,
50
config
51
});
52
```
53
54
### Load Package JSON Function
55
56
Loads package.json file from the specified directory.
57
58
```typescript { .api }
59
/**
60
* Load package.json file
61
* @param cwd - Directory to search for package.json (defaults to process.cwd())
62
* @returns Parsed package.json object or empty object if not found
63
*/
64
function load_pkg_json(cwd?: string): Record<string, any>;
65
```
66
67
**Usage Examples:**
68
69
```typescript
70
import { load_pkg_json } from "@sveltejs/package/src/config.js";
71
72
// Load package.json from current directory
73
const pkg = load_pkg_json();
74
75
// Load package.json from specific directory
76
const pkg = load_pkg_json("/path/to/project");
77
78
// Access package information
79
console.log(`Package name: ${pkg.name}`);
80
console.log(`Version: ${pkg.version}`);
81
```
82
83
## Configuration File Discovery
84
85
### Config File Resolution
86
87
The system searches for configuration files in this order:
88
89
1. `svelte.config.js`
90
2. `svelte.config.ts`
91
92
If multiple files exist, the first one found is used and a warning is displayed.
93
94
### Configuration Loading Process
95
96
1. **Discovery**: Search for config files in the specified directory
97
2. **Selection**: Choose the first config file found
98
3. **Import**: Dynamically import the config file with timestamp for cache busting
99
4. **Validation**: Check for deprecated `config.package` field
100
5. **Return**: Return the loaded configuration object
101
102
## Configuration Structure
103
104
### Extensions Configuration
105
106
```typescript
107
// svelte.config.js
108
export default {
109
extensions: ['.svelte', '.svx'] // File extensions to process
110
};
111
```
112
113
### Kit Configuration
114
115
```typescript
116
// svelte.config.js
117
export default {
118
kit: {
119
// Path aliases for import resolution
120
alias: {
121
'$lib': 'src/lib',
122
'$utils': 'src/utils'
123
},
124
125
// File paths
126
files: {
127
lib: 'src/lib' // Library source directory
128
},
129
130
// Output directory for build artifacts
131
outDir: '.svelte-kit'
132
}
133
};
134
```
135
136
### Preprocessing Configuration
137
138
```typescript
139
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
140
141
// svelte.config.js
142
export default {
143
preprocess: vitePreprocess() // Svelte preprocessor
144
};
145
```
146
147
## Error Handling
148
149
### Deprecated Configuration
150
151
The system will throw an error if the deprecated `config.package` field is found:
152
153
```typescript
154
// This will cause an error
155
export default {
156
package: { // ❌ Deprecated
157
// ...
158
}
159
};
160
```
161
162
### Missing Configuration
163
164
If no configuration file is found, an empty configuration object is returned, allowing the system to use defaults.
165
166
## Types
167
168
```typescript { .api }
169
interface Config {
170
extensions?: string[];
171
kit?: {
172
alias?: Record<string, string>;
173
files?: {
174
lib?: string;
175
};
176
outDir?: string;
177
};
178
preprocess?: PreprocessorGroup;
179
}
180
181
interface LoadConfigOptions {
182
cwd?: string;
183
}
184
```