Skip to content

Commit

Permalink
[fix] correctly pass Vite options in preview mode
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Jul 29, 2021
1 parent 485ee23 commit 2cd2c9f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/unlucky-vans-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

[fix] correctly pass Vite options in preview mode
15 changes: 8 additions & 7 deletions packages/kit/src/core/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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: {
Expand Down Expand Up @@ -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)
}
});

Expand Down
5 changes: 4 additions & 1 deletion packages/kit/src/core/preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, () => {
Expand Down
19 changes: 9 additions & 10 deletions packages/kit/src/core/server/index.js
Original file line number Diff line number Diff line change
@@ -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<import('net').Server>}
*/
Expand All @@ -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();
}
Expand Down

0 comments on commit 2cd2c9f

Please sign in to comment.