Skip to content

Commit 55314e5

Browse files
authored
feat: automatic handling for kit.browser.hydrate config (#368)
* feat: automatic handling for kit.browser.hydrate config * fix: improve function name and add comment * chore: fix spelling
1 parent 830375d commit 55314e5

File tree

5 files changed

+56
-6
lines changed

5 files changed

+56
-6
lines changed

.changeset/five-dogs-care.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': minor
3+
---
4+
5+
Automate setting of compilerOptions.hydratable from kit.browser.hydrate option

packages/e2e-tests/kit-node/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
},
1414
"devDependencies": {
1515
"@sveltejs/adapter-node": "^1.0.0-next.78",
16-
"@sveltejs/kit": "^1.0.0-next.348",
16+
"@sveltejs/kit": "^1.0.0-next.350",
1717
"e2e-test-dep-svelte-api-only": "workspace:*",
1818
"e2e-test-dep-vite-plugins": "workspace:*",
1919
"svelte": "^3.48.0",

packages/vite-plugin-svelte/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
}
6565
},
6666
"devDependencies": {
67+
"@sveltejs/kit": "^1.0.0-next.350",
6768
"@types/debug": "^4.1.7",
6869
"@types/diff-match-patch": "^1.0.32",
6970
"diff-match-patch": "^1.0.5",

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

+27-3
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@ import { log } from './log';
1111
import { loadSvelteConfig } from './load-svelte-config';
1212
import { SVELTE_HMR_IMPORTS, SVELTE_IMPORTS, SVELTE_RESOLVE_MAIN_FIELDS } from './constants';
1313
// eslint-disable-next-line node/no-missing-import
14-
import { CompileOptions, Warning } from 'svelte/types/compiler/interfaces';
15-
import {
14+
import type { CompileOptions, Warning } from 'svelte/types/compiler/interfaces';
15+
import type {
1616
MarkupPreprocessor,
1717
Preprocessor,
1818
PreprocessorGroup,
1919
Processed
2020
// eslint-disable-next-line node/no-missing-import
2121
} from 'svelte/types/compiler/preprocess';
22+
// eslint-disable-next-line node/no-missing-import
23+
import type { KitConfig } from '@sveltejs/kit';
2224
import path from 'path';
2325
import { findRootSvelteDependencies, needsOptimization, SvelteDependency } from './dependencies';
2426
import { createRequire } from 'module';
@@ -76,7 +78,6 @@ export async function preResolveOptions(
7678
inlineOptions,
7779
extraOptions
7880
);
79-
8081
// configFile of svelteConfig contains the absolute path it was loaded from,
8182
// prefer it over the possibly relative inline path
8283
if (svelteConfig?.configFile) {
@@ -116,6 +117,7 @@ export function resolveOptions(
116117
const merged: ResolvedOptions = mergeConfigs(defaultOptions, preResolveOptions, extraOptions);
117118

118119
removeIgnoredOptions(merged);
120+
addSvelteKitOptions(merged);
119121
addExtraPreprocessors(merged, viteConfig);
120122
enforceOptionsForHmr(merged);
121123
enforceOptionsForProduction(merged);
@@ -195,6 +197,23 @@ function removeIgnoredOptions(options: ResolvedOptions) {
195197
}
196198
}
197199

200+
// some SvelteKit options need compilerOptions to work, so set them here.
201+
function addSvelteKitOptions(options: ResolvedOptions) {
202+
if (options?.kit != null) {
203+
const hydratable = options.kit.browser?.hydrate !== false;
204+
if (
205+
options.compilerOptions.hydratable != null &&
206+
options.compilerOptions.hydratable !== hydratable
207+
) {
208+
log.warn(
209+
`Conflicting values "compilerOptions.hydratable: ${options.compilerOptions.hydratable}" and "kit.browser.hydrate: ${options.kit.browser?.hydrate}" in your svelte config. You should remove "compilerOptions.hydratable".`
210+
);
211+
}
212+
log.debug(`Setting compilerOptions.hydratable: ${hydratable} for SvelteKit`);
213+
options.compilerOptions.hydratable = hydratable;
214+
}
215+
}
216+
198217
// vite passes unresolved `root`option to config hook but we need the resolved value, so do it here
199218
// https://github.com/sveltejs/vite-plugin-svelte/issues/113
200219
// https://github.com/vitejs/vite/blob/43c957de8a99bb326afd732c962f42127b0a4d1e/packages/vite/src/node/config.ts#L293
@@ -476,6 +495,11 @@ export interface Options {
476495
* These options are considered experimental and breaking changes to them can occur in any release
477496
*/
478497
experimental?: ExperimentalOptions;
498+
499+
/**
500+
* Options for SvelteKit
501+
*/
502+
kit?: KitConfig;
479503
}
480504

481505
/**

pnpm-lock.yaml

+22-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)