@sveltejs/kit 1.0.0-next.50 → 1.0.0-next.500
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 +94 -67
- package/postinstall.js +38 -0
- package/scripts/special-types/$env+dynamic+private.md +10 -0
- package/scripts/special-types/$env+dynamic+public.md +8 -0
- package/scripts/special-types/$env+static+private.md +19 -0
- package/scripts/special-types/$env+static+public.md +7 -0
- package/scripts/special-types/$lib.md +5 -0
- package/src/cli.js +108 -0
- package/src/constants.js +7 -0
- package/src/core/adapt/builder.js +206 -0
- package/src/core/adapt/index.js +31 -0
- package/src/core/config/default-error.html +56 -0
- package/src/core/config/index.js +110 -0
- package/src/core/config/options.js +504 -0
- package/src/core/config/types.d.ts +1 -0
- package/src/core/env.js +121 -0
- package/src/core/generate_manifest/index.js +93 -0
- package/src/core/prerender/crawl.js +198 -0
- package/src/core/prerender/entities.js +2252 -0
- package/src/core/prerender/prerender.js +431 -0
- package/src/core/prerender/queue.js +80 -0
- package/src/core/sync/create_manifest_data/index.js +488 -0
- package/src/core/sync/create_manifest_data/types.d.ts +37 -0
- package/src/core/sync/sync.js +70 -0
- package/src/core/sync/utils.js +33 -0
- package/src/core/sync/write_ambient.js +53 -0
- package/src/core/sync/write_client_manifest.js +106 -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 +678 -0
- package/src/core/utils.js +70 -0
- package/src/exports/hooks/index.js +1 -0
- package/src/exports/hooks/sequence.js +44 -0
- package/src/exports/index.js +45 -0
- package/src/exports/node/index.js +173 -0
- package/src/exports/node/polyfills.js +41 -0
- package/src/exports/vite/build/build_server.js +378 -0
- package/src/exports/vite/build/build_service_worker.js +90 -0
- package/src/exports/vite/build/utils.js +180 -0
- package/src/exports/vite/dev/index.js +577 -0
- package/src/exports/vite/graph_analysis/index.js +277 -0
- package/src/exports/vite/graph_analysis/types.d.ts +5 -0
- package/src/exports/vite/graph_analysis/utils.js +30 -0
- package/src/exports/vite/index.js +598 -0
- package/src/exports/vite/preview/index.js +189 -0
- package/src/exports/vite/types.d.ts +3 -0
- package/src/exports/vite/utils.js +157 -0
- package/src/runtime/app/env.js +1 -0
- package/src/runtime/app/environment.js +11 -0
- package/src/runtime/app/forms.js +114 -0
- package/src/runtime/app/navigation.js +23 -0
- package/src/runtime/app/paths.js +1 -0
- package/src/runtime/app/stores.js +102 -0
- package/src/runtime/client/ambient.d.ts +26 -0
- package/src/runtime/client/client.js +1583 -0
- package/src/runtime/client/fetcher.js +107 -0
- package/src/runtime/client/parse.js +60 -0
- package/src/runtime/client/singletons.js +21 -0
- package/src/runtime/client/start.js +37 -0
- package/src/runtime/client/types.d.ts +84 -0
- package/src/runtime/client/utils.js +159 -0
- package/src/runtime/components/error.svelte +16 -0
- package/{assets → src/runtime}/components/layout.svelte +0 -0
- package/src/runtime/control.js +98 -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 +6 -0
- package/src/runtime/env-public.js +6 -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/cookie.js +127 -0
- package/src/runtime/server/data/index.js +136 -0
- package/src/runtime/server/endpoint.js +90 -0
- package/src/runtime/server/index.js +340 -0
- package/src/runtime/server/page/actions.js +243 -0
- package/src/runtime/server/page/crypto.js +239 -0
- package/src/runtime/server/page/csp.js +250 -0
- package/src/runtime/server/page/fetch.js +301 -0
- package/src/runtime/server/page/index.js +304 -0
- package/src/runtime/server/page/load_data.js +124 -0
- package/src/runtime/server/page/render.js +342 -0
- package/src/runtime/server/page/respond_with_error.js +104 -0
- package/src/runtime/server/page/serialize_data.js +87 -0
- package/src/runtime/server/page/types.d.ts +41 -0
- package/src/runtime/server/utils.js +179 -0
- package/src/utils/array.js +9 -0
- package/src/utils/error.js +22 -0
- package/src/utils/escape.js +46 -0
- package/src/utils/filesystem.js +137 -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 +117 -0
- package/src/utils/unit_test.js +11 -0
- package/src/utils/url.js +142 -0
- package/svelte-kit.js +1 -1
- package/types/ambient.d.ts +426 -0
- package/types/index.d.ts +444 -0
- package/types/internal.d.ts +378 -0
- package/types/private.d.ts +224 -0
- package/CHANGELOG.md +0 -484
- package/assets/components/error.svelte +0 -13
- package/assets/runtime/app/env.js +0 -5
- package/assets/runtime/app/navigation.js +0 -44
- package/assets/runtime/app/paths.js +0 -1
- package/assets/runtime/app/stores.js +0 -93
- package/assets/runtime/chunks/utils.js +0 -22
- package/assets/runtime/internal/singletons.js +0 -23
- package/assets/runtime/internal/start.js +0 -779
- package/assets/runtime/paths.js +0 -12
- package/dist/chunks/index.js +0 -3516
- package/dist/chunks/index2.js +0 -587
- package/dist/chunks/index3.js +0 -246
- package/dist/chunks/index4.js +0 -528
- package/dist/chunks/index5.js +0 -763
- 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 -550
- package/dist/ssr.js +0 -2588
- package/types.d.ts +0 -89
- package/types.internal.d.ts +0 -191
|
@@ -0,0 +1,678 @@
|
|
|
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
|
+
/**
|
|
25
|
+
* Creates types for the whole manifest
|
|
26
|
+
* @param {import('types').ValidatedConfig} config
|
|
27
|
+
* @param {import('types').ManifestData} manifest_data
|
|
28
|
+
*/
|
|
29
|
+
export async function write_all_types(config, manifest_data) {
|
|
30
|
+
if (!ts) return;
|
|
31
|
+
|
|
32
|
+
const types_dir = `${config.kit.outDir}/types`;
|
|
33
|
+
|
|
34
|
+
// empty out files that no longer need to exist
|
|
35
|
+
const routes_dir = path.relative('.', config.kit.files.routes);
|
|
36
|
+
const expected_directories = new Set(
|
|
37
|
+
manifest_data.routes.map((route) => path.join(routes_dir, route.id))
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
if (fs.existsSync(types_dir)) {
|
|
41
|
+
for (const file of walk(types_dir)) {
|
|
42
|
+
const dir = path.dirname(file);
|
|
43
|
+
if (!expected_directories.has(dir)) {
|
|
44
|
+
rimraf(file);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const routes_map = create_routes_map(manifest_data);
|
|
50
|
+
// For each directory, write $types.d.ts
|
|
51
|
+
for (const route of manifest_data.routes) {
|
|
52
|
+
update_types(config, routes_map, route);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Creates types related to the given file. This should only be called
|
|
58
|
+
* if the file in question was edited, not if it was created/deleted/moved.
|
|
59
|
+
* @param {import('types').ValidatedConfig} config
|
|
60
|
+
* @param {import('types').ManifestData} manifest_data
|
|
61
|
+
* @param {string} file
|
|
62
|
+
*/
|
|
63
|
+
export async function write_types(config, manifest_data, file) {
|
|
64
|
+
if (!ts) return;
|
|
65
|
+
|
|
66
|
+
if (!path.basename(file).startsWith('+')) {
|
|
67
|
+
// Not a route file
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const id = posixify(path.relative(config.kit.files.routes, path.dirname(file)));
|
|
72
|
+
|
|
73
|
+
const route = manifest_data.routes.find((route) => route.id === id);
|
|
74
|
+
if (!route) return; // this shouldn't ever happen
|
|
75
|
+
|
|
76
|
+
update_types(config, create_routes_map(manifest_data), route);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Collect all leafs into a leaf -> route map
|
|
81
|
+
* @param {import('types').ManifestData} manifest_data
|
|
82
|
+
*/
|
|
83
|
+
function create_routes_map(manifest_data) {
|
|
84
|
+
/** @type {Map<import('types').PageNode, import('types').RouteData>} */
|
|
85
|
+
const map = new Map();
|
|
86
|
+
for (const route of manifest_data.routes) {
|
|
87
|
+
if (route.leaf) {
|
|
88
|
+
map.set(route.leaf, route);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return map;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Update types for a specific route
|
|
96
|
+
* @param {import('types').ValidatedConfig} config
|
|
97
|
+
* @param {Map<import('types').PageNode, import('types').RouteData>} routes
|
|
98
|
+
* @param {import('types').RouteData} route
|
|
99
|
+
*/
|
|
100
|
+
function update_types(config, routes, route) {
|
|
101
|
+
if (!route.leaf && !route.layout && !route.endpoint) return; // nothing to do
|
|
102
|
+
|
|
103
|
+
const routes_dir = posixify(path.relative('.', config.kit.files.routes));
|
|
104
|
+
const outdir = path.join(config.kit.outDir, 'types', routes_dir, route.id);
|
|
105
|
+
|
|
106
|
+
// first, check if the types are out of date
|
|
107
|
+
const input_files = [];
|
|
108
|
+
|
|
109
|
+
/** @type {import('types').PageNode | null} */
|
|
110
|
+
let node = route.leaf;
|
|
111
|
+
while (node) {
|
|
112
|
+
if (node.shared) input_files.push(node.shared);
|
|
113
|
+
if (node.server) input_files.push(node.server);
|
|
114
|
+
node = node.parent ?? null;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** @type {import('types').PageNode | null} */
|
|
118
|
+
node = route.layout;
|
|
119
|
+
while (node) {
|
|
120
|
+
if (node.shared) input_files.push(node.shared);
|
|
121
|
+
if (node.server) input_files.push(node.server);
|
|
122
|
+
node = node.parent ?? null;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (route.endpoint) {
|
|
126
|
+
input_files.push(route.endpoint.file);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
try {
|
|
130
|
+
fs.mkdirSync(outdir, { recursive: true });
|
|
131
|
+
} catch {}
|
|
132
|
+
|
|
133
|
+
const output_files = compact(
|
|
134
|
+
fs.readdirSync(outdir).map((name) => {
|
|
135
|
+
const stats = fs.statSync(path.join(outdir, name));
|
|
136
|
+
if (stats.isDirectory()) return;
|
|
137
|
+
return {
|
|
138
|
+
name,
|
|
139
|
+
updated: stats.mtimeMs
|
|
140
|
+
};
|
|
141
|
+
})
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
const source_last_updated = Math.max(...input_files.map((file) => fs.statSync(file).mtimeMs));
|
|
145
|
+
const types_last_updated = Math.max(...output_files.map((file) => file?.updated));
|
|
146
|
+
|
|
147
|
+
// types were generated more recently than the source files, so don't regenerate
|
|
148
|
+
if (types_last_updated > source_last_updated) return;
|
|
149
|
+
|
|
150
|
+
// track which old files end up being surplus to requirements
|
|
151
|
+
const to_delete = new Set(output_files.map((file) => file.name));
|
|
152
|
+
|
|
153
|
+
// now generate new types
|
|
154
|
+
const imports = [`import type * as Kit from '@sveltejs/kit';`];
|
|
155
|
+
|
|
156
|
+
/** @type {string[]} */
|
|
157
|
+
const declarations = [];
|
|
158
|
+
|
|
159
|
+
/** @type {string[]} */
|
|
160
|
+
const exports = [];
|
|
161
|
+
|
|
162
|
+
// add 'Expand' helper
|
|
163
|
+
// Makes sure a type is "repackaged" and therefore more readable
|
|
164
|
+
declarations.push('type Expand<T> = T extends infer O ? { [K in keyof O]: O[K] } : never;');
|
|
165
|
+
declarations.push(
|
|
166
|
+
`type RouteParams = { ${route.names.map((param) => `${param}: string`).join('; ')} }`
|
|
167
|
+
);
|
|
168
|
+
|
|
169
|
+
// 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
|
|
170
|
+
if (route.layout || route.leaf) {
|
|
171
|
+
// If T extends the empty object, void is also allowed as a return type
|
|
172
|
+
declarations.push(`type MaybeWithVoid<T> = {} extends T ? T | void : T;`);
|
|
173
|
+
// Returns the key of the object whose values are required.
|
|
174
|
+
declarations.push(
|
|
175
|
+
`export type RequiredKeys<T> = { [K in keyof T]-?: {} extends { [P in K]: T[K] } ? never : K; }[keyof T];`
|
|
176
|
+
);
|
|
177
|
+
// 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.
|
|
178
|
+
// If none, void is also allowed as a return type.
|
|
179
|
+
declarations.push(
|
|
180
|
+
`type OutputDataShape<T> = MaybeWithVoid<Omit<App.PageData, RequiredKeys<T>> & Partial<Pick<App.PageData, keyof T & keyof App.PageData>> & Record<string, any>>`
|
|
181
|
+
);
|
|
182
|
+
// null & {} == null, we need to prevent that in some situations
|
|
183
|
+
declarations.push(`type EnsureParentData<T> = T extends null | undefined ? {} : T;`);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (route.leaf) {
|
|
187
|
+
const { declarations: d, exports: e, written_proxies } = process_node(route.leaf, outdir, true);
|
|
188
|
+
|
|
189
|
+
exports.push(...e);
|
|
190
|
+
declarations.push(...d);
|
|
191
|
+
|
|
192
|
+
for (const file of written_proxies) to_delete.delete(file);
|
|
193
|
+
|
|
194
|
+
if (route.leaf.server) {
|
|
195
|
+
exports.push(`export type Action = Kit.Action<RouteParams>`);
|
|
196
|
+
exports.push(`export type Actions = Kit.Actions<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
|
+
}
|
|
236
|
+
|
|
237
|
+
if (route.leaf?.server || route.endpoint) {
|
|
238
|
+
exports.push(`export type RequestEvent = Kit.RequestEvent<RouteParams>;`);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
const output = [imports.join('\n'), declarations.join('\n'), exports.join('\n')]
|
|
242
|
+
.filter(Boolean)
|
|
243
|
+
.join('\n\n');
|
|
244
|
+
|
|
245
|
+
fs.writeFileSync(`${outdir}/$types.d.ts`, output);
|
|
246
|
+
to_delete.delete('$types.d.ts');
|
|
247
|
+
|
|
248
|
+
for (const file of to_delete) {
|
|
249
|
+
fs.unlinkSync(path.join(outdir, file));
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* @param {import('types').PageNode} node
|
|
255
|
+
* @param {string} outdir
|
|
256
|
+
* @param {boolean} is_page
|
|
257
|
+
* @param {boolean} [all_pages_have_load]
|
|
258
|
+
*/
|
|
259
|
+
function process_node(node, outdir, is_page, all_pages_have_load = true) {
|
|
260
|
+
const params = `${is_page ? 'Route' : 'Layout'}Params`;
|
|
261
|
+
const prefix = is_page ? 'Page' : 'Layout';
|
|
262
|
+
|
|
263
|
+
/** @type {string[]} */
|
|
264
|
+
let written_proxies = [];
|
|
265
|
+
/** @type {string[]} */
|
|
266
|
+
const declarations = [];
|
|
267
|
+
/** @type {string[]} */
|
|
268
|
+
const exports = [];
|
|
269
|
+
|
|
270
|
+
/** @type {string} */
|
|
271
|
+
let server_data;
|
|
272
|
+
/** @type {string} */
|
|
273
|
+
let data;
|
|
274
|
+
|
|
275
|
+
if (node.server) {
|
|
276
|
+
const content = fs.readFileSync(node.server, 'utf8');
|
|
277
|
+
const proxy = tweak_types(content, true);
|
|
278
|
+
const basename = path.basename(node.server);
|
|
279
|
+
if (proxy?.modified) {
|
|
280
|
+
fs.writeFileSync(`${outdir}/proxy${basename}`, proxy.code);
|
|
281
|
+
written_proxies.push(`proxy${basename}`);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
server_data = get_data_type(node.server, 'null', proxy, true);
|
|
285
|
+
|
|
286
|
+
const parent_type = `${prefix}ServerParentData`;
|
|
287
|
+
|
|
288
|
+
declarations.push(`type ${parent_type} = ${get_parent_type(node, 'LayoutServerData')};`);
|
|
289
|
+
|
|
290
|
+
// +page.js load present -> server can return all-optional data
|
|
291
|
+
const output_data_shape =
|
|
292
|
+
node.shared || (!is_page && all_pages_have_load)
|
|
293
|
+
? `Partial<App.PageData> & Record<string, any> | void`
|
|
294
|
+
: `OutputDataShape<${parent_type}>`;
|
|
295
|
+
exports.push(
|
|
296
|
+
`export type ${prefix}ServerLoad<OutputData extends ${output_data_shape} = ${output_data_shape}> = Kit.ServerLoad<${params}, ${parent_type}, OutputData>;`
|
|
297
|
+
);
|
|
298
|
+
|
|
299
|
+
exports.push(`export type ${prefix}ServerLoadEvent = Parameters<${prefix}ServerLoad>[0];`);
|
|
300
|
+
|
|
301
|
+
if (is_page) {
|
|
302
|
+
let type = 'unknown';
|
|
303
|
+
if (proxy) {
|
|
304
|
+
if (proxy.exports.includes('actions')) {
|
|
305
|
+
// If the file wasn't tweaked, we can use the return type of the original file.
|
|
306
|
+
// The advantage is that type updates are reflected without saving.
|
|
307
|
+
const from = proxy.modified
|
|
308
|
+
? `./proxy${replace_ext_with_js(basename)}`
|
|
309
|
+
: path_to_original(outdir, node.server);
|
|
310
|
+
|
|
311
|
+
type = `Expand<Kit.AwaitedActions<typeof import('${from}').actions>>`;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
exports.push(`export type ActionData = ${type};`);
|
|
315
|
+
}
|
|
316
|
+
} else {
|
|
317
|
+
server_data = 'null';
|
|
318
|
+
}
|
|
319
|
+
exports.push(`export type ${prefix}ServerData = ${server_data};`);
|
|
320
|
+
|
|
321
|
+
const parent_type = `${prefix}ParentData`;
|
|
322
|
+
declarations.push(`type ${parent_type} = ${get_parent_type(node, 'LayoutData')};`);
|
|
323
|
+
|
|
324
|
+
if (node.shared) {
|
|
325
|
+
const content = fs.readFileSync(node.shared, 'utf8');
|
|
326
|
+
const proxy = tweak_types(content, false);
|
|
327
|
+
if (proxy?.modified) {
|
|
328
|
+
fs.writeFileSync(`${outdir}/proxy${path.basename(node.shared)}`, proxy.code);
|
|
329
|
+
written_proxies.push(`proxy${path.basename(node.shared)}`);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
const type = get_data_type(node.shared, `${parent_type} & ${prefix}ServerData`, proxy);
|
|
333
|
+
|
|
334
|
+
data = `Expand<Omit<${parent_type}, keyof ${type}> & ${type}>`;
|
|
335
|
+
|
|
336
|
+
const output_data_shape =
|
|
337
|
+
!is_page && all_pages_have_load
|
|
338
|
+
? `Partial<App.PageData> & Record<string, any> | void`
|
|
339
|
+
: `OutputDataShape<${parent_type}>`;
|
|
340
|
+
exports.push(
|
|
341
|
+
`export type ${prefix}Load<OutputData extends ${output_data_shape} = ${output_data_shape}> = Kit.Load<${params}, ${prefix}ServerData, ${parent_type}, OutputData>;`
|
|
342
|
+
);
|
|
343
|
+
|
|
344
|
+
exports.push(`export type ${prefix}LoadEvent = Parameters<${prefix}Load>[0];`);
|
|
345
|
+
} else if (server_data === 'null') {
|
|
346
|
+
data = `Expand<${parent_type}>`;
|
|
347
|
+
} else {
|
|
348
|
+
data = `Expand<Omit<${parent_type}, keyof ${prefix}ServerData> & ${prefix}ServerData>`;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
exports.push(`export type ${prefix}Data = ${data};`);
|
|
352
|
+
|
|
353
|
+
return { declarations, exports, written_proxies };
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* @param {string} file_path
|
|
357
|
+
* @param {string} fallback
|
|
358
|
+
* @param {Proxy} proxy
|
|
359
|
+
* @param {boolean} expand
|
|
360
|
+
*/
|
|
361
|
+
function get_data_type(file_path, fallback, proxy, expand = false) {
|
|
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
|
+
const type = `Kit.AwaitedProperties<Awaited<ReturnType<typeof import('${from}').load>>>`;
|
|
370
|
+
return expand ? `Expand<${type}>` : type;
|
|
371
|
+
} else {
|
|
372
|
+
return fallback;
|
|
373
|
+
}
|
|
374
|
+
} else {
|
|
375
|
+
return 'unknown';
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* Get the parent type string by recursively looking up the parent layout and accumulate them to one type.
|
|
382
|
+
* @param {import('types').PageNode} node
|
|
383
|
+
* @param {string} type
|
|
384
|
+
*/
|
|
385
|
+
function get_parent_type(node, type) {
|
|
386
|
+
const parent_imports = [];
|
|
387
|
+
|
|
388
|
+
let parent = node.parent;
|
|
389
|
+
|
|
390
|
+
while (parent) {
|
|
391
|
+
const d = node.depth - parent.depth;
|
|
392
|
+
// unshift because we need it the other way round for the import string
|
|
393
|
+
parent_imports.unshift(
|
|
394
|
+
`${d === 0 ? '' : `import('${'../'.repeat(d)}${'$types.js'}').`}${type}`
|
|
395
|
+
);
|
|
396
|
+
parent = parent.parent;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
let parent_str = `EnsureParentData<${parent_imports[0] || '{}'}>`;
|
|
400
|
+
for (let i = 1; i < parent_imports.length; i++) {
|
|
401
|
+
// Omit is necessary because a parent could have a property with the same key which would
|
|
402
|
+
// cause a type conflict. At runtime the child overwrites the parent property in this case,
|
|
403
|
+
// so reflect that in the type definition.
|
|
404
|
+
// EnsureParentData is necessary because {something: string} & null becomes null.
|
|
405
|
+
// Output types of server loads can be null but when passed in through the `parent` parameter they are the empty object instead.
|
|
406
|
+
parent_str = `Omit<${parent_str}, keyof ${parent_imports[i]}> & EnsureParentData<${parent_imports[i]}>`;
|
|
407
|
+
}
|
|
408
|
+
return parent_str;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* @param {string} outdir
|
|
413
|
+
* @param {string} file_path
|
|
414
|
+
*/
|
|
415
|
+
function path_to_original(outdir, file_path) {
|
|
416
|
+
return posixify(path.relative(outdir, path.join(cwd, replace_ext_with_js(file_path))));
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* @param {string} file_path
|
|
421
|
+
*/
|
|
422
|
+
function replace_ext_with_js(file_path) {
|
|
423
|
+
// Another extension than `.js` (or nothing, but that fails with node16 moduleResolution)
|
|
424
|
+
// will result in TS failing to lookup the file
|
|
425
|
+
const ext = path.extname(file_path);
|
|
426
|
+
return file_path.slice(0, -ext.length) + '.js';
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* @param {string} content
|
|
431
|
+
* @param {boolean} is_server
|
|
432
|
+
* @returns {Proxy}
|
|
433
|
+
*/
|
|
434
|
+
export function tweak_types(content, is_server) {
|
|
435
|
+
const names = new Set(is_server ? ['load', 'actions'] : ['load']);
|
|
436
|
+
|
|
437
|
+
try {
|
|
438
|
+
let modified = false;
|
|
439
|
+
|
|
440
|
+
const ast = ts.createSourceFile(
|
|
441
|
+
'filename.ts',
|
|
442
|
+
content,
|
|
443
|
+
ts.ScriptTarget.Latest,
|
|
444
|
+
false,
|
|
445
|
+
ts.ScriptKind.TS
|
|
446
|
+
);
|
|
447
|
+
|
|
448
|
+
const code = new MagicString(content);
|
|
449
|
+
|
|
450
|
+
const exports = new Map();
|
|
451
|
+
|
|
452
|
+
ast.forEachChild((node) => {
|
|
453
|
+
if (
|
|
454
|
+
ts.isExportDeclaration(node) &&
|
|
455
|
+
node.exportClause &&
|
|
456
|
+
ts.isNamedExports(node.exportClause)
|
|
457
|
+
) {
|
|
458
|
+
node.exportClause.elements.forEach((element) => {
|
|
459
|
+
const exported = element.name;
|
|
460
|
+
if (names.has(element.name.text)) {
|
|
461
|
+
const local = element.propertyName || element.name;
|
|
462
|
+
exports.set(exported.text, local.text);
|
|
463
|
+
}
|
|
464
|
+
});
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
if (node.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword)) {
|
|
468
|
+
if (ts.isFunctionDeclaration(node) && node.name?.text && names.has(node.name?.text)) {
|
|
469
|
+
exports.set(node.name.text, node.name.text);
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
if (ts.isVariableStatement(node)) {
|
|
473
|
+
node.declarationList.declarations.forEach((declaration) => {
|
|
474
|
+
if (ts.isIdentifier(declaration.name) && names.has(declaration.name.text)) {
|
|
475
|
+
exports.set(declaration.name.text, declaration.name.text);
|
|
476
|
+
}
|
|
477
|
+
});
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
});
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* @param {import('typescript').Node} node
|
|
484
|
+
* @param {import('typescript').Node} value
|
|
485
|
+
*/
|
|
486
|
+
function replace_jsdoc_type_tags(node, value) {
|
|
487
|
+
let _modified = false;
|
|
488
|
+
// @ts-ignore
|
|
489
|
+
if (node.jsDoc) {
|
|
490
|
+
// @ts-ignore
|
|
491
|
+
for (const comment of node.jsDoc) {
|
|
492
|
+
for (const tag of comment.tags ?? []) {
|
|
493
|
+
if (ts.isJSDocTypeTag(tag)) {
|
|
494
|
+
const is_fn =
|
|
495
|
+
ts.isFunctionDeclaration(value) ||
|
|
496
|
+
ts.isFunctionExpression(value) ||
|
|
497
|
+
ts.isArrowFunction(value);
|
|
498
|
+
|
|
499
|
+
if (is_fn && value.parameters?.length > 0) {
|
|
500
|
+
const name = ts.isIdentifier(value.parameters[0].name)
|
|
501
|
+
? value.parameters[0].name.text
|
|
502
|
+
: 'event';
|
|
503
|
+
code.overwrite(tag.tagName.pos, tag.tagName.end, 'param');
|
|
504
|
+
code.prependRight(tag.typeExpression.pos + 1, 'Parameters<');
|
|
505
|
+
code.appendLeft(tag.typeExpression.end - 1, '>[0]');
|
|
506
|
+
code.appendLeft(tag.typeExpression.end, ` ${name}`);
|
|
507
|
+
} else {
|
|
508
|
+
code.overwrite(tag.pos, tag.end, '');
|
|
509
|
+
}
|
|
510
|
+
_modified = true;
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
modified ||= _modified;
|
|
516
|
+
return _modified;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
ast.forEachChild((node) => {
|
|
520
|
+
if (ts.isFunctionDeclaration(node) && node.name?.text && node.name?.text === 'load') {
|
|
521
|
+
// remove JSDoc comment above `export function load ...`
|
|
522
|
+
replace_jsdoc_type_tags(node, node);
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
if (ts.isVariableStatement(node)) {
|
|
526
|
+
// remove JSDoc comment above `export const load = ...`
|
|
527
|
+
if (
|
|
528
|
+
ts.isIdentifier(node.declarationList.declarations[0].name) &&
|
|
529
|
+
names.has(node.declarationList.declarations[0].name.text) &&
|
|
530
|
+
node.declarationList.declarations[0].initializer
|
|
531
|
+
) {
|
|
532
|
+
replace_jsdoc_type_tags(node, node.declarationList.declarations[0].initializer);
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
for (const declaration of node.declarationList.declarations) {
|
|
536
|
+
if (
|
|
537
|
+
ts.isIdentifier(declaration.name) &&
|
|
538
|
+
declaration.name.text === 'load' &&
|
|
539
|
+
declaration.initializer
|
|
540
|
+
) {
|
|
541
|
+
// edge case — remove JSDoc comment above individual export
|
|
542
|
+
replace_jsdoc_type_tags(declaration, declaration.initializer);
|
|
543
|
+
|
|
544
|
+
// remove type from `export const load: Load ...`
|
|
545
|
+
if (declaration.type) {
|
|
546
|
+
let a = declaration.type.pos;
|
|
547
|
+
let b = declaration.type.end;
|
|
548
|
+
while (/\s/.test(content[a])) a += 1;
|
|
549
|
+
|
|
550
|
+
const type = content.slice(a, b);
|
|
551
|
+
code.remove(declaration.name.end, declaration.type.end);
|
|
552
|
+
|
|
553
|
+
const rhs = declaration.initializer;
|
|
554
|
+
|
|
555
|
+
if (
|
|
556
|
+
rhs &&
|
|
557
|
+
(ts.isArrowFunction(rhs) || ts.isFunctionExpression(rhs)) &&
|
|
558
|
+
rhs.parameters.length
|
|
559
|
+
) {
|
|
560
|
+
const arg = rhs.parameters[0];
|
|
561
|
+
const add_parens = content[arg.pos - 1] !== '(';
|
|
562
|
+
|
|
563
|
+
if (add_parens) code.prependRight(arg.pos, '(');
|
|
564
|
+
|
|
565
|
+
if (arg && !arg.type) {
|
|
566
|
+
code.appendLeft(
|
|
567
|
+
arg.name.end,
|
|
568
|
+
`: Parameters<${type}>[0]` + (add_parens ? ')' : '')
|
|
569
|
+
);
|
|
570
|
+
} else {
|
|
571
|
+
// prevent "type X is imported but not used" (isn't silenced by @ts-nocheck) when svelte-check runs
|
|
572
|
+
code.append(`;null as any as ${type};`);
|
|
573
|
+
}
|
|
574
|
+
} else {
|
|
575
|
+
// prevent "type X is imported but not used" (isn't silenced by @ts-nocheck) when svelte-check runs
|
|
576
|
+
code.append(`;null as any as ${type};`);
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
modified = true;
|
|
580
|
+
}
|
|
581
|
+
} else if (
|
|
582
|
+
is_server &&
|
|
583
|
+
ts.isIdentifier(declaration.name) &&
|
|
584
|
+
declaration.name?.text === 'actions' &&
|
|
585
|
+
declaration.initializer
|
|
586
|
+
) {
|
|
587
|
+
// remove JSDoc comment from `export const actions = ..`
|
|
588
|
+
const removed = replace_jsdoc_type_tags(node, declaration.initializer);
|
|
589
|
+
// ... and move type to each individual action
|
|
590
|
+
if (removed) {
|
|
591
|
+
const rhs = declaration.initializer;
|
|
592
|
+
if (ts.isObjectLiteralExpression(rhs)) {
|
|
593
|
+
for (const prop of rhs.properties) {
|
|
594
|
+
if (ts.isPropertyAssignment(prop) && ts.isIdentifier(prop.name)) {
|
|
595
|
+
const rhs = prop.initializer;
|
|
596
|
+
const replaced = replace_jsdoc_type_tags(prop, rhs);
|
|
597
|
+
if (
|
|
598
|
+
!replaced &&
|
|
599
|
+
rhs &&
|
|
600
|
+
(ts.isArrowFunction(rhs) || ts.isFunctionExpression(rhs)) &&
|
|
601
|
+
rhs.parameters?.[0]
|
|
602
|
+
) {
|
|
603
|
+
const name = ts.isIdentifier(rhs.parameters[0].name)
|
|
604
|
+
? rhs.parameters[0].name.text
|
|
605
|
+
: 'event';
|
|
606
|
+
code.prependRight(
|
|
607
|
+
rhs.pos,
|
|
608
|
+
`/** @param {import('./$types').RequestEvent} ${name} */ `
|
|
609
|
+
);
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
// remove type from `export const actions: Actions ...`
|
|
617
|
+
if (declaration.type) {
|
|
618
|
+
let a = declaration.type.pos;
|
|
619
|
+
let b = declaration.type.end;
|
|
620
|
+
while (/\s/.test(content[a])) a += 1;
|
|
621
|
+
|
|
622
|
+
const type = content.slice(a, b);
|
|
623
|
+
code.remove(declaration.name.end, declaration.type.end);
|
|
624
|
+
code.append(`;null as any as ${type};`);
|
|
625
|
+
modified = true;
|
|
626
|
+
|
|
627
|
+
// ... and move type to each individual action
|
|
628
|
+
const rhs = declaration.initializer;
|
|
629
|
+
if (ts.isObjectLiteralExpression(rhs)) {
|
|
630
|
+
for (const prop of rhs.properties) {
|
|
631
|
+
if (ts.isPropertyAssignment(prop) && ts.isIdentifier(prop.name)) {
|
|
632
|
+
const rhs = prop.initializer;
|
|
633
|
+
|
|
634
|
+
if (
|
|
635
|
+
rhs &&
|
|
636
|
+
(ts.isArrowFunction(rhs) || ts.isFunctionExpression(rhs)) &&
|
|
637
|
+
rhs.parameters.length
|
|
638
|
+
) {
|
|
639
|
+
const arg = rhs.parameters[0];
|
|
640
|
+
const add_parens = content[arg.pos - 1] !== '(';
|
|
641
|
+
|
|
642
|
+
if (add_parens) code.prependRight(arg.pos, '(');
|
|
643
|
+
|
|
644
|
+
if (arg && !arg.type) {
|
|
645
|
+
code.appendLeft(
|
|
646
|
+
arg.name.end,
|
|
647
|
+
`: import('./$types').RequestEvent` + (add_parens ? ')' : '')
|
|
648
|
+
);
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
});
|
|
659
|
+
|
|
660
|
+
if (modified) {
|
|
661
|
+
// Ignore all type errors so they don't show up twice when svelte-check runs
|
|
662
|
+
// Account for possible @ts-check which would overwrite @ts-nocheck
|
|
663
|
+
if (code.original.startsWith('// @ts-check')) {
|
|
664
|
+
code.prependLeft('// @ts-check'.length, '\n// @ts-nocheck\n');
|
|
665
|
+
} else {
|
|
666
|
+
code.prepend('// @ts-nocheck\n');
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
return {
|
|
671
|
+
modified,
|
|
672
|
+
code: code.toString(),
|
|
673
|
+
exports: Array.from(exports.keys())
|
|
674
|
+
};
|
|
675
|
+
} catch {
|
|
676
|
+
return null;
|
|
677
|
+
}
|
|
678
|
+
}
|