Skip to content

Commit 7f8aef7

Browse files
authored
chore: reduce use of any types (#15659)
1 parent be1c95f commit 7f8aef7

3 files changed

Lines changed: 42 additions & 29 deletions

File tree

packages/kit/src/exports/vite/dev/index.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/** @import { RequestEvent } from '@sveltejs/kit' */
2+
/** @import { PrerenderOption, UniversalNode } from 'types' */
13
import fs from 'node:fs';
24
import path from 'node:path';
35
import process from 'node:process';
@@ -34,13 +36,19 @@ const vite_css_query_regex = /(?:\?|&)(?:raw|url|inline)(?:&|$)/;
3436
export async function dev(vite, vite_config, svelte_config, get_remotes) {
3537
installPolyfills();
3638

39+
/** @type {AsyncLocalStorage<{ event: RequestEvent, config: any, prerender: PrerenderOption }>} */
3740
const async_local_storage = new AsyncLocalStorage();
3841

3942
globalThis.__SVELTEKIT_TRACK__ = (label) => {
4043
const context = async_local_storage.getStore();
4144
if (!context || context.prerender === true) return;
4245

43-
check_feature(context.event.route.id, context.config, label, svelte_config.kit.adapter);
46+
check_feature(
47+
/** @type {string} */ (context.event.route.id),
48+
context.config,
49+
label,
50+
svelte_config.kit.adapter
51+
);
4452
};
4553

4654
const fetch = globalThis.fetch;
@@ -68,7 +76,8 @@ export async function dev(vite, vite_config, svelte_config, get_remotes) {
6876
async function loud_ssr_load_module(url) {
6977
try {
7078
return await vite.ssrLoadModule(url, { fixStacktrace: true });
71-
} catch (/** @type {any} */ err) {
79+
} catch (/** @type {unknown} */ e) {
80+
const err = /** @type {import('rollup').RollupError} */ (e);
7281
const msg = buildErrorMessage(err, [colors.red(`Internal server error: ${err.message}`)]);
7382

7483
if (!vite.config.logger.hasErrorLogged(err)) {
@@ -77,13 +86,13 @@ export async function dev(vite, vite_config, svelte_config, get_remotes) {
7786

7887
vite.ws.send({
7988
type: 'error',
80-
err: {
89+
err: /** @type {import('vite').ErrorPayload['err']} */ ({
8190
...err,
8291
// these properties are non-enumerable and will
8392
// not be serialized unless we explicitly include them
8493
message: err.message,
85-
stack: err.stack
86-
}
94+
stack: err.stack ?? ''
95+
})
8796
});
8897

8998
throw err;
@@ -204,7 +213,7 @@ export async function dev(vite, vite_config, svelte_config, get_remotes) {
204213

205214
if (node.universal) {
206215
if (node.page_options?.ssr === false) {
207-
result.universal = node.page_options;
216+
result.universal = /** @type {UniversalNode} */ (node.page_options);
208217
} else {
209218
// TODO: explain why the file was loaded on the server if we fail to load it
210219
const { module, module_node } = await resolve(node.universal);
@@ -437,7 +446,7 @@ export async function dev(vite, vite_config, svelte_config, get_remotes) {
437446
return () => {
438447
const serve_static_middleware = vite.middlewares.stack.find(
439448
(middleware) =>
440-
/** @type {function} */ (middleware.handle).name === 'viteServeStaticMiddleware'
449+
/** @type {Function} */ (middleware.handle).name === 'viteServeStaticMiddleware'
441450
);
442451

443452
// Vite will give a 403 on URLs like /test, /static, and /package.json preventing us from

packages/kit/src/exports/vite/index.js

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/** @import { Options } from '@sveltejs/vite-plugin-svelte' */
2+
/** @import { PreprocessorGroup } from 'svelte/compiler' */
3+
/** @import { ConfigEnv, Manifest, Plugin, ResolvedConfig, UserConfig, ViteDevServer } from 'vite' */
14
import fs from 'node:fs';
25
import path from 'node:path';
36
import process from 'node:process';
@@ -87,7 +90,7 @@ const options_regex = /(export\s+const\s+(prerender|csr|ssr|trailingSlash))\s*=/
8790
/** @type {Set<string>} */
8891
const warned = new Set();
8992

90-
/** @type {import('svelte/compiler').PreprocessorGroup} */
93+
/** @type {PreprocessorGroup} */
9194
const warning_preprocessor = {
9295
script: ({ content, filename }) => {
9396
if (!filename) return;
@@ -130,12 +133,12 @@ const warning_preprocessor = {
130133

131134
/**
132135
* Returns the SvelteKit Vite plugins.
133-
* @returns {Promise<import('vite').Plugin[]>}
136+
* @returns {Promise<Plugin[]>}
134137
*/
135138
export async function sveltekit() {
136139
const svelte_config = await load_config();
137140

138-
/** @type {import('@sveltejs/vite-plugin-svelte').Options['preprocess']} */
141+
/** @type {Options['preprocess']} */
139142
let preprocess = svelte_config.preprocess;
140143
if (Array.isArray(preprocess)) {
141144
preprocess = [...preprocess, warning_preprocessor];
@@ -145,7 +148,7 @@ export async function sveltekit() {
145148
preprocess = warning_preprocessor;
146149
}
147150

148-
/** @type {import('@sveltejs/vite-plugin-svelte').Options} */
151+
/** @type {Options} */
149152
const vite_plugin_svelte_options = {
150153
configFile: false,
151154
extensions: svelte_config.extensions,
@@ -185,10 +188,10 @@ let build_metadata = undefined;
185188
* - https://rollupjs.org/guide/en/#output-generation-hooks
186189
*
187190
* @param {{ svelte_config: import('types').ValidatedConfig }} options
188-
* @return {Promise<import('vite').Plugin[]>}
191+
* @return {Promise<Plugin[]>}
189192
*/
190193
async function kit({ svelte_config }) {
191-
/** @type {import('vite')} */
194+
/** @type {typeof import('vite')} */
192195
const vite = await import_peer('vite');
193196

194197
// @ts-ignore `vite.rolldownVersion` only exists in `vite 8`
@@ -199,10 +202,10 @@ async function kit({ svelte_config }) {
199202

200203
const version_hash = hash(kit.version.name);
201204

202-
/** @type {import('vite').ResolvedConfig} */
205+
/** @type {ResolvedConfig} */
203206
let vite_config;
204207

205-
/** @type {import('vite').ConfigEnv} */
208+
/** @type {ConfigEnv} */
206209
let vite_config_env;
207210

208211
/** @type {boolean} */
@@ -214,7 +217,7 @@ async function kit({ svelte_config }) {
214217
/** @type {() => Promise<void>} */
215218
let finalise;
216219

217-
/** @type {import('vite').UserConfig} */
220+
/** @type {UserConfig} */
218221
let initial_config;
219222

220223
const service_worker_entry_file = resolve_entry(kit.files.serviceWorker);
@@ -234,7 +237,7 @@ async function kit({ svelte_config }) {
234237
const sourcemapIgnoreList = /** @param {string} relative_path */ (relative_path) =>
235238
relative_path.includes('node_modules') || relative_path.includes(kit.outDir);
236239

237-
/** @type {import('vite').Plugin} */
240+
/** @type {Plugin} */
238241
const plugin_setup = {
239242
name: 'vite-plugin-sveltekit-setup',
240243

@@ -268,7 +271,7 @@ async function kit({ svelte_config }) {
268271
const generated = path.posix.join(kit.outDir, 'generated');
269272

270273
// dev and preview config can be shared
271-
/** @type {import('vite').UserConfig} */
274+
/** @type {UserConfig} */
272275
const new_config = {
273276
resolve: {
274277
alias: [
@@ -438,7 +441,7 @@ async function kit({ svelte_config }) {
438441
}
439442
};
440443

441-
/** @type {import('vite').Plugin} */
444+
/** @type {Plugin} */
442445
const plugin_virtual_modules = {
443446
name: 'vite-plugin-sveltekit-virtual-modules',
444447

@@ -562,7 +565,7 @@ async function kit({ svelte_config }) {
562565
/**
563566
* Ensures that client-side code can't accidentally import server-side code,
564567
* whether in `*.server.js` files, `$app/server`, `$lib/server`, or `$env/[static|dynamic]/private`
565-
* @type {import('vite').Plugin}
568+
* @type {Plugin}
566569
*/
567570
const plugin_guard = {
568571
name: 'vite-plugin-sveltekit-guard',
@@ -669,7 +672,7 @@ async function kit({ svelte_config }) {
669672
}
670673
};
671674

672-
/** @type {import('vite').ViteDevServer} */
675+
/** @type {ViteDevServer} */
673676
let dev_server;
674677

675678
/** @type {Array<{ hash: string, file: string }>} */
@@ -681,7 +684,7 @@ async function kit({ svelte_config }) {
681684
/** @type {Set<string>} Track which remote hashes have already been emitted */
682685
const emitted_remote_hashes = new Set();
683686

684-
/** @type {import('vite').Plugin} */
687+
/** @type {Plugin} */
685688
const plugin_remote = {
686689
name: 'vite-plugin-sveltekit-remote',
687690

@@ -810,7 +813,7 @@ async function kit({ svelte_config }) {
810813
}
811814
};
812815

813-
/** @type {import('vite').Plugin} */
816+
/** @type {Plugin} */
814817
const plugin_compile = {
815818
name: 'vite-plugin-sveltekit-compile',
816819

@@ -822,7 +825,7 @@ async function kit({ svelte_config }) {
822825
// avoids overwriting the base setting that's also set by Vitest
823826
order: 'pre',
824827
handler(config) {
825-
/** @type {import('vite').UserConfig} */
828+
/** @type {UserConfig} */
826829
let new_config;
827830

828831
const kit_paths_base = kit.paths.base || '/';
@@ -1070,7 +1073,7 @@ async function kit({ svelte_config }) {
10701073
const verbose = vite_config.logLevel === 'info';
10711074
const log = logger({ verbose });
10721075

1073-
/** @type {import('vite').Manifest} */
1076+
/** @type {Manifest} */
10741077
const server_manifest = JSON.parse(read(`${out}/server/.vite/manifest.json`));
10751078

10761079
/** @type {import('types').BuildData} */
@@ -1194,7 +1197,7 @@ async function kit({ svelte_config }) {
11941197
}
11951198
}
11961199

1197-
/** @type {import('vite').Manifest} */
1200+
/** @type {Manifest} */
11981201
const client_manifest = JSON.parse(read(`${out}/client/.vite/manifest.json`));
11991202

12001203
/**
@@ -1421,8 +1424,8 @@ async function kit({ svelte_config }) {
14211424
}
14221425

14231426
/**
1424-
* @param {Record<string, any>} config
1425-
* @param {Record<string, any>} resolved_config
1427+
* @param {UserConfig} config
1428+
* @param {UserConfig} resolved_config
14261429
*/
14271430
function warn_overridden_config(config, resolved_config) {
14281431
const overridden = find_overridden_config(config, resolved_config, enforced_config, '', []);

packages/kit/types/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2935,10 +2935,11 @@ declare module '@sveltejs/kit/node/polyfills' {
29352935
}
29362936

29372937
declare module '@sveltejs/kit/vite' {
2938+
import type { Plugin } from 'vite';
29382939
/**
29392940
* Returns the SvelteKit Vite plugins.
29402941
* */
2941-
export function sveltekit(): Promise<import("vite").Plugin[]>;
2942+
export function sveltekit(): Promise<Plugin[]>;
29422943

29432944
export {};
29442945
}

0 commit comments

Comments
 (0)