Skip to content

Commit

Permalink
[chore] provide Vite config via plugin (#5073)
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann authored May 27, 2022
1 parent 2f5ec06 commit 712b4d8
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 154 deletions.
5 changes: 5 additions & 0 deletions .changeset/cyan-files-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

[chore] provide Vite config via plugin
53 changes: 38 additions & 15 deletions packages/kit/src/cli.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import sade from 'sade';
import colors from 'kleur';
import chokidar from 'chokidar';
import fs from 'fs';
import colors from 'kleur';
import { relative } from 'path';
import * as ports from 'port-authority';
import chokidar from 'chokidar';
import sade from 'sade';
import * as vite from 'vite';
import { load_config } from './core/config/index.js';
import { networkInterfaces, release } from 'os';
import { coalesce_to_error } from './utils/error.js';
Expand Down Expand Up @@ -62,35 +63,57 @@ prog
let close;

async function start() {
const { dev } = await import('./core/dev/index.js');
const svelte_config = await load_config();
const { plugins } = await import('./core/dev/plugin.js');
const vite_config = await svelte_config.kit.vite();

/** @type {import('vite').UserConfig} */
const config = {
plugins: [...(vite_config.plugins || []), plugins(svelte_config)]
};
config.server = {};

// optional config from command-line flags
// these should take precedence, but not print conflict warnings
if (host) {
config.server.host = host;
}

const { server, config } = await dev({
port,
host,
https
});
// if https is already enabled then do nothing. it could be an object and we
// don't want to overwrite with a boolean
if (https && !vite_config?.server?.https) {
config.server.https = https;
}

if (port) {
config.server.port = port;
}

const server = await vite.createServer(config);
await server.listen(port);

const address_info = /** @type {import('net').AddressInfo} */ (
/** @type {import('http').Server} */ (server.httpServer).address()
);

const vite_config = server.config;
const resolved_config = server.config;

welcome({
port: address_info.port,
host: address_info.address,
https: !!(https || vite_config.server.https),
open: first && (open || !!vite_config.server.open),
base: config.kit.paths.base,
loose: vite_config.server.fs.strict === false,
allow: vite_config.server.fs.allow
https: !!(https || resolved_config.server.https),
open: first && (open || !!resolved_config.server.open),
base: svelte_config.kit.paths.base,
loose: resolved_config.server.fs.strict === false,
allow: resolved_config.server.fs.allow
});

first = false;

return server.close;
}

// TODO: we should probably replace this with something like vite-plugin-restart
async function relaunch() {
const id = uid;
relaunching = true;
Expand Down
104 changes: 0 additions & 104 deletions packages/kit/src/core/dev/index.js

This file was deleted.

Loading

0 comments on commit 712b4d8

Please sign in to comment.