Skip to content

Commit a299b87

Browse files
dominikgbluwy
andauthored
fix: improve support for configFile: false use in kit (#319)
* fix: allow kit options inline to enable sveltekit passing loaded config without warning * fix: do not restart vite devserver on config file changes with * chore: improve docs Co-authored-by: Bjorn Lu <[email protected]> Co-authored-by: Bjorn Lu <[email protected]>
1 parent 90d64c1 commit a299b87

File tree

5 files changed

+54
-22
lines changed

5 files changed

+54
-22
lines changed

.changeset/silver-readers-wait.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': patch
3+
---
4+
5+
do not warn if kit options are passed as inline config

.changeset/young-rice-bathe.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': patch
3+
---
4+
5+
do not restart vite devserver on changes of svelte config when `configFile: false` is set

docs/config.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ export default defineConfig({
3636
});
3737
```
3838

39-
> To prevent reading the default config, use `configFile: false`.
40-
4139
A basic Svelte config looks like this:
4240

4341
```js
@@ -56,6 +54,25 @@ Depending on Node's mode, make sure you're using the correct extension and synta
5654

5755
> Try to stick with the `.js` extension whenever possible.
5856
57+
### Disable automatic handling of Svelte config
58+
59+
Use `configFile: false` to prevent `vite-plugin-svelte` from reading the config file or restarting the Vite dev server when it changes.
60+
61+
```js
62+
export default defineConfig({
63+
plugins: [
64+
svelte({
65+
configFile: false
66+
// your svelte config here
67+
})
68+
]
69+
});
70+
```
71+
72+
> Warning:
73+
> This option primarily exists for frameworks like SvelteKit that do their own parsing of Svelte config and control the Vite dev server.
74+
> You are responsible to provide the complete inline config when used.
75+
5976
## Svelte options
6077

6178
These options are specific to the Svelte compiler and are generally shared across many bundler integrations.

packages/vite-plugin-svelte/src/utils/options.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ const knownOptions = new Set([
3737
'hot',
3838
'ignorePluginPreprocessors',
3939
'disableDependencyReinclusion',
40-
'experimental'
40+
'experimental',
41+
'kit'
4142
]);
4243

4344
export function validateInlineOptions(inlineOptions?: Partial<Options>) {
@@ -355,7 +356,7 @@ export interface Options {
355356
/**
356357
* Path to a svelte config file, either absolute or relative to Vite root
357358
*
358-
* set to `false` to skip reading config from a file
359+
* set to `false` to ignore the svelte config file
359360
*
360361
* @see https://vitejs.dev/config/#root
361362
*/

packages/vite-plugin-svelte/src/utils/watch.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,30 +58,34 @@ export function setupWatchers(
5858
}
5959
};
6060

61-
const possibleSvelteConfigs = knownSvelteConfigNames.map((cfg) => path.join(root, cfg));
62-
const restartOnConfigAdd = (filename: string) => {
63-
if (possibleSvelteConfigs.includes(filename)) {
64-
triggerViteRestart(filename);
65-
}
66-
};
67-
68-
const restartOnConfigChange = (filename: string) => {
69-
if (filename === svelteConfigFile) {
70-
triggerViteRestart(filename);
71-
}
72-
};
73-
7461
// collection of watcher listeners by event
7562
const listenerCollection = {
7663
add: [] as Array<Function>,
7764
change: [emitChangeEventOnDependants],
7865
unlink: [removeUnlinkedFromCache, emitChangeEventOnDependants]
7966
};
80-
if (svelteConfigFile) {
81-
listenerCollection.change.push(restartOnConfigChange);
82-
listenerCollection.unlink.push(restartOnConfigChange);
83-
} else {
84-
listenerCollection.add.push(restartOnConfigAdd);
67+
68+
if (svelteConfigFile !== false) {
69+
// configFile false means we ignore the file and external process is responsible
70+
const possibleSvelteConfigs = knownSvelteConfigNames.map((cfg) => path.join(root, cfg));
71+
const restartOnConfigAdd = (filename: string) => {
72+
if (possibleSvelteConfigs.includes(filename)) {
73+
triggerViteRestart(filename);
74+
}
75+
};
76+
77+
const restartOnConfigChange = (filename: string) => {
78+
if (filename === svelteConfigFile) {
79+
triggerViteRestart(filename);
80+
}
81+
};
82+
83+
if (svelteConfigFile) {
84+
listenerCollection.change.push(restartOnConfigChange);
85+
listenerCollection.unlink.push(restartOnConfigChange);
86+
} else {
87+
listenerCollection.add.push(restartOnConfigAdd);
88+
}
8589
}
8690

8791
Object.entries(listenerCollection).forEach(([evt, listeners]) => {

0 commit comments

Comments
 (0)