@sveltejs/kit 1.0.0-next.31 → 1.0.0-next.310
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -9
- package/assets/app/env.js +20 -0
- package/assets/app/navigation.js +24 -0
- package/assets/app/paths.js +1 -0
- package/assets/app/stores.js +97 -0
- package/assets/client/singletons.js +13 -0
- package/assets/client/start.js +1655 -0
- package/assets/components/error.svelte +18 -2
- package/assets/env.js +8 -0
- package/assets/paths.js +13 -0
- package/assets/server/index.js +2862 -0
- package/dist/chunks/amp_hook.js +56 -0
- package/dist/chunks/cert.js +28154 -0
- package/dist/chunks/constants.js +663 -0
- package/dist/chunks/filesystem.js +110 -0
- package/dist/chunks/index.js +515 -0
- package/dist/chunks/index2.js +1326 -0
- package/dist/chunks/index3.js +118 -0
- package/dist/chunks/index4.js +185 -0
- package/dist/chunks/index5.js +251 -0
- package/dist/chunks/index6.js +15585 -0
- package/dist/chunks/index7.js +4207 -0
- package/dist/chunks/misc.js +78 -0
- package/dist/chunks/multipart-parser.js +449 -0
- package/dist/chunks/object.js +83 -0
- package/dist/chunks/sync.js +983 -0
- package/dist/chunks/url.js +56 -0
- package/dist/cli.js +1023 -91
- package/dist/hooks.js +28 -0
- package/dist/install-fetch.js +6518 -0
- package/dist/node.js +94 -0
- package/package.json +92 -54
- package/svelte-kit.js +2 -0
- package/types/ambient.d.ts +298 -0
- package/types/index.d.ts +258 -0
- package/types/internal.d.ts +314 -0
- package/types/private.d.ts +269 -0
- package/CHANGELOG.md +0 -344
- package/assets/runtime/app/navigation.js +0 -23
- package/assets/runtime/app/navigation.js.map +0 -1
- package/assets/runtime/app/paths.js +0 -2
- package/assets/runtime/app/paths.js.map +0 -1
- package/assets/runtime/app/stores.js +0 -78
- package/assets/runtime/app/stores.js.map +0 -1
- package/assets/runtime/internal/singletons.js +0 -15
- package/assets/runtime/internal/singletons.js.map +0 -1
- package/assets/runtime/internal/start.js +0 -591
- package/assets/runtime/internal/start.js.map +0 -1
- package/assets/runtime/utils-85ebcc60.js +0 -18
- package/assets/runtime/utils-85ebcc60.js.map +0 -1
- package/dist/api.js +0 -44
- package/dist/api.js.map +0 -1
- package/dist/cli.js.map +0 -1
- package/dist/create_app.js +0 -580
- package/dist/create_app.js.map +0 -1
- package/dist/index.js +0 -375
- package/dist/index.js.map +0 -1
- package/dist/index2.js +0 -12205
- package/dist/index2.js.map +0 -1
- package/dist/index3.js +0 -549
- package/dist/index3.js.map +0 -1
- package/dist/index4.js +0 -74
- package/dist/index4.js.map +0 -1
- package/dist/index5.js +0 -468
- package/dist/index5.js.map +0 -1
- package/dist/index6.js +0 -735
- package/dist/index6.js.map +0 -1
- package/dist/renderer.js +0 -2425
- package/dist/renderer.js.map +0 -1
- package/dist/standard.js +0 -103
- package/dist/standard.js.map +0 -1
- package/dist/utils.js +0 -58
- package/dist/utils.js.map +0 -1
- package/svelte-kit +0 -3
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import fs__default from 'fs';
|
|
2
|
+
import path__default from 'path';
|
|
3
|
+
|
|
4
|
+
/** @param {string} dir */
|
|
5
|
+
function mkdirp(dir) {
|
|
6
|
+
try {
|
|
7
|
+
fs__default.mkdirSync(dir, { recursive: true });
|
|
8
|
+
} catch (/** @type {any} */ e) {
|
|
9
|
+
if (e.code === 'EEXIST') return;
|
|
10
|
+
throw e;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** @param {string} path */
|
|
15
|
+
function rimraf(path) {
|
|
16
|
+
(fs__default.rmSync || fs__default.rmdirSync)(path, { recursive: true, force: true });
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @param {string} source
|
|
21
|
+
* @param {string} target
|
|
22
|
+
* @param {{
|
|
23
|
+
* filter?: (basename: string) => boolean;
|
|
24
|
+
* replace?: Record<string, string>;
|
|
25
|
+
* }} opts
|
|
26
|
+
*/
|
|
27
|
+
function copy(source, target, opts = {}) {
|
|
28
|
+
if (!fs__default.existsSync(source)) return [];
|
|
29
|
+
|
|
30
|
+
/** @type {string[]} */
|
|
31
|
+
const files = [];
|
|
32
|
+
|
|
33
|
+
const prefix = posixify(target) + '/';
|
|
34
|
+
|
|
35
|
+
const regex = opts.replace
|
|
36
|
+
? new RegExp(`\\b(${Object.keys(opts.replace).join('|')})\\b`, 'g')
|
|
37
|
+
: null;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @param {string} from
|
|
41
|
+
* @param {string} to
|
|
42
|
+
*/
|
|
43
|
+
function go(from, to) {
|
|
44
|
+
if (opts.filter && !opts.filter(path__default.basename(from))) return;
|
|
45
|
+
|
|
46
|
+
const stats = fs__default.statSync(from);
|
|
47
|
+
|
|
48
|
+
if (stats.isDirectory()) {
|
|
49
|
+
fs__default.readdirSync(from).forEach((file) => {
|
|
50
|
+
go(path__default.join(from, file), path__default.join(to, file));
|
|
51
|
+
});
|
|
52
|
+
} else {
|
|
53
|
+
mkdirp(path__default.dirname(to));
|
|
54
|
+
|
|
55
|
+
if (opts.replace) {
|
|
56
|
+
const data = fs__default.readFileSync(from, 'utf-8');
|
|
57
|
+
fs__default.writeFileSync(
|
|
58
|
+
to,
|
|
59
|
+
data.replace(
|
|
60
|
+
/** @type {RegExp} */ (regex),
|
|
61
|
+
(match, key) => /** @type {Record<string, string>} */ (opts.replace)[key]
|
|
62
|
+
)
|
|
63
|
+
);
|
|
64
|
+
} else {
|
|
65
|
+
fs__default.copyFileSync(from, to);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
files.push(to === target ? posixify(path__default.basename(to)) : posixify(to).replace(prefix, ''));
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
go(source, target);
|
|
73
|
+
|
|
74
|
+
return files;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Get a list of all files in a directory
|
|
79
|
+
* @param {string} cwd - the directory to walk
|
|
80
|
+
* @param {boolean} [dirs] - whether to include directories in the result
|
|
81
|
+
*/
|
|
82
|
+
function walk(cwd, dirs = false) {
|
|
83
|
+
/** @type {string[]} */
|
|
84
|
+
const all_files = [];
|
|
85
|
+
|
|
86
|
+
/** @param {string} dir */
|
|
87
|
+
function walk_dir(dir) {
|
|
88
|
+
const files = fs__default.readdirSync(path__default.join(cwd, dir));
|
|
89
|
+
|
|
90
|
+
for (const file of files) {
|
|
91
|
+
const joined = path__default.join(dir, file);
|
|
92
|
+
const stats = fs__default.statSync(path__default.join(cwd, joined));
|
|
93
|
+
if (stats.isDirectory()) {
|
|
94
|
+
if (dirs) all_files.push(joined);
|
|
95
|
+
walk_dir(joined);
|
|
96
|
+
} else {
|
|
97
|
+
all_files.push(joined);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return walk_dir(''), all_files;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** @param {string} str */
|
|
106
|
+
function posixify(str) {
|
|
107
|
+
return str.replace(/\\/g, '/');
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export { copy as c, mkdirp as m, posixify as p, rimraf as r, walk as w };
|
|
@@ -0,0 +1,515 @@
|
|
|
1
|
+
import path__default from 'path';
|
|
2
|
+
import { svelte } from '@sveltejs/vite-plugin-svelte';
|
|
3
|
+
import vite from 'vite';
|
|
4
|
+
import { d as deep_merge } from './object.js';
|
|
5
|
+
import { g as get_runtime_path, r as resolve_entry, $, l as load_template, c as coalesce_to_error, a as get_mime_lookup, b as get_aliases, p as print_config_conflicts } from '../cli.js';
|
|
6
|
+
import fs__default from 'fs';
|
|
7
|
+
import { URL } from 'url';
|
|
8
|
+
import { S as SVELTE_KIT_ASSETS, s as sirv } from './constants.js';
|
|
9
|
+
import { installFetch } from '../install-fetch.js';
|
|
10
|
+
import { update, init } from './sync.js';
|
|
11
|
+
import { getRequest, setResponse } from '../node.js';
|
|
12
|
+
import { sequence } from '../hooks.js';
|
|
13
|
+
import { p as posixify } from './filesystem.js';
|
|
14
|
+
import { p as parse_route_id } from './misc.js';
|
|
15
|
+
import { n as normalize_path } from './url.js';
|
|
16
|
+
import 'sade';
|
|
17
|
+
import 'child_process';
|
|
18
|
+
import 'net';
|
|
19
|
+
import 'os';
|
|
20
|
+
import 'querystring';
|
|
21
|
+
import 'node:http';
|
|
22
|
+
import 'node:https';
|
|
23
|
+
import 'node:zlib';
|
|
24
|
+
import 'node:stream';
|
|
25
|
+
import 'node:util';
|
|
26
|
+
import 'node:url';
|
|
27
|
+
import 'stream';
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @param {import('types').ValidatedConfig} config
|
|
31
|
+
* @param {string} cwd
|
|
32
|
+
* @returns {Promise<import('vite').Plugin>}
|
|
33
|
+
*/
|
|
34
|
+
async function create_plugin(config, cwd) {
|
|
35
|
+
const runtime = get_runtime_path(config);
|
|
36
|
+
|
|
37
|
+
/** @type {import('types').Handle} */
|
|
38
|
+
let amp;
|
|
39
|
+
|
|
40
|
+
if (config.kit.amp) {
|
|
41
|
+
process.env.VITE_SVELTEKIT_AMP = 'true';
|
|
42
|
+
amp = (await import('./amp_hook.js')).handle;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
process.env.VITE_SVELTEKIT_APP_VERSION_POLL_INTERVAL = '0';
|
|
46
|
+
|
|
47
|
+
/** @type {import('types').Respond} */
|
|
48
|
+
const respond = (await import(`${runtime}/server/index.js`)).respond;
|
|
49
|
+
|
|
50
|
+
return {
|
|
51
|
+
name: 'vite-plugin-svelte-kit',
|
|
52
|
+
|
|
53
|
+
configureServer(vite) {
|
|
54
|
+
installFetch();
|
|
55
|
+
|
|
56
|
+
/** @type {import('types').SSRManifest} */
|
|
57
|
+
let manifest;
|
|
58
|
+
|
|
59
|
+
function update_manifest() {
|
|
60
|
+
const { manifest_data } = update(config);
|
|
61
|
+
|
|
62
|
+
manifest = {
|
|
63
|
+
appDir: config.kit.appDir,
|
|
64
|
+
assets: new Set(manifest_data.assets.map((asset) => asset.file)),
|
|
65
|
+
mimeTypes: get_mime_lookup(manifest_data),
|
|
66
|
+
_: {
|
|
67
|
+
entry: {
|
|
68
|
+
file: `/@fs${runtime}/client/start.js`,
|
|
69
|
+
css: [],
|
|
70
|
+
js: []
|
|
71
|
+
},
|
|
72
|
+
nodes: manifest_data.components.map((id) => {
|
|
73
|
+
return async () => {
|
|
74
|
+
const url = id.startsWith('..') ? `/@fs${path__default.posix.resolve(id)}` : `/${id}`;
|
|
75
|
+
|
|
76
|
+
const module = /** @type {import('types').SSRComponent} */ (
|
|
77
|
+
await vite.ssrLoadModule(url, { fixStacktrace: false })
|
|
78
|
+
);
|
|
79
|
+
const node = await vite.moduleGraph.getModuleByUrl(url);
|
|
80
|
+
|
|
81
|
+
if (!node) throw new Error(`Could not find node for ${url}`);
|
|
82
|
+
|
|
83
|
+
const deps = new Set();
|
|
84
|
+
find_deps(node, deps);
|
|
85
|
+
|
|
86
|
+
/** @type {Record<string, string>} */
|
|
87
|
+
const styles = {};
|
|
88
|
+
|
|
89
|
+
for (const dep of deps) {
|
|
90
|
+
const parsed = new URL(dep.url, 'http://localhost/');
|
|
91
|
+
const query = parsed.searchParams;
|
|
92
|
+
|
|
93
|
+
// TODO what about .scss files, etc?
|
|
94
|
+
if (
|
|
95
|
+
dep.file.endsWith('.css') ||
|
|
96
|
+
(query.has('svelte') && query.get('type') === 'style')
|
|
97
|
+
) {
|
|
98
|
+
try {
|
|
99
|
+
const mod = await vite.ssrLoadModule(dep.url, { fixStacktrace: false });
|
|
100
|
+
styles[dep.url] = mod.default;
|
|
101
|
+
} catch {
|
|
102
|
+
// this can happen with dynamically imported modules, I think
|
|
103
|
+
// because the Vite module graph doesn't distinguish between
|
|
104
|
+
// static and dynamic imports? TODO investigate, submit fix
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return {
|
|
110
|
+
module,
|
|
111
|
+
entry: url.endsWith('.svelte') ? url : url + '?import',
|
|
112
|
+
css: [],
|
|
113
|
+
js: [],
|
|
114
|
+
// in dev we inline all styles to avoid FOUC
|
|
115
|
+
styles
|
|
116
|
+
};
|
|
117
|
+
};
|
|
118
|
+
}),
|
|
119
|
+
routes: manifest_data.routes.map((route) => {
|
|
120
|
+
const { pattern, names, types } = parse_route_id(route.id);
|
|
121
|
+
|
|
122
|
+
if (route.type === 'page') {
|
|
123
|
+
return {
|
|
124
|
+
type: 'page',
|
|
125
|
+
id: route.id,
|
|
126
|
+
pattern,
|
|
127
|
+
names,
|
|
128
|
+
types,
|
|
129
|
+
shadow: route.shadow
|
|
130
|
+
? async () => {
|
|
131
|
+
const url = path__default.resolve(cwd, /** @type {string} */ (route.shadow));
|
|
132
|
+
return await vite.ssrLoadModule(url, { fixStacktrace: false });
|
|
133
|
+
}
|
|
134
|
+
: null,
|
|
135
|
+
a: route.a.map((id) => (id ? manifest_data.components.indexOf(id) : undefined)),
|
|
136
|
+
b: route.b.map((id) => (id ? manifest_data.components.indexOf(id) : undefined))
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return {
|
|
141
|
+
type: 'endpoint',
|
|
142
|
+
id: route.id,
|
|
143
|
+
pattern,
|
|
144
|
+
names,
|
|
145
|
+
types,
|
|
146
|
+
load: async () => {
|
|
147
|
+
const url = path__default.resolve(cwd, route.file);
|
|
148
|
+
return await vite.ssrLoadModule(url, { fixStacktrace: false });
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
}),
|
|
152
|
+
matchers: async () => {
|
|
153
|
+
/** @type {Record<string, import('types').ParamMatcher>} */
|
|
154
|
+
const matchers = {};
|
|
155
|
+
|
|
156
|
+
for (const key in manifest_data.matchers) {
|
|
157
|
+
const file = manifest_data.matchers[key];
|
|
158
|
+
const url = path__default.resolve(cwd, file);
|
|
159
|
+
const module = await vite.ssrLoadModule(url, { fixStacktrace: false });
|
|
160
|
+
|
|
161
|
+
if (module.match) {
|
|
162
|
+
matchers[key] = module.match;
|
|
163
|
+
} else {
|
|
164
|
+
throw new Error(`${file} does not export a \`match\` function`);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return matchers;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** @param {Error} error */
|
|
175
|
+
function fix_stack_trace(error) {
|
|
176
|
+
return error.stack ? vite.ssrRewriteStacktrace(error.stack) : error.stack;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
update_manifest();
|
|
180
|
+
|
|
181
|
+
vite.watcher.on('add', update_manifest);
|
|
182
|
+
vite.watcher.on('unlink', update_manifest);
|
|
183
|
+
|
|
184
|
+
const assets = config.kit.paths.assets ? SVELTE_KIT_ASSETS : config.kit.paths.base;
|
|
185
|
+
const asset_server = sirv(config.kit.files.assets, {
|
|
186
|
+
dev: true,
|
|
187
|
+
etag: true,
|
|
188
|
+
maxAge: 0,
|
|
189
|
+
extensions: []
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
return () => {
|
|
193
|
+
remove_html_middlewares(vite.middlewares);
|
|
194
|
+
|
|
195
|
+
vite.middlewares.use(async (req, res) => {
|
|
196
|
+
try {
|
|
197
|
+
if (!req.url || !req.method) throw new Error('Incomplete request');
|
|
198
|
+
|
|
199
|
+
const base = `${vite.config.server.https ? 'https' : 'http'}://${
|
|
200
|
+
req.headers[':authority'] || req.headers.host
|
|
201
|
+
}`;
|
|
202
|
+
|
|
203
|
+
const decoded = decodeURI(new URL(base + req.url).pathname);
|
|
204
|
+
|
|
205
|
+
if (decoded.startsWith(assets)) {
|
|
206
|
+
const pathname = decoded.slice(assets.length);
|
|
207
|
+
const file = config.kit.files.assets + pathname;
|
|
208
|
+
|
|
209
|
+
if (fs__default.existsSync(file) && !fs__default.statSync(file).isDirectory()) {
|
|
210
|
+
req.url = encodeURI(pathname); // don't need query/hash
|
|
211
|
+
asset_server(req, res);
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
if (req.url === '/favicon.ico') return not_found(res);
|
|
217
|
+
|
|
218
|
+
if (!decoded.startsWith(config.kit.paths.base)) {
|
|
219
|
+
const suggestion = normalize_path(
|
|
220
|
+
config.kit.paths.base + req.url,
|
|
221
|
+
config.kit.trailingSlash
|
|
222
|
+
);
|
|
223
|
+
return not_found(res, `Not found (did you mean ${suggestion}?)`);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** @type {Partial<import('types').Hooks>} */
|
|
227
|
+
const user_hooks = resolve_entry(config.kit.files.hooks)
|
|
228
|
+
? await vite.ssrLoadModule(`/${config.kit.files.hooks}`, { fixStacktrace: false })
|
|
229
|
+
: {};
|
|
230
|
+
|
|
231
|
+
const handle = user_hooks.handle || (({ event, resolve }) => resolve(event));
|
|
232
|
+
|
|
233
|
+
/** @type {import('types').Hooks} */
|
|
234
|
+
const hooks = {
|
|
235
|
+
getSession: user_hooks.getSession || (() => ({})),
|
|
236
|
+
handle: amp ? sequence(amp, handle) : handle,
|
|
237
|
+
handleError:
|
|
238
|
+
user_hooks.handleError ||
|
|
239
|
+
(({ /** @type {Error & { frame?: string }} */ error }) => {
|
|
240
|
+
console.error($.bold().red(error.message));
|
|
241
|
+
if (error.frame) {
|
|
242
|
+
console.error($.gray(error.frame));
|
|
243
|
+
}
|
|
244
|
+
if (error.stack) {
|
|
245
|
+
console.error($.gray(error.stack));
|
|
246
|
+
}
|
|
247
|
+
}),
|
|
248
|
+
externalFetch: user_hooks.externalFetch || fetch
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
if (/** @type {any} */ (hooks).getContext) {
|
|
252
|
+
// TODO remove this for 1.0
|
|
253
|
+
throw new Error(
|
|
254
|
+
'The getContext hook has been removed. See https://kit.svelte.dev/docs/hooks'
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
if (/** @type {any} */ (hooks).serverFetch) {
|
|
259
|
+
// TODO remove this for 1.0
|
|
260
|
+
throw new Error('The serverFetch hook has been renamed to externalFetch.');
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// TODO the / prefix will probably fail if outDir is outside the cwd (which
|
|
264
|
+
// could be the case in a monorepo setup), but without it these modules
|
|
265
|
+
// can get loaded twice via different URLs, which causes failures. Might
|
|
266
|
+
// require changes to Vite to fix
|
|
267
|
+
const { default: root } = await vite.ssrLoadModule(
|
|
268
|
+
`/${posixify(path__default.relative(cwd, `${config.kit.outDir}/generated/root.svelte`))}`,
|
|
269
|
+
{ fixStacktrace: false }
|
|
270
|
+
);
|
|
271
|
+
|
|
272
|
+
const paths = await vite.ssrLoadModule(
|
|
273
|
+
true
|
|
274
|
+
? `/${posixify(path__default.relative(cwd, `${config.kit.outDir}/runtime/paths.js`))}`
|
|
275
|
+
: `/@fs${runtime}/paths.js`,
|
|
276
|
+
{ fixStacktrace: false }
|
|
277
|
+
);
|
|
278
|
+
|
|
279
|
+
paths.set_paths({
|
|
280
|
+
base: config.kit.paths.base,
|
|
281
|
+
assets
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
let request;
|
|
285
|
+
|
|
286
|
+
try {
|
|
287
|
+
request = await getRequest(base, req);
|
|
288
|
+
} catch (/** @type {any} */ err) {
|
|
289
|
+
res.statusCode = err.status || 400;
|
|
290
|
+
return res.end(err.reason || 'Invalid request body');
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
const template = load_template(cwd, config);
|
|
294
|
+
|
|
295
|
+
const rendered = await respond(
|
|
296
|
+
request,
|
|
297
|
+
{
|
|
298
|
+
amp: config.kit.amp,
|
|
299
|
+
csp: config.kit.csp,
|
|
300
|
+
dev: true,
|
|
301
|
+
floc: config.kit.floc,
|
|
302
|
+
get_stack: (error) => {
|
|
303
|
+
return fix_stack_trace(error);
|
|
304
|
+
},
|
|
305
|
+
handle_error: (error, event) => {
|
|
306
|
+
hooks.handleError({
|
|
307
|
+
error: new Proxy(error, {
|
|
308
|
+
get: (target, property) => {
|
|
309
|
+
if (property === 'stack') {
|
|
310
|
+
return fix_stack_trace(error);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
return Reflect.get(target, property, target);
|
|
314
|
+
}
|
|
315
|
+
}),
|
|
316
|
+
event,
|
|
317
|
+
|
|
318
|
+
// TODO remove for 1.0
|
|
319
|
+
// @ts-expect-error
|
|
320
|
+
get request() {
|
|
321
|
+
throw new Error(
|
|
322
|
+
'request in handleError has been replaced with event. See https://github.com/sveltejs/kit/pull/3384 for details'
|
|
323
|
+
);
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
},
|
|
327
|
+
hooks,
|
|
328
|
+
hydrate: config.kit.browser.hydrate,
|
|
329
|
+
manifest,
|
|
330
|
+
method_override: config.kit.methodOverride,
|
|
331
|
+
paths: {
|
|
332
|
+
base: config.kit.paths.base,
|
|
333
|
+
assets
|
|
334
|
+
},
|
|
335
|
+
prefix: '',
|
|
336
|
+
prerender: config.kit.prerender.enabled,
|
|
337
|
+
read: (file) => fs__default.readFileSync(path__default.join(config.kit.files.assets, file)),
|
|
338
|
+
root,
|
|
339
|
+
router: config.kit.browser.router,
|
|
340
|
+
template: ({ head, body, assets, nonce }) => {
|
|
341
|
+
return (
|
|
342
|
+
template
|
|
343
|
+
.replace(/%svelte\.assets%/g, assets)
|
|
344
|
+
.replace(/%svelte\.nonce%/g, nonce)
|
|
345
|
+
// head and body must be replaced last, in case someone tries to sneak in %svelte.assets% etc
|
|
346
|
+
.replace('%svelte.head%', () => head)
|
|
347
|
+
.replace('%svelte.body%', () => body)
|
|
348
|
+
);
|
|
349
|
+
},
|
|
350
|
+
template_contains_nonce: template.includes('%svelte.nonce%'),
|
|
351
|
+
trailing_slash: config.kit.trailingSlash
|
|
352
|
+
},
|
|
353
|
+
{
|
|
354
|
+
getClientAddress: () => {
|
|
355
|
+
const { remoteAddress } = req.socket;
|
|
356
|
+
if (remoteAddress) return remoteAddress;
|
|
357
|
+
throw new Error('Could not determine clientAddress');
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
);
|
|
361
|
+
|
|
362
|
+
if (rendered) {
|
|
363
|
+
setResponse(res, rendered);
|
|
364
|
+
} else {
|
|
365
|
+
not_found(res);
|
|
366
|
+
}
|
|
367
|
+
} catch (e) {
|
|
368
|
+
const error = coalesce_to_error(e);
|
|
369
|
+
vite.ssrFixStacktrace(error);
|
|
370
|
+
res.statusCode = 500;
|
|
371
|
+
res.end(error.stack);
|
|
372
|
+
}
|
|
373
|
+
});
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/** @param {import('http').ServerResponse} res */
|
|
380
|
+
function not_found(res, message = 'Not found') {
|
|
381
|
+
res.statusCode = 404;
|
|
382
|
+
res.end(message);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* @param {import('connect').Server} server
|
|
387
|
+
*/
|
|
388
|
+
function remove_html_middlewares(server) {
|
|
389
|
+
const html_middlewares = [
|
|
390
|
+
'viteIndexHtmlMiddleware',
|
|
391
|
+
'vite404Middleware',
|
|
392
|
+
'viteSpaFallbackMiddleware'
|
|
393
|
+
];
|
|
394
|
+
for (let i = server.stack.length - 1; i > 0; i--) {
|
|
395
|
+
// @ts-expect-error using internals until https://github.com/vitejs/vite/pull/4640 is merged
|
|
396
|
+
if (html_middlewares.includes(server.stack[i].handle.name)) {
|
|
397
|
+
server.stack.splice(i, 1);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* @param {import('vite').ModuleNode} node
|
|
404
|
+
* @param {Set<import('vite').ModuleNode>} deps
|
|
405
|
+
*/
|
|
406
|
+
function find_deps(node, deps) {
|
|
407
|
+
for (const dep of node.importedModules) {
|
|
408
|
+
if (!deps.has(dep)) {
|
|
409
|
+
deps.add(dep);
|
|
410
|
+
find_deps(dep, deps);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* @typedef {{
|
|
417
|
+
* cwd: string,
|
|
418
|
+
* port: number,
|
|
419
|
+
* host?: string,
|
|
420
|
+
* https: boolean,
|
|
421
|
+
* config: import('types').ValidatedConfig
|
|
422
|
+
* }} Options
|
|
423
|
+
* @typedef {import('types').SSRComponent} SSRComponent
|
|
424
|
+
*/
|
|
425
|
+
|
|
426
|
+
/** @param {Options} opts */
|
|
427
|
+
async function dev({ cwd, port, host, https, config }) {
|
|
428
|
+
init(config);
|
|
429
|
+
|
|
430
|
+
const [vite_config] = deep_merge(
|
|
431
|
+
{
|
|
432
|
+
server: {
|
|
433
|
+
fs: {
|
|
434
|
+
allow: [
|
|
435
|
+
...new Set([
|
|
436
|
+
config.kit.files.lib,
|
|
437
|
+
config.kit.files.routes,
|
|
438
|
+
config.kit.outDir,
|
|
439
|
+
path__default.resolve(cwd, 'src'),
|
|
440
|
+
path__default.resolve(cwd, 'node_modules'),
|
|
441
|
+
path__default.resolve(vite.searchForWorkspaceRoot(cwd), 'node_modules')
|
|
442
|
+
])
|
|
443
|
+
]
|
|
444
|
+
},
|
|
445
|
+
strictPort: true
|
|
446
|
+
}
|
|
447
|
+
},
|
|
448
|
+
await config.kit.vite()
|
|
449
|
+
);
|
|
450
|
+
|
|
451
|
+
/** @type {[any, string[]]} */
|
|
452
|
+
const [merged_config, conflicts] = deep_merge(vite_config, {
|
|
453
|
+
configFile: false,
|
|
454
|
+
root: cwd,
|
|
455
|
+
resolve: {
|
|
456
|
+
alias: get_aliases(config)
|
|
457
|
+
},
|
|
458
|
+
build: {
|
|
459
|
+
rollupOptions: {
|
|
460
|
+
// Vite dependency crawler needs an explicit JS entry point
|
|
461
|
+
// eventhough server otherwise works without it
|
|
462
|
+
input: `${get_runtime_path(config)}/client/start.js`
|
|
463
|
+
}
|
|
464
|
+
},
|
|
465
|
+
plugins: [
|
|
466
|
+
svelte({
|
|
467
|
+
extensions: config.extensions,
|
|
468
|
+
// In AMP mode, we know that there are no conditional component imports. In that case, we
|
|
469
|
+
// don't need to include CSS for components that are imported but unused, so we can just
|
|
470
|
+
// include rendered CSS.
|
|
471
|
+
// This would also apply if hydrate and router are both false, but we don't know if one
|
|
472
|
+
// has been enabled at the page level, so we don't do anything there.
|
|
473
|
+
emitCss: !config.kit.amp,
|
|
474
|
+
compilerOptions: {
|
|
475
|
+
hydratable: !!config.kit.browser.hydrate
|
|
476
|
+
}
|
|
477
|
+
}),
|
|
478
|
+
await create_plugin(config, cwd)
|
|
479
|
+
],
|
|
480
|
+
base: '/'
|
|
481
|
+
});
|
|
482
|
+
|
|
483
|
+
print_config_conflicts(conflicts, 'kit.vite.');
|
|
484
|
+
|
|
485
|
+
// optional config from command-line flags
|
|
486
|
+
// these should take precedence, but not print conflict warnings
|
|
487
|
+
if (host) {
|
|
488
|
+
merged_config.server.host = host;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
// if https is already enabled then do nothing. it could be an object and we
|
|
492
|
+
// don't want to overwrite with a boolean
|
|
493
|
+
if (https && !merged_config.server.https) {
|
|
494
|
+
merged_config.server.https = https;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
if (port) {
|
|
498
|
+
merged_config.server.port = port;
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
const server = await vite.createServer(merged_config);
|
|
502
|
+
await server.listen(port);
|
|
503
|
+
|
|
504
|
+
const address_info = /** @type {import('net').AddressInfo} */ (
|
|
505
|
+
/** @type {import('http').Server} */ (server.httpServer).address()
|
|
506
|
+
);
|
|
507
|
+
|
|
508
|
+
return {
|
|
509
|
+
address_info,
|
|
510
|
+
server_config: vite_config.server,
|
|
511
|
+
close: () => server.close()
|
|
512
|
+
};
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
export { dev };
|