Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions packages/kit/src/exports/vite/dev/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/** @import { RequestEvent } from '@sveltejs/kit' */
/** @import { PrerenderOption, UniversalNode } from 'types' */
import fs from 'node:fs';
import path from 'node:path';
import process from 'node:process';
Expand Down Expand Up @@ -34,13 +36,19 @@ const vite_css_query_regex = /(?:\?|&)(?:raw|url|inline)(?:&|$)/;
export async function dev(vite, vite_config, svelte_config, get_remotes) {
installPolyfills();

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

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

check_feature(context.event.route.id, context.config, label, svelte_config.kit.adapter);
check_feature(
/** @type {string} */ (context.event.route.id),
context.config,
label,
svelte_config.kit.adapter
);
};

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

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

vite.ws.send({
type: 'error',
err: {
err: /** @type {import('vite').ErrorPayload['err']} */ ({
...err,
// these properties are non-enumerable and will
// not be serialized unless we explicitly include them
message: err.message,
stack: err.stack
}
stack: err.stack ?? ''
})
});

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

if (node.universal) {
if (node.page_options?.ssr === false) {
result.universal = node.page_options;
result.universal = /** @type {UniversalNode} */ (node.page_options);
} else {
// TODO: explain why the file was loaded on the server if we fail to load it
const { module, module_node } = await resolve(node.universal);
Expand Down Expand Up @@ -437,7 +446,7 @@ export async function dev(vite, vite_config, svelte_config, get_remotes) {
return () => {
const serve_static_middleware = vite.middlewares.stack.find(
(middleware) =>
/** @type {function} */ (middleware.handle).name === 'viteServeStaticMiddleware'
/** @type {Function} */ (middleware.handle).name === 'viteServeStaticMiddleware'
);

// Vite will give a 403 on URLs like /test, /static, and /package.json preventing us from
Expand Down
45 changes: 24 additions & 21 deletions packages/kit/src/exports/vite/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/** @import { Options } from '@sveltejs/vite-plugin-svelte' */
/** @import { PreprocessorGroup } from 'svelte/compiler' */
/** @import { ConfigEnv, Manifest, Plugin, ResolvedConfig, UserConfig, ViteDevServer } from 'vite' */
import fs from 'node:fs';
import path from 'node:path';
import process from 'node:process';
Expand Down Expand Up @@ -87,7 +90,7 @@ const options_regex = /(export\s+const\s+(prerender|csr|ssr|trailingSlash))\s*=/
/** @type {Set<string>} */
const warned = new Set();

/** @type {import('svelte/compiler').PreprocessorGroup} */
/** @type {PreprocessorGroup} */
const warning_preprocessor = {
script: ({ content, filename }) => {
if (!filename) return;
Expand Down Expand Up @@ -130,12 +133,12 @@ const warning_preprocessor = {

/**
* Returns the SvelteKit Vite plugins.
* @returns {Promise<import('vite').Plugin[]>}
* @returns {Promise<Plugin[]>}
*/
export async function sveltekit() {
const svelte_config = await load_config();

/** @type {import('@sveltejs/vite-plugin-svelte').Options['preprocess']} */
/** @type {Options['preprocess']} */
let preprocess = svelte_config.preprocess;
if (Array.isArray(preprocess)) {
preprocess = [...preprocess, warning_preprocessor];
Expand All @@ -145,7 +148,7 @@ export async function sveltekit() {
preprocess = warning_preprocessor;
}

/** @type {import('@sveltejs/vite-plugin-svelte').Options} */
/** @type {Options} */
const vite_plugin_svelte_options = {
configFile: false,
extensions: svelte_config.extensions,
Expand Down Expand Up @@ -185,10 +188,10 @@ let build_metadata = undefined;
* - https://rollupjs.org/guide/en/#output-generation-hooks
*
* @param {{ svelte_config: import('types').ValidatedConfig }} options
* @return {Promise<import('vite').Plugin[]>}
* @return {Promise<Plugin[]>}
*/
async function kit({ svelte_config }) {
/** @type {import('vite')} */
/** @type {typeof import('vite')} */
const vite = await import_peer('vite');

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

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

/** @type {import('vite').ResolvedConfig} */
/** @type {ResolvedConfig} */
let vite_config;

/** @type {import('vite').ConfigEnv} */
/** @type {ConfigEnv} */
let vite_config_env;

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

/** @type {import('vite').UserConfig} */
/** @type {UserConfig} */
let initial_config;

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

/** @type {import('vite').Plugin} */
/** @type {Plugin} */
const plugin_setup = {
name: 'vite-plugin-sveltekit-setup',

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

// dev and preview config can be shared
/** @type {import('vite').UserConfig} */
/** @type {UserConfig} */
const new_config = {
resolve: {
alias: [
Expand Down Expand Up @@ -438,7 +441,7 @@ async function kit({ svelte_config }) {
}
};

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

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

/** @type {import('vite').ViteDevServer} */
/** @type {ViteDevServer} */
let dev_server;

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

/** @type {import('vite').Plugin} */
/** @type {Plugin} */
const plugin_remote = {
name: 'vite-plugin-sveltekit-remote',

Expand Down Expand Up @@ -810,7 +813,7 @@ async function kit({ svelte_config }) {
}
};

/** @type {import('vite').Plugin} */
/** @type {Plugin} */
const plugin_compile = {
name: 'vite-plugin-sveltekit-compile',

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

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

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

/** @type {import('types').BuildData} */
Expand Down Expand Up @@ -1194,7 +1197,7 @@ async function kit({ svelte_config }) {
}
}

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

/**
Expand Down Expand Up @@ -1421,8 +1424,8 @@ async function kit({ svelte_config }) {
}

/**
* @param {Record<string, any>} config
* @param {Record<string, any>} resolved_config
* @param {UserConfig} config
* @param {UserConfig} resolved_config
*/
function warn_overridden_config(config, resolved_config) {
const overridden = find_overridden_config(config, resolved_config, enforced_config, '', []);
Expand Down
3 changes: 2 additions & 1 deletion packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2935,10 +2935,11 @@ declare module '@sveltejs/kit/node/polyfills' {
}

declare module '@sveltejs/kit/vite' {
import type { Plugin } from 'vite';
/**
* Returns the SvelteKit Vite plugins.
* */
export function sveltekit(): Promise<import("vite").Plugin[]>;
export function sveltekit(): Promise<Plugin[]>;

export {};
}
Expand Down
Loading