diff --git a/.changeset/unlucky-vans-allow.md b/.changeset/unlucky-vans-allow.md new file mode 100644 index 000000000000..68bea079cbf8 --- /dev/null +++ b/.changeset/unlucky-vans-allow.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +[fix] correctly pass Vite options in preview mode diff --git a/packages/kit/src/core/dev/index.js b/packages/kit/src/core/dev/index.js index ba8cf1c53768..b5ee87ed27f2 100644 --- a/packages/kit/src/core/dev/index.js +++ b/packages/kit/src/core/dev/index.js @@ -91,8 +91,8 @@ class Watcher extends EventEmitter { async init_server() { if (!this.manifest) throw new Error('Must call init() before init_server()'); - /** @type {any} */ - const user_config = (this.config.kit.vite && this.config.kit.vite()) || {}; + /** @type {import('vite').UserConfig} */ + const vite_config = (this.config.kit.vite && this.config.kit.vite()) || {}; const default_config = { server: { @@ -105,15 +105,15 @@ class Watcher extends EventEmitter { /** @type {(req: import("http").IncomingMessage, res: import("http").ServerResponse) => void} */ let handler = (req, res) => {}; - this.server = await get_server(this.https, user_config, (req, res) => handler(req, res)); + this.server = await get_server(this.https, vite_config, (req, res) => handler(req, res)); - const alias = user_config.resolve && user_config.resolve.alias; + const alias = vite_config.resolve && vite_config.resolve.alias; // don't warn on overriding defaults - const [modified_user_config] = deep_merge(default_config, user_config); + const [modified_vite_config] = deep_merge(default_config, vite_config); /** @type {[any, string[]]} */ - const [merged_config, conflicts] = deep_merge(modified_user_config, { + const [merged_config, conflicts] = deep_merge(modified_vite_config, { configFile: false, root: this.cwd, resolve: { @@ -153,7 +153,8 @@ class Watcher extends EventEmitter { entries: [] }, ssr: { - noExternal: get_no_external(this.cwd, user_config.ssr && user_config.ssr.noExternal) + // @ts-expect-error ssr is considered in beta, so not exposed by Vite + noExternal: get_no_external(this.cwd, vite_config.ssr && vite_config.ssr.noExternal) } }); diff --git a/packages/kit/src/core/preview/index.js b/packages/kit/src/core/preview/index.js index 2c9a43471464..cf965ab938cd 100644 --- a/packages/kit/src/core/preview/index.js +++ b/packages/kit/src/core/preview/index.js @@ -59,7 +59,10 @@ export async function preview({ read: (file) => fs.readFileSync(join(config.kit.files.assets, file)) }); - const server = await get_server(use_https, config.kit, (req, res) => { + /** @type {import('vite').UserConfig} */ + const vite_config = (config.kit.vite && config.kit.vite()) || {}; + + const server = await get_server(use_https, vite_config, (req, res) => { const parsed = parse(req.url || ''); assets_handler(req, res, () => { diff --git a/packages/kit/src/core/server/index.js b/packages/kit/src/core/server/index.js index 4463f840d673..d5f11683f418 100644 --- a/packages/kit/src/core/server/index.js +++ b/packages/kit/src/core/server/index.js @@ -1,9 +1,9 @@ import http from 'http'; import https from 'https'; + /** - * * @param {boolean} use_https - * @param {any} user_config + * @param {import('vite').UserConfig} user_config * @param {(req: http.IncomingMessage, res: http.ServerResponse) => void} handler * @returns {Promise} */ @@ -12,14 +12,13 @@ export async function get_server(use_https, user_config, handler) { const https_options = {}; if (use_https) { - if ( - user_config.server && - user_config.server.https && - user_config.server.https.key && - user_config.server.https.cert - ) { - https_options.key = user_config.server.https.key.toString(); - https_options.cert = user_config.server.https.cert.toString(); + const secure_opts = user_config.server + ? /** @type {import('tls').SecureContextOptions} */ (user_config.server.https) + : {}; + + if (secure_opts.key && secure_opts.cert) { + https_options.key = secure_opts.key.toString(); + https_options.cert = secure_opts.cert.toString(); } else { https_options.key = https_options.cert = (await import('./cert')).createCertificate(); }