forked from sveltejs/kit
/
index.js
109 lines (98 loc) · 2.61 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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 { get_aliases, get_runtime_path } from '../utils.js';
import { create_plugin } from './plugin.js';
import * as sync from '../sync/sync.js';
/**
* @typedef {{
* cwd: string,
* port: number,
* host?: string,
* https: boolean,
* config: import('types').ValidatedConfig
* }} Options
* @typedef {import('types').SSRComponent} SSRComponent
*/
/** @param {Options} opts */
export async function dev({ cwd, port, host, https, config }) {
sync.init(config);
const [vite_config] = deep_merge(
{
server: {
fs: {
allow: [
...new Set([
config.kit.files.lib,
config.kit.files.routes,
config.kit.outDir,
path.resolve(cwd, 'src'),
path.resolve(cwd, 'node_modules'),
path.resolve(vite.searchForWorkspaceRoot(cwd), 'node_modules')
])
]
},
port: 3000,
strictPort: true,
watch: {
ignored: [`${config.kit.outDir}/**`, `!${config.kit.outDir}/generated/**`]
}
}
},
await config.kit.vite()
);
/** @type {[any, string[]]} */
const [merged_config, conflicts] = deep_merge(vite_config, {
configFile: false,
root: cwd,
resolve: {
alias: get_aliases(config)
},
build: {
rollupOptions: {
// Vite dependency crawler needs an explicit JS entry point
// eventhough server otherwise works without it
input: `${get_runtime_path(config)}/client/start.js`
}
},
plugins: [
svelte({
...config,
emitCss: true,
compilerOptions: {
...config.compilerOptions,
hydratable: !!config.kit.browser.hydrate
},
configFile: false
}),
await create_plugin(config, cwd)
],
base: '/'
});
print_config_conflicts(conflicts, 'kit.vite.');
// optional config from command-line flags
// these should take precedence, but not print conflict warnings
if (host) {
merged_config.server.host = host;
}
// 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 && !merged_config.server.https) {
merged_config.server.https = https;
}
if (port) {
merged_config.server.port = port;
}
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()
};
}