@sveltejs/kit 1.0.0-next.45 → 1.0.0-next.450
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/package.json +92 -63
- package/src/cli.js +112 -0
- package/src/constants.js +7 -0
- package/src/core/adapt/builder.js +223 -0
- package/src/core/adapt/index.js +19 -0
- package/src/core/config/index.js +86 -0
- package/src/core/config/options.js +488 -0
- package/src/core/config/types.d.ts +1 -0
- package/src/core/env.js +97 -0
- package/src/core/generate_manifest/index.js +78 -0
- package/src/core/prerender/crawl.js +194 -0
- package/src/core/prerender/prerender.js +380 -0
- package/src/core/prerender/queue.js +80 -0
- package/src/core/sync/create_manifest_data/index.js +452 -0
- package/src/core/sync/create_manifest_data/types.d.ts +37 -0
- package/src/core/sync/sync.js +59 -0
- package/src/core/sync/utils.js +33 -0
- package/src/core/sync/write_ambient.js +27 -0
- package/src/core/sync/write_client_manifest.js +94 -0
- package/src/core/sync/write_matchers.js +25 -0
- package/src/core/sync/write_root.js +91 -0
- package/src/core/sync/write_tsconfig.js +195 -0
- package/src/core/sync/write_types/index.js +596 -0
- package/src/core/utils.js +70 -0
- package/src/exports/hooks/index.js +1 -0
- package/src/exports/hooks/sequence.js +26 -0
- package/src/exports/index.js +45 -0
- package/src/exports/node/index.js +145 -0
- package/src/exports/node/polyfills.js +40 -0
- package/src/exports/vite/build/build_server.js +348 -0
- package/src/exports/vite/build/build_service_worker.js +90 -0
- package/src/exports/vite/build/utils.js +160 -0
- package/src/exports/vite/dev/index.js +543 -0
- package/src/exports/vite/index.js +588 -0
- package/src/exports/vite/preview/index.js +186 -0
- package/src/exports/vite/types.d.ts +3 -0
- package/src/exports/vite/utils.js +345 -0
- package/src/runtime/app/env.js +1 -0
- package/src/runtime/app/environment.js +11 -0
- package/src/runtime/app/navigation.js +22 -0
- package/src/runtime/app/paths.js +1 -0
- package/src/runtime/app/stores.js +102 -0
- package/src/runtime/client/ambient.d.ts +24 -0
- package/src/runtime/client/client.js +1394 -0
- package/src/runtime/client/fetcher.js +60 -0
- package/src/runtime/client/parse.js +60 -0
- package/src/runtime/client/singletons.js +21 -0
- package/src/runtime/client/start.js +48 -0
- package/src/runtime/client/types.d.ts +88 -0
- package/src/runtime/client/utils.js +113 -0
- package/src/runtime/components/error.svelte +16 -0
- package/{assets → src/runtime}/components/layout.svelte +0 -0
- package/src/runtime/control.js +33 -0
- package/src/runtime/env/dynamic/private.js +1 -0
- package/src/runtime/env/dynamic/public.js +1 -0
- package/src/runtime/env-private.js +7 -0
- package/src/runtime/env-public.js +7 -0
- package/src/runtime/env.js +6 -0
- package/src/runtime/hash.js +16 -0
- package/src/runtime/paths.js +11 -0
- package/src/runtime/server/data/index.js +146 -0
- package/src/runtime/server/endpoint.js +50 -0
- package/src/runtime/server/index.js +369 -0
- package/src/runtime/server/page/cookie.js +25 -0
- package/src/runtime/server/page/crypto.js +239 -0
- package/src/runtime/server/page/csp.js +249 -0
- package/src/runtime/server/page/fetch.js +266 -0
- package/src/runtime/server/page/index.js +413 -0
- package/src/runtime/server/page/load_data.js +124 -0
- package/src/runtime/server/page/render.js +379 -0
- package/src/runtime/server/page/respond_with_error.js +94 -0
- package/src/runtime/server/page/types.d.ts +44 -0
- package/src/runtime/server/utils.js +137 -0
- package/src/utils/array.js +9 -0
- package/src/utils/error.js +22 -0
- package/src/utils/escape.js +104 -0
- package/src/utils/filesystem.js +108 -0
- package/src/utils/functions.js +16 -0
- package/src/utils/http.js +55 -0
- package/src/utils/misc.js +1 -0
- package/src/utils/routing.js +146 -0
- package/src/utils/url.js +142 -0
- package/svelte-kit.js +1 -1
- package/types/ambient.d.ts +366 -0
- package/types/index.d.ts +345 -0
- package/types/internal.d.ts +379 -0
- package/types/private.d.ts +209 -0
- package/CHANGELOG.md +0 -450
- package/assets/components/error.svelte +0 -13
- package/assets/runtime/app/env.js +0 -5
- package/assets/runtime/app/navigation.js +0 -41
- package/assets/runtime/app/paths.js +0 -1
- package/assets/runtime/app/stores.js +0 -93
- package/assets/runtime/chunks/utils.js +0 -19
- package/assets/runtime/internal/singletons.js +0 -23
- package/assets/runtime/internal/start.js +0 -770
- package/assets/runtime/paths.js +0 -12
- package/dist/chunks/index.js +0 -3521
- package/dist/chunks/index2.js +0 -587
- package/dist/chunks/index3.js +0 -246
- package/dist/chunks/index4.js +0 -536
- package/dist/chunks/index5.js +0 -761
- package/dist/chunks/index6.js +0 -322
- package/dist/chunks/standard.js +0 -99
- package/dist/chunks/utils.js +0 -83
- package/dist/cli.js +0 -546
- package/dist/ssr.js +0 -2580
|
@@ -0,0 +1,596 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import MagicString from 'magic-string';
|
|
4
|
+
import { posixify, rimraf, walk } from '../../../utils/filesystem.js';
|
|
5
|
+
import { compact } from '../../../utils/array.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @typedef {{
|
|
9
|
+
* modified: boolean;
|
|
10
|
+
* code: string;
|
|
11
|
+
* exports: any[];
|
|
12
|
+
* } | null} Proxy
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/** @type {import('typescript')} */
|
|
16
|
+
// @ts-ignore
|
|
17
|
+
let ts = undefined;
|
|
18
|
+
try {
|
|
19
|
+
ts = (await import('typescript')).default;
|
|
20
|
+
} catch {}
|
|
21
|
+
|
|
22
|
+
const cwd = process.cwd();
|
|
23
|
+
|
|
24
|
+
const shared_names = new Set(['load']);
|
|
25
|
+
const server_names = new Set(['load', 'POST', 'PUT', 'PATCH', 'DELETE']); // TODO replace with a single `action`
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Creates types for the whole manifest
|
|
29
|
+
* @param {import('types').ValidatedConfig} config
|
|
30
|
+
* @param {import('types').ManifestData} manifest_data
|
|
31
|
+
*/
|
|
32
|
+
export async function write_all_types(config, manifest_data) {
|
|
33
|
+
if (!ts) return;
|
|
34
|
+
|
|
35
|
+
const types_dir = `${config.kit.outDir}/types`;
|
|
36
|
+
|
|
37
|
+
// empty out files that no longer need to exist
|
|
38
|
+
const routes_dir = path.relative('.', config.kit.files.routes);
|
|
39
|
+
const expected_directories = new Set(
|
|
40
|
+
manifest_data.routes.map((route) => path.join(routes_dir, route.id))
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
if (fs.existsSync(types_dir)) {
|
|
44
|
+
for (const file of walk(types_dir)) {
|
|
45
|
+
const dir = path.dirname(file);
|
|
46
|
+
if (!expected_directories.has(dir)) {
|
|
47
|
+
rimraf(file);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const routes_map = create_routes_map(manifest_data);
|
|
53
|
+
// For each directory, write $types.d.ts
|
|
54
|
+
for (const route of manifest_data.routes) {
|
|
55
|
+
update_types(config, routes_map, route);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Creates types related to the given file. This should only be called
|
|
61
|
+
* if the file in question was edited, not if it was created/deleted/moved.
|
|
62
|
+
* @param {import('types').ValidatedConfig} config
|
|
63
|
+
* @param {import('types').ManifestData} manifest_data
|
|
64
|
+
* @param {string} file
|
|
65
|
+
*/
|
|
66
|
+
export async function write_types(config, manifest_data, file) {
|
|
67
|
+
if (!ts) return;
|
|
68
|
+
|
|
69
|
+
if (!path.basename(file).startsWith('+')) {
|
|
70
|
+
// Not a route file
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const filepath = path.relative(config.kit.files.routes, file);
|
|
75
|
+
const id = path.dirname(filepath);
|
|
76
|
+
|
|
77
|
+
const route = manifest_data.routes.find((route) => route.id === id);
|
|
78
|
+
if (!route) return; // this shouldn't ever happen
|
|
79
|
+
|
|
80
|
+
update_types(config, create_routes_map(manifest_data), route);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Collect all leafs into a leaf -> route map
|
|
85
|
+
* @param {import('types').ManifestData} manifest_data
|
|
86
|
+
*/
|
|
87
|
+
function create_routes_map(manifest_data) {
|
|
88
|
+
/** @type {Map<import('types').PageNode, import('types').RouteData>} */
|
|
89
|
+
const map = new Map();
|
|
90
|
+
for (const route of manifest_data.routes) {
|
|
91
|
+
if (route.leaf) {
|
|
92
|
+
map.set(route.leaf, route);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return map;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Update types for a specific route
|
|
100
|
+
* @param {import('types').ValidatedConfig} config
|
|
101
|
+
* @param {Map<import('types').PageNode, import('types').RouteData>} routes
|
|
102
|
+
* @param {import('types').RouteData} route
|
|
103
|
+
*/
|
|
104
|
+
function update_types(config, routes, route) {
|
|
105
|
+
if (!route.leaf && !route.layout && !route.endpoint) return; // nothing to do
|
|
106
|
+
|
|
107
|
+
const routes_dir = posixify(path.relative('.', config.kit.files.routes));
|
|
108
|
+
const outdir = path.join(config.kit.outDir, 'types', routes_dir, route.id);
|
|
109
|
+
|
|
110
|
+
// first, check if the types are out of date
|
|
111
|
+
const input_files = [];
|
|
112
|
+
|
|
113
|
+
/** @type {import('types').PageNode | null} */
|
|
114
|
+
let node = route.leaf;
|
|
115
|
+
while (node) {
|
|
116
|
+
if (node.shared) input_files.push(node.shared);
|
|
117
|
+
if (node.server) input_files.push(node.server);
|
|
118
|
+
node = node.parent ?? null;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** @type {import('types').PageNode | null} */
|
|
122
|
+
node = route.layout;
|
|
123
|
+
while (node) {
|
|
124
|
+
if (node.shared) input_files.push(node.shared);
|
|
125
|
+
if (node.server) input_files.push(node.server);
|
|
126
|
+
node = node.parent ?? null;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (route.endpoint) {
|
|
130
|
+
input_files.push(route.endpoint.file);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
try {
|
|
134
|
+
fs.mkdirSync(outdir, { recursive: true });
|
|
135
|
+
} catch {}
|
|
136
|
+
|
|
137
|
+
const output_files = compact(
|
|
138
|
+
fs.readdirSync(outdir).map((name) => {
|
|
139
|
+
const stats = fs.statSync(path.join(outdir, name));
|
|
140
|
+
if (stats.isDirectory()) return;
|
|
141
|
+
return {
|
|
142
|
+
name,
|
|
143
|
+
updated: stats.mtimeMs
|
|
144
|
+
};
|
|
145
|
+
})
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
const source_last_updated = Math.max(...input_files.map((file) => fs.statSync(file).mtimeMs));
|
|
149
|
+
const types_last_updated = Math.max(...output_files.map((file) => file?.updated));
|
|
150
|
+
|
|
151
|
+
// types were generated more recently than the source files, so don't regenerate
|
|
152
|
+
if (types_last_updated > source_last_updated) return;
|
|
153
|
+
|
|
154
|
+
// track which old files end up being surplus to requirements
|
|
155
|
+
const to_delete = new Set(output_files.map((file) => file.name));
|
|
156
|
+
|
|
157
|
+
// now generate new types
|
|
158
|
+
const imports = [`import type * as Kit from '@sveltejs/kit';`];
|
|
159
|
+
|
|
160
|
+
/** @type {string[]} */
|
|
161
|
+
const declarations = [];
|
|
162
|
+
|
|
163
|
+
/** @type {string[]} */
|
|
164
|
+
const exports = [];
|
|
165
|
+
|
|
166
|
+
declarations.push(
|
|
167
|
+
`type RouteParams = { ${route.names.map((param) => `${param}: string`).join('; ')} }`
|
|
168
|
+
);
|
|
169
|
+
|
|
170
|
+
// These could also be placed in our public types, but it would bloat them unnecessarily and we may want to change these in the future
|
|
171
|
+
if (route.layout || route.leaf) {
|
|
172
|
+
// If T extends the empty object, void is also allowed as a return type
|
|
173
|
+
declarations.push(`type MaybeWithVoid<T> = {} extends T ? T | void : T;`);
|
|
174
|
+
// Returns the key of the object whose values are required.
|
|
175
|
+
declarations.push(
|
|
176
|
+
`export type RequiredKeys<T> = { [K in keyof T]-?: {} extends { [P in K]: T[K] } ? never : K; }[keyof T];`
|
|
177
|
+
);
|
|
178
|
+
// Helper type to get the correct output type for load functions. It should be passed the parent type to check what types from App.PageData are still required.
|
|
179
|
+
// If none, void is also allowed as a return type.
|
|
180
|
+
declarations.push(
|
|
181
|
+
`type OutputDataShape<T> = MaybeWithVoid<Omit<App.PageData, RequiredKeys<T>> & Partial<Pick<App.PageData, keyof T & keyof App.PageData>> & Record<string, any>>`
|
|
182
|
+
);
|
|
183
|
+
// null & {} == null, we need to prevent that in some situations
|
|
184
|
+
declarations.push(`type EnsureParentData<T> = NonNullable<T> extends never ? {} : T;`);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (route.leaf) {
|
|
188
|
+
const { declarations: d, exports: e, written_proxies } = process_node(route.leaf, outdir, true);
|
|
189
|
+
|
|
190
|
+
exports.push(...e);
|
|
191
|
+
declarations.push(...d);
|
|
192
|
+
|
|
193
|
+
for (const file of written_proxies) to_delete.delete(file);
|
|
194
|
+
|
|
195
|
+
if (route.leaf.server) {
|
|
196
|
+
exports.push(`export type Action = Kit.Action<RouteParams>`);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (route.layout) {
|
|
201
|
+
let all_pages_have_load = true;
|
|
202
|
+
const layout_params = new Set();
|
|
203
|
+
route.layout.child_pages?.forEach((page) => {
|
|
204
|
+
const leaf = routes.get(page);
|
|
205
|
+
if (leaf) {
|
|
206
|
+
for (const name of leaf.names) {
|
|
207
|
+
layout_params.add(name);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
if (!page.server && !page.shared) {
|
|
211
|
+
all_pages_have_load = false;
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
declarations.push(
|
|
216
|
+
`type LayoutParams = RouteParams & { ${Array.from(layout_params).map(
|
|
217
|
+
(param) => `${param}?: string`
|
|
218
|
+
)} }`
|
|
219
|
+
);
|
|
220
|
+
|
|
221
|
+
const {
|
|
222
|
+
exports: e,
|
|
223
|
+
declarations: d,
|
|
224
|
+
written_proxies
|
|
225
|
+
} = process_node(route.layout, outdir, false, all_pages_have_load);
|
|
226
|
+
|
|
227
|
+
exports.push(...e);
|
|
228
|
+
declarations.push(...d);
|
|
229
|
+
|
|
230
|
+
for (const file of written_proxies) to_delete.delete(file);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
if (route.endpoint) {
|
|
234
|
+
exports.push(`export type RequestHandler = Kit.RequestHandler<RouteParams>;`);
|
|
235
|
+
exports.push(`export type RequestEvent = Kit.RequestEvent<RouteParams>;`);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const output = [imports.join('\n'), declarations.join('\n'), exports.join('\n')]
|
|
239
|
+
.filter(Boolean)
|
|
240
|
+
.join('\n\n');
|
|
241
|
+
|
|
242
|
+
fs.writeFileSync(`${outdir}/$types.d.ts`, output);
|
|
243
|
+
to_delete.delete('$types.d.ts');
|
|
244
|
+
|
|
245
|
+
for (const file of to_delete) {
|
|
246
|
+
fs.unlinkSync(path.join(outdir, file));
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* @param {import('types').PageNode} node
|
|
252
|
+
* @param {string} outdir
|
|
253
|
+
* @param {boolean} is_page
|
|
254
|
+
* @param {boolean} [all_pages_have_load]
|
|
255
|
+
*/
|
|
256
|
+
function process_node(node, outdir, is_page, all_pages_have_load = true) {
|
|
257
|
+
const params = `${is_page ? 'Route' : 'Layout'}Params`;
|
|
258
|
+
const prefix = is_page ? 'Page' : 'Layout';
|
|
259
|
+
|
|
260
|
+
/** @type {string[]} */
|
|
261
|
+
let written_proxies = [];
|
|
262
|
+
/** @type {string[]} */
|
|
263
|
+
const declarations = [];
|
|
264
|
+
/** @type {string[]} */
|
|
265
|
+
const exports = [];
|
|
266
|
+
|
|
267
|
+
/** @type {string} */
|
|
268
|
+
let server_data;
|
|
269
|
+
/** @type {string} */
|
|
270
|
+
let data;
|
|
271
|
+
|
|
272
|
+
if (node.server) {
|
|
273
|
+
const content = fs.readFileSync(node.server, 'utf8');
|
|
274
|
+
const proxy = tweak_types(content, server_names);
|
|
275
|
+
const basename = path.basename(node.server);
|
|
276
|
+
if (proxy?.modified) {
|
|
277
|
+
fs.writeFileSync(`${outdir}/proxy${basename}`, proxy.code);
|
|
278
|
+
written_proxies.push(`proxy${basename}`);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
server_data = get_data_type(node.server, 'null', proxy);
|
|
282
|
+
|
|
283
|
+
const parent_type = `${prefix}ServerParentData`;
|
|
284
|
+
|
|
285
|
+
declarations.push(`type ${parent_type} = ${get_parent_type(node, 'LayoutServerData')};`);
|
|
286
|
+
|
|
287
|
+
// +page.js load present -> server can return all-optional data
|
|
288
|
+
const output_data_shape =
|
|
289
|
+
node.shared || (!is_page && all_pages_have_load)
|
|
290
|
+
? `Partial<App.PageData> & Record<string, any> | void`
|
|
291
|
+
: `OutputDataShape<${parent_type}>`;
|
|
292
|
+
exports.push(
|
|
293
|
+
`export type ${prefix}ServerLoad<OutputData extends ${output_data_shape} = ${output_data_shape}> = Kit.ServerLoad<${params}, ${parent_type}, OutputData>;`
|
|
294
|
+
);
|
|
295
|
+
|
|
296
|
+
exports.push(`export type ${prefix}ServerLoadEvent = Parameters<${prefix}ServerLoad>[0];`);
|
|
297
|
+
|
|
298
|
+
if (is_page) {
|
|
299
|
+
let errors = 'unknown';
|
|
300
|
+
if (proxy) {
|
|
301
|
+
const types = [];
|
|
302
|
+
for (const method of ['POST', 'PUT', 'PATCH', 'DELETE']) {
|
|
303
|
+
if (proxy.exports.includes(method)) {
|
|
304
|
+
// If the file wasn't tweaked, we can use the return type of the original file.
|
|
305
|
+
// The advantage is that type updates are reflected without saving.
|
|
306
|
+
const from = proxy.modified
|
|
307
|
+
? `./proxy${replace_ext_with_js(basename)}`
|
|
308
|
+
: path_to_original(outdir, node.server);
|
|
309
|
+
|
|
310
|
+
types.push(`Kit.AwaitedErrors<typeof import('${from}').${method}>`);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
errors = types.length ? types.join(' | ') : 'null';
|
|
314
|
+
}
|
|
315
|
+
exports.push(`export type Errors = ${errors};`);
|
|
316
|
+
}
|
|
317
|
+
} else {
|
|
318
|
+
server_data = 'null';
|
|
319
|
+
}
|
|
320
|
+
exports.push(`export type ${prefix}ServerData = ${server_data};`);
|
|
321
|
+
|
|
322
|
+
const parent_type = `${prefix}ParentData`;
|
|
323
|
+
declarations.push(`type ${parent_type} = ${get_parent_type(node, 'LayoutData')};`);
|
|
324
|
+
|
|
325
|
+
if (node.shared) {
|
|
326
|
+
const content = fs.readFileSync(node.shared, 'utf8');
|
|
327
|
+
const proxy = tweak_types(content, shared_names);
|
|
328
|
+
if (proxy?.modified) {
|
|
329
|
+
fs.writeFileSync(`${outdir}/proxy${path.basename(node.shared)}`, proxy.code);
|
|
330
|
+
written_proxies.push(`proxy${path.basename(node.shared)}`);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
const type = get_data_type(node.shared, `${parent_type} & ${prefix}ServerData`, proxy);
|
|
334
|
+
|
|
335
|
+
data = `Omit<${parent_type}, keyof ${type}> & ${type}`;
|
|
336
|
+
|
|
337
|
+
const output_data_shape =
|
|
338
|
+
!is_page && all_pages_have_load
|
|
339
|
+
? `Partial<App.PageData> & Record<string, any> | void`
|
|
340
|
+
: `OutputDataShape<${parent_type}>`;
|
|
341
|
+
exports.push(
|
|
342
|
+
`export type ${prefix}Load<OutputData extends ${output_data_shape} = ${output_data_shape}> = Kit.Load<${params}, ${prefix}ServerData, ${parent_type}, OutputData>;`
|
|
343
|
+
);
|
|
344
|
+
|
|
345
|
+
exports.push(`export type ${prefix}LoadEvent = Parameters<${prefix}Load>[0];`);
|
|
346
|
+
} else if (server_data === 'null') {
|
|
347
|
+
data = parent_type;
|
|
348
|
+
} else {
|
|
349
|
+
data = `Omit<${parent_type}, keyof ${prefix}ServerData> & ${prefix}ServerData`;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
exports.push(`export type ${prefix}Data = ${data};`);
|
|
353
|
+
|
|
354
|
+
return { declarations, exports, written_proxies };
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* @param {string} file_path
|
|
358
|
+
* @param {string} fallback
|
|
359
|
+
* @param {Proxy} proxy
|
|
360
|
+
*/
|
|
361
|
+
function get_data_type(file_path, fallback, proxy) {
|
|
362
|
+
if (proxy) {
|
|
363
|
+
if (proxy.exports.includes('load')) {
|
|
364
|
+
// If the file wasn't tweaked, we can use the return type of the original file.
|
|
365
|
+
// The advantage is that type updates are reflected without saving.
|
|
366
|
+
const from = proxy.modified
|
|
367
|
+
? `./proxy${replace_ext_with_js(path.basename(file_path))}`
|
|
368
|
+
: path_to_original(outdir, file_path);
|
|
369
|
+
return `Kit.AwaitedProperties<Awaited<ReturnType<typeof import('${from}').load>>>`;
|
|
370
|
+
} else {
|
|
371
|
+
return fallback;
|
|
372
|
+
}
|
|
373
|
+
} else {
|
|
374
|
+
return 'unknown';
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* Get the parent type string by recursively looking up the parent layout and accumulate them to one type.
|
|
381
|
+
* @param {import('types').PageNode} node
|
|
382
|
+
* @param {string} type
|
|
383
|
+
*/
|
|
384
|
+
function get_parent_type(node, type) {
|
|
385
|
+
const parent_imports = [];
|
|
386
|
+
|
|
387
|
+
let parent = node.parent;
|
|
388
|
+
|
|
389
|
+
while (parent) {
|
|
390
|
+
const d = node.depth - parent.depth;
|
|
391
|
+
// unshift because we need it the other way round for the import string
|
|
392
|
+
parent_imports.unshift(
|
|
393
|
+
`${d === 0 ? '' : `import('${'../'.repeat(d)}${'$types.js'}').`}${type}`
|
|
394
|
+
);
|
|
395
|
+
parent = parent.parent;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
let parent_str = `EnsureParentData<${parent_imports[0] || '{}'}>`;
|
|
399
|
+
for (let i = 1; i < parent_imports.length; i++) {
|
|
400
|
+
// Omit is necessary because a parent could have a property with the same key which would
|
|
401
|
+
// cause a type conflict. At runtime the child overwrites the parent property in this case,
|
|
402
|
+
// so reflect that in the type definition.
|
|
403
|
+
// EnsureParentData is necessary because {something: string} & null becomes null.
|
|
404
|
+
// Output types of server loads can be null but when passed in through the `parent` parameter they are the empty object instead.
|
|
405
|
+
parent_str = `Omit<${parent_str}, keyof ${parent_imports[i]}> & EnsureParentData<${parent_imports[i]}>`;
|
|
406
|
+
}
|
|
407
|
+
return parent_str;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* @param {string} outdir
|
|
412
|
+
* @param {string} file_path
|
|
413
|
+
*/
|
|
414
|
+
function path_to_original(outdir, file_path) {
|
|
415
|
+
return posixify(path.relative(outdir, path.join(cwd, replace_ext_with_js(file_path))));
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* @param {string} file_path
|
|
420
|
+
*/
|
|
421
|
+
function replace_ext_with_js(file_path) {
|
|
422
|
+
// Another extension than `.js` (or nothing, but that fails with node16 moduleResolution)
|
|
423
|
+
// will result in TS failing to lookup the file
|
|
424
|
+
const ext = path.extname(file_path);
|
|
425
|
+
return file_path.slice(0, -ext.length) + '.js';
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* @param {string} content
|
|
430
|
+
* @param {Set<string>} names
|
|
431
|
+
* @returns {Proxy}
|
|
432
|
+
*/
|
|
433
|
+
export function tweak_types(content, names) {
|
|
434
|
+
try {
|
|
435
|
+
let modified = false;
|
|
436
|
+
|
|
437
|
+
const ast = ts.createSourceFile(
|
|
438
|
+
'filename.ts',
|
|
439
|
+
content,
|
|
440
|
+
ts.ScriptTarget.Latest,
|
|
441
|
+
false,
|
|
442
|
+
ts.ScriptKind.TS
|
|
443
|
+
);
|
|
444
|
+
|
|
445
|
+
const code = new MagicString(content);
|
|
446
|
+
|
|
447
|
+
const exports = new Map();
|
|
448
|
+
|
|
449
|
+
ast.forEachChild((node) => {
|
|
450
|
+
if (
|
|
451
|
+
ts.isExportDeclaration(node) &&
|
|
452
|
+
node.exportClause &&
|
|
453
|
+
ts.isNamedExports(node.exportClause)
|
|
454
|
+
) {
|
|
455
|
+
node.exportClause.elements.forEach((element) => {
|
|
456
|
+
const exported = element.name;
|
|
457
|
+
if (names.has(element.name.text)) {
|
|
458
|
+
const local = element.propertyName || element.name;
|
|
459
|
+
exports.set(exported.text, local.text);
|
|
460
|
+
}
|
|
461
|
+
});
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
if (node.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword)) {
|
|
465
|
+
if (ts.isFunctionDeclaration(node) && node.name?.text && names.has(node.name?.text)) {
|
|
466
|
+
exports.set(node.name.text, node.name.text);
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
if (ts.isVariableStatement(node)) {
|
|
470
|
+
node.declarationList.declarations.forEach((declaration) => {
|
|
471
|
+
if (ts.isIdentifier(declaration.name) && names.has(declaration.name.text)) {
|
|
472
|
+
exports.set(declaration.name.text, declaration.name.text);
|
|
473
|
+
}
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
});
|
|
478
|
+
|
|
479
|
+
/**
|
|
480
|
+
* @param {import('typescript').Node} node
|
|
481
|
+
* @param {import('typescript').Node} value
|
|
482
|
+
*/
|
|
483
|
+
function replace_jsdoc_type_tags(node, value) {
|
|
484
|
+
// @ts-ignore
|
|
485
|
+
if (node.jsDoc) {
|
|
486
|
+
// @ts-ignore
|
|
487
|
+
for (const comment of node.jsDoc) {
|
|
488
|
+
for (const tag of comment.tags) {
|
|
489
|
+
if (ts.isJSDocTypeTag(tag)) {
|
|
490
|
+
const is_fn =
|
|
491
|
+
ts.isFunctionDeclaration(value) ||
|
|
492
|
+
ts.isFunctionExpression(value) ||
|
|
493
|
+
ts.isArrowFunction(value);
|
|
494
|
+
|
|
495
|
+
if (is_fn && value.parameters?.length > 0) {
|
|
496
|
+
code.overwrite(tag.tagName.pos, tag.tagName.end, 'param');
|
|
497
|
+
code.prependRight(tag.typeExpression.pos + 1, 'Parameters<');
|
|
498
|
+
code.appendLeft(tag.typeExpression.end - 1, '>[0]');
|
|
499
|
+
code.appendLeft(tag.typeExpression.end, ' event');
|
|
500
|
+
} else {
|
|
501
|
+
code.overwrite(tag.pos, tag.end, '');
|
|
502
|
+
}
|
|
503
|
+
modified = true;
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
ast.forEachChild((node) => {
|
|
511
|
+
if (ts.isFunctionDeclaration(node) && node.name?.text && names.has(node.name?.text)) {
|
|
512
|
+
// remove JSDoc comment above `export function load ...`
|
|
513
|
+
replace_jsdoc_type_tags(node, node);
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
if (ts.isVariableStatement(node)) {
|
|
517
|
+
// remove JSDoc comment above `export const load = ...`
|
|
518
|
+
if (
|
|
519
|
+
ts.isIdentifier(node.declarationList.declarations[0].name) &&
|
|
520
|
+
names.has(node.declarationList.declarations[0].name.text) &&
|
|
521
|
+
node.declarationList.declarations[0].initializer
|
|
522
|
+
) {
|
|
523
|
+
replace_jsdoc_type_tags(node, node.declarationList.declarations[0].initializer);
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
for (const declaration of node.declarationList.declarations) {
|
|
527
|
+
if (
|
|
528
|
+
ts.isIdentifier(declaration.name) &&
|
|
529
|
+
names.has(declaration.name.text) &&
|
|
530
|
+
declaration.initializer
|
|
531
|
+
) {
|
|
532
|
+
// edge case — remove JSDoc comment above individual export
|
|
533
|
+
replace_jsdoc_type_tags(declaration, declaration.initializer);
|
|
534
|
+
|
|
535
|
+
// remove type from `export const load: Load ...`
|
|
536
|
+
if (declaration.type) {
|
|
537
|
+
let a = declaration.type.pos;
|
|
538
|
+
let b = declaration.type.end;
|
|
539
|
+
while (/\s/.test(content[a])) a += 1;
|
|
540
|
+
|
|
541
|
+
const type = content.slice(a, b);
|
|
542
|
+
code.remove(declaration.name.end, declaration.type.end);
|
|
543
|
+
|
|
544
|
+
const rhs = declaration.initializer;
|
|
545
|
+
|
|
546
|
+
if (
|
|
547
|
+
rhs &&
|
|
548
|
+
(ts.isArrowFunction(rhs) || ts.isFunctionExpression(rhs)) &&
|
|
549
|
+
rhs.parameters.length
|
|
550
|
+
) {
|
|
551
|
+
const arg = rhs.parameters[0];
|
|
552
|
+
|
|
553
|
+
const add_parens = content[arg.pos - 1] !== '(';
|
|
554
|
+
|
|
555
|
+
if (add_parens) code.prependRight(arg.pos, '(');
|
|
556
|
+
|
|
557
|
+
if (arg && !arg.type) {
|
|
558
|
+
code.appendLeft(
|
|
559
|
+
arg.name.end,
|
|
560
|
+
`: Parameters<${type}>[0]` + (add_parens ? ')' : '')
|
|
561
|
+
);
|
|
562
|
+
} else {
|
|
563
|
+
// prevent "type X is imported but not used" (isn't silenced by @ts-nocheck) when svelte-check runs
|
|
564
|
+
code.append(`;null as any as ${type};`);
|
|
565
|
+
}
|
|
566
|
+
} else {
|
|
567
|
+
// prevent "type X is imported but not used" (isn't silenced by @ts-nocheck) when svelte-check runs
|
|
568
|
+
code.append(`;null as any as ${type};`);
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
modified = true;
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
});
|
|
577
|
+
|
|
578
|
+
if (modified) {
|
|
579
|
+
// Ignore all type errors so they don't show up twice when svelte-check runs
|
|
580
|
+
// Account for possible @ts-check which would overwrite @ts-nocheck
|
|
581
|
+
if (code.original.startsWith('// @ts-check')) {
|
|
582
|
+
code.prependLeft('// @ts-check'.length, '\n// @ts-nocheck\n');
|
|
583
|
+
} else {
|
|
584
|
+
code.prepend('// @ts-nocheck\n');
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
return {
|
|
589
|
+
modified,
|
|
590
|
+
code: code.toString(),
|
|
591
|
+
exports: Array.from(exports.keys())
|
|
592
|
+
};
|
|
593
|
+
} catch {
|
|
594
|
+
return null;
|
|
595
|
+
}
|
|
596
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import colors from 'kleur';
|
|
3
|
+
import { fileURLToPath } from 'url';
|
|
4
|
+
import { posixify } from '../utils/filesystem.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Resolved path of the `runtime` directory
|
|
8
|
+
*
|
|
9
|
+
* TODO Windows issue:
|
|
10
|
+
* Vite or sth else somehow sets the driver letter inconsistently to lower or upper case depending on the run environment.
|
|
11
|
+
* In playwright debug mode run through VS Code this a root-to-lowercase conversion is needed in order for the tests to run.
|
|
12
|
+
* If we do this conversion in other cases it has the opposite effect though and fails.
|
|
13
|
+
*/
|
|
14
|
+
export const runtime_directory = posixify(fileURLToPath(new URL('../runtime', import.meta.url)));
|
|
15
|
+
|
|
16
|
+
/** Prefix for the `runtime` directory, for use with import declarations */
|
|
17
|
+
export const runtime_prefix = posixify_path(runtime_directory);
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* This allows us to import SvelteKit internals that aren't exposed via `pkg.exports` in a
|
|
21
|
+
* way that works whether `@sveltejs/kit` is installed inside the project's `node_modules`
|
|
22
|
+
* or in a workspace root
|
|
23
|
+
*/
|
|
24
|
+
export const runtime_base = runtime_directory.startsWith(process.cwd())
|
|
25
|
+
? `/${path.relative('.', runtime_directory)}`
|
|
26
|
+
: `/@fs${
|
|
27
|
+
// Windows/Linux separation - Windows starts with a drive letter, we need a / in front there
|
|
28
|
+
runtime_directory.startsWith('/') ? '' : '/'
|
|
29
|
+
}${runtime_directory}`;
|
|
30
|
+
|
|
31
|
+
/** @param {string} str */
|
|
32
|
+
function posixify_path(str) {
|
|
33
|
+
const parsed = path.parse(str);
|
|
34
|
+
return `/${parsed.dir.slice(parsed.root.length).split(path.sep).join('/')}/${parsed.base}`;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function noop() {}
|
|
38
|
+
|
|
39
|
+
/** @param {{ verbose: boolean }} opts */
|
|
40
|
+
export function logger({ verbose }) {
|
|
41
|
+
/** @type {import('types').Logger} */
|
|
42
|
+
const log = (msg) => console.log(msg.replace(/^/gm, ' '));
|
|
43
|
+
|
|
44
|
+
/** @param {string} msg */
|
|
45
|
+
const err = (msg) => console.error(msg.replace(/^/gm, ' '));
|
|
46
|
+
|
|
47
|
+
log.success = (msg) => log(colors.green(`✔ ${msg}`));
|
|
48
|
+
log.error = (msg) => err(colors.bold().red(msg));
|
|
49
|
+
log.warn = (msg) => log(colors.bold().yellow(msg));
|
|
50
|
+
|
|
51
|
+
log.minor = verbose ? (msg) => log(colors.grey(msg)) : noop;
|
|
52
|
+
log.info = verbose ? log : noop;
|
|
53
|
+
|
|
54
|
+
return log;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** @param {import('types').ManifestData} manifest_data */
|
|
58
|
+
export function get_mime_lookup(manifest_data) {
|
|
59
|
+
/** @type {Record<string, string>} */
|
|
60
|
+
const mime = {};
|
|
61
|
+
|
|
62
|
+
manifest_data.assets.forEach((asset) => {
|
|
63
|
+
if (asset.type) {
|
|
64
|
+
const ext = path.extname(asset.file);
|
|
65
|
+
mime[ext] = asset.type;
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
return mime;
|
|
70
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { sequence } from './sequence.js';
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param {...import('types').Handle} handlers
|
|
3
|
+
* @returns {import('types').Handle}
|
|
4
|
+
*/
|
|
5
|
+
export function sequence(...handlers) {
|
|
6
|
+
const length = handlers.length;
|
|
7
|
+
if (!length) return ({ event, resolve }) => resolve(event);
|
|
8
|
+
|
|
9
|
+
return ({ event, resolve }) => {
|
|
10
|
+
return apply_handle(0, event);
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @param {number} i
|
|
14
|
+
* @param {import('types').RequestEvent} event
|
|
15
|
+
* @returns {import('types').MaybePromise<Response>}
|
|
16
|
+
*/
|
|
17
|
+
function apply_handle(i, event) {
|
|
18
|
+
const handle = handlers[i];
|
|
19
|
+
|
|
20
|
+
return handle({
|
|
21
|
+
event,
|
|
22
|
+
resolve: i < length - 1 ? (event) => apply_handle(i + 1, event) : resolve
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
}
|