Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore] return config from server start methods #5043

Merged
merged 2 commits into from
May 24, 2022
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
5 changes: 5 additions & 0 deletions .changeset/fresh-sheep-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

[chore] return config from server start methods
33 changes: 17 additions & 16 deletions packages/kit/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,40 +61,43 @@ prog
/** @type {() => Promise<void>} */
let close;

/** @param {import('types').ValidatedConfig} config */
async function start(config) {
async function start() {
const { dev } = await import('./core/dev/index.js');

const { address_info, server_config, close } = await dev({
const { server, config } = await dev({
port,
host,
https,
config
https
});

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

const vite_config = server.config;

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

first = false;

return close;
return server.close;
}

async function relaunch() {
const id = uid;
relaunching = true;

try {
const updated_config = await load_config();
await close();
close = await start(updated_config);
close = await start();

if (id !== uid) relaunch();
} catch (e) {
Expand All @@ -114,8 +117,7 @@ prog

process.env.NODE_ENV = process.env.NODE_ENV || 'development';

const config = await load_config();
close = await start(config);
close = await start();

chokidar.watch('svelte.config.js', { ignoreInitial: true }).on('change', () => {
if (relaunching) uid += 1;
Expand Down Expand Up @@ -178,11 +180,10 @@ prog
await check_port(port);

process.env.NODE_ENV = process.env.NODE_ENV || 'production';
const config = await load_config();

const { preview } = await import('./core/preview/index.js');

await preview({ port, host, config, https });
const { config } = await preview({ port, host, https });

welcome({ port, host, https, open, base: config.kit.paths.base });
} catch (error) {
Expand Down
17 changes: 7 additions & 10 deletions packages/kit/src/core/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import path from 'path';
import { svelte } from '@sveltejs/vite-plugin-svelte';
import * as vite from 'vite';
import { deep_merge } from '../../utils/object.js';
import { print_config_conflicts } from '../config/index.js';
import { load_config, print_config_conflicts } from '../config/index.js';
import { get_aliases, get_runtime_path } from '../utils.js';
import { create_plugin } from './plugin.js';
import * as sync from '../sync/sync.js';
Expand All @@ -14,13 +14,15 @@ const cwd = process.cwd();
* port: number,
* host?: string,
* https: boolean,
* config: import('types').ValidatedConfig
* }} Options
* @typedef {import('types').SSRComponent} SSRComponent
*/

/** @param {Options} opts */
export async function dev({ port, host, https, config }) {
export async function dev({ port, host, https }) {
/** @type {import('types').ValidatedConfig} */
const config = await load_config();

sync.init(config);

const [vite_config] = deep_merge(
Expand Down Expand Up @@ -98,13 +100,8 @@ export async function dev({ port, host, https, config }) {
const server = await vite.createServer(merged_config);
await server.listen(port);

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

return {
address_info,
server_config: vite_config.server,
close: () => server.close()
server,
config
};
}
7 changes: 4 additions & 3 deletions packages/kit/src/core/preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import sirv from 'sirv';
import { pathToFileURL } from 'url';
import { getRequest, setResponse } from '../../node/index.js';
import { installPolyfills } from '../../node/polyfills.js';
import { load_config } from '../config/index.js';
import { SVELTE_KIT_ASSETS } from '../constants.js';

/** @typedef {import('http').IncomingMessage} Req */
Expand All @@ -28,14 +29,14 @@ const mutable = (dir) =>
* @param {{
* port: number;
* host?: string;
* config: import('types').ValidatedConfig;
* https?: boolean;
* cwd?: string;
* }} opts
*/
export async function preview({ port, host, config, https: use_https = false }) {
export async function preview({ port, host, https: use_https = false }) {
installPolyfills();

const config = await load_config();
const { paths } = config.kit;
const base = paths.base;
const assets = paths.assets ? SVELTE_KIT_ASSETS : paths.base;
Expand Down Expand Up @@ -163,7 +164,7 @@ export async function preview({ port, host, config, https: use_https = false })

return new Promise((fulfil) => {
http_server.listen(port, host, () => {
fulfil(http_server);
fulfil({ server: http_server, config });
});
});
}
Expand Down