Skip to content
Merged
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
50 changes: 26 additions & 24 deletions packages/kit/src/exports/vite/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/** @import { EnvVarConfig, KitConfig } from '@sveltejs/kit' */
/** @import { Options, SvelteConfig } from '@sveltejs/vite-plugin-svelte' */
/** @import { PreprocessorGroup } from 'svelte/compiler' */
/** @import { Plugin, Manifest, ResolvedConfig, UserConfig, ViteDevServer, Rolldown } from 'vite' */
/** @import { BuildData, ManifestData, Prerendered, ServerMetadata, RemoteInternals, ValidatedConfig, ValidatedKitConfig } from 'types' */
/** @import { Manifest, Plugin, ResolvedConfig, Rolldown, UserConfig, ViteDevServer } from 'vite' */
import fs from 'node:fs';
import path from 'node:path';
import process from 'node:process';
Expand Down Expand Up @@ -60,6 +61,7 @@ import { process_config, split_config, validate_config } from '../../core/config
import { treeshake_prerendered_remotes } from './build/remote.js';

/**
* The posix-ified root of the project based on the Vite configuration.
* Populated after Vite plugins' `config` hooks run
* @type {string}
*/
Expand Down Expand Up @@ -259,14 +261,14 @@ function plugin_root() {
* - https://rolldown.rs/apis/plugin-api#output-generation-hooks
*
* @param {object} opts
* @param {import('types').ValidatedConfig} opts.svelte_config
* @param {ValidatedConfig} opts.svelte_config
* @return {Plugin[]}
*/
function kit({ svelte_config }) {
/** @type {typeof import('vite')} */
let vite;

/** @type {import('types').ValidatedKitConfig} */
/** @type {ValidatedKitConfig} */
let kit;
/** @type {string} `kit.outDir` but posix-ified */
let out_dir;
Expand All @@ -285,10 +287,10 @@ function kit({ svelte_config }) {
/** @type {Record<string, string>} */
let env;

/** @type {import('types').ManifestData} */
/** @type {ManifestData} */
let manifest_data;

/** @type {import('types').ServerMetadata | undefined} only set at build time once analysis is finished */
/** @type {ServerMetadata | undefined} only set at build time once analysis is finished */
let build_metadata = undefined;

/** @type {UserConfig} */
Expand Down Expand Up @@ -649,18 +651,18 @@ function kit({ svelte_config }) {

case sveltekit_server: {
return dedent`
export let read_implementation = null;
export let read_implementation = null;

export let manifest = null;
export let manifest = null;

export function set_read_implementation(fn) {
read_implementation = fn;
}
export function set_read_implementation(fn) {
read_implementation = fn;
}

export function set_manifest(_) {
manifest = _;
}
`;
export function set_manifest(_) {
manifest = _;
}
`;
}
}
}
Expand Down Expand Up @@ -920,7 +922,7 @@ function kit({ svelte_config }) {

// For the client, read the exports and create a new module that only contains fetch functions with the correct metadata

/** @type {Map<string, import('types').RemoteInternals['type']>} */
/** @type {Map<string, RemoteInternals['type']>} */
const map = new Map();

// in dev, load the server module here (which will result in this hook
Expand Down Expand Up @@ -994,7 +996,7 @@ function kit({ svelte_config }) {
let vite_server_manifest;
/** @type {Manifest | null} */
let vite_client_manifest = null;
/** @type {import('types').Prerendered} */
/** @type {Prerendered} */
let prerendered;

/** @type {Set<string>} */
Expand Down Expand Up @@ -1375,7 +1377,7 @@ function kit({ svelte_config }) {
}
}
},
// during the initial server build we don't know yet
// these are stubs that will be replaced after the initial server build
define: {
__SVELTEKIT_HAS_SERVER_LOAD__: 'true',
__SVELTEKIT_HAS_UNIVERSAL_LOAD__: 'true',
Expand Down Expand Up @@ -1464,16 +1466,16 @@ function kit({ svelte_config }) {
* Adds the SvelteKit middleware to do SSR in dev mode.
* @see https://vitejs.dev/guide/api-plugin.html#configureserver
*/
async configureServer(vite) {
return await dev(vite, vite_config, svelte_config, () => remotes, root);
async configureServer(server) {
return await dev(server, vite_config, svelte_config, () => remotes, root);
},

/**
* Adds the SvelteKit middleware to do SSR in preview mode.
* @see https://vitejs.dev/guide/api-plugin.html#configurepreviewserver
*/
configurePreviewServer(vite) {
return preview(vite, vite_config, svelte_config);
configurePreviewServer(server) {
return preview(server, vite_config, svelte_config);
},

applyToEnvironment(environment) {
Expand Down Expand Up @@ -1521,13 +1523,13 @@ function kit({ svelte_config }) {
await builder.build(builder.environments.ssr)
);

const verbose = vite_config.logLevel === 'info';

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably the same but vite_config is what we store when the configResolved hook runs whereas builder.config is from when the buildApp hook runs much later

const verbose = builder.config.logLevel === 'info';
const log = logger({ verbose });

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

/** @type {import('types').BuildData} */
/** @type {BuildData} */
const build_data = {
app_dir: kit.appDir,
app_path: `${kit.paths.base.slice(1)}${kit.paths.base ? '/' : ''}${kit.appDir}`,
Expand Down Expand Up @@ -1949,7 +1951,7 @@ function find_overridden_config(config, resolved_config, enforced_config, path,
}

/**
* @param {import('types').ValidatedConfig} config
* @param {ValidatedConfig} config
*/
const create_service_worker_module = (config) => dedent`
if (typeof self === 'undefined' || self instanceof ServiceWorkerGlobalScope === false) {
Expand Down
Loading