@sveltejs/kit 3.0.0-next.11 → 3.0.0-next.12
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/package.json +1 -1
- package/src/cli.js +8 -8
- package/src/constants.js +1 -1
- package/src/core/config/options.js +43 -35
- package/src/core/env.js +25 -11
- package/src/core/sync/sync.js +11 -14
- package/src/core/sync/utils.js +21 -1
- package/src/core/sync/{write_non_ambient.js → write_app_types.js} +23 -21
- package/src/core/sync/write_client_manifest.js +4 -12
- package/src/core/sync/write_env.js +6 -4
- package/src/core/sync/write_server.js +10 -15
- package/src/core/sync/write_tsconfig/index.js +245 -0
- package/src/core/sync/write_tsconfig/utils.js +162 -0
- package/src/core/sync/write_types/index.js +80 -88
- package/src/exports/internal/server/index.js +3 -1
- package/src/exports/public.d.ts +28 -51
- package/src/exports/vite/dev/index.js +88 -76
- package/src/exports/vite/index.js +364 -144
- package/src/exports/vite/module_ids.js +3 -1
- package/src/exports/vite/utils.js +1 -10
- package/src/runner.js +13 -0
- package/src/runtime/app/forms.js +4 -0
- package/src/runtime/app/manifest/index.js +1 -0
- package/src/runtime/app/paths/client.js +30 -30
- package/src/runtime/app/paths/internal/client.js +26 -0
- package/src/runtime/app/paths/server.js +22 -9
- package/src/runtime/app/paths/types.d.ts +11 -19
- package/src/runtime/app/server/remote/command.js +12 -7
- package/src/runtime/app/server/remote/form.js +29 -26
- package/src/runtime/app/server/remote/prerender.js +9 -2
- package/src/runtime/app/server/remote/query.js +5 -4
- package/src/runtime/app/server/remote/shared.js +48 -30
- package/src/runtime/app/service-worker/index.js +24 -0
- package/src/runtime/app/state/index.js +1 -1
- package/src/runtime/client/client.js +46 -9
- package/src/runtime/client/remote-functions/form.svelte.js +43 -51
- package/src/runtime/client/remote-functions/query/index.js +2 -2
- package/src/runtime/client/remote-functions/query-batch.svelte.js +2 -3
- package/src/runtime/client/remote-functions/query-live/iterator.js +5 -2
- package/src/runtime/client/remote-functions/shared.svelte.js +4 -1
- package/src/runtime/client/state.svelte.js +54 -21
- package/src/runtime/form-utils.js +89 -54
- package/src/runtime/server/cookie.js +1 -1
- package/src/runtime/server/data/index.js +31 -28
- package/src/runtime/server/errors.js +1 -1
- package/src/runtime/server/page/actions.js +3 -3
- package/src/runtime/server/page/render.js +19 -28
- package/src/runtime/server/remote-functions.js +66 -35
- package/src/runtime/server/respond.js +56 -14
- package/src/runtime/server/utils.js +10 -0
- package/src/types/ambient-private.d.ts +8 -0
- package/src/types/ambient.d.ts +22 -27
- package/src/types/global-private.d.ts +12 -0
- package/src/types/internal.d.ts +13 -3
- package/src/utils/url.js +12 -0
- package/src/version.js +1 -1
- package/types/index.d.ts +80 -98
- package/types/index.d.ts.map +4 -1
- package/src/core/sync/write_ambient.js +0 -18
- package/src/core/sync/write_tsconfig.js +0 -258
- /package/src/core/sync/{write_tsconfig_test → write_tsconfig/test-app}/package.json +0 -0
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
/** @import { ValidatedKitConfig } from 'types' */
|
|
2
|
+
import process from 'node:process';
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import ts from 'typescript';
|
|
6
|
+
import { styleText } from 'node:util';
|
|
7
|
+
import { write_if_changed } from '../utils.js';
|
|
8
|
+
import {
|
|
9
|
+
ESSENTIAL_OPTIONS,
|
|
10
|
+
extends_id,
|
|
11
|
+
get_subpath_imports,
|
|
12
|
+
normalize_config,
|
|
13
|
+
RECOMMENDED_OPTIONS,
|
|
14
|
+
remove_trailing_slashstar,
|
|
15
|
+
validate_resolved_config
|
|
16
|
+
} from './utils.js';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Generates the tsconfig that the user's tsconfig inherits from.
|
|
20
|
+
* @param {import('types').ValidatedKitConfig} kit
|
|
21
|
+
* @param {string} root
|
|
22
|
+
*/
|
|
23
|
+
export function write_tsconfig(kit, root) {
|
|
24
|
+
const paths = get_paths(kit, root);
|
|
25
|
+
|
|
26
|
+
write_parent_tsconfig(
|
|
27
|
+
root,
|
|
28
|
+
root,
|
|
29
|
+
'$app/tsconfig',
|
|
30
|
+
{
|
|
31
|
+
compilerOptions: {
|
|
32
|
+
paths,
|
|
33
|
+
rootDirs: ['.', `${kit.outDir}/types`],
|
|
34
|
+
types: ['$app/types'],
|
|
35
|
+
|
|
36
|
+
// This is required for svelte-package to work as expected
|
|
37
|
+
// Can be overwritten
|
|
38
|
+
lib: ['ESNext', 'DOM', 'DOM.Iterable'],
|
|
39
|
+
|
|
40
|
+
...ESSENTIAL_OPTIONS,
|
|
41
|
+
...RECOMMENDED_OPTIONS
|
|
42
|
+
},
|
|
43
|
+
exclude: [kit.files.serviceWorker]
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
extends: '$app/tsconfig',
|
|
47
|
+
include: ['src']
|
|
48
|
+
},
|
|
49
|
+
kit.typescript.config
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
write_parent_tsconfig(
|
|
53
|
+
root,
|
|
54
|
+
kit.files.serviceWorker,
|
|
55
|
+
'$app/tsconfig/service-worker',
|
|
56
|
+
{
|
|
57
|
+
compilerOptions: {
|
|
58
|
+
paths,
|
|
59
|
+
types: ['$app/types'],
|
|
60
|
+
lib: ['ESNext', 'WebWorker'],
|
|
61
|
+
...ESSENTIAL_OPTIONS
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
extends: '$app/tsconfig/service-worker'
|
|
66
|
+
}
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Write a generated `tsconfig.json` inside `node_modules`, for the
|
|
72
|
+
* user config to extend
|
|
73
|
+
* @param {string} root The project root
|
|
74
|
+
* @param {string} dir The directory to resolve a user config from
|
|
75
|
+
* @param {string} id The id of the generated config
|
|
76
|
+
* @param {any} config The contents of the generated tsconfig, with paths relative to `root`
|
|
77
|
+
* @param {any} example What to print if the user config does _not_ extend the generated config
|
|
78
|
+
* @param {ValidatedKitConfig['typescript']['config']} [transform] TODO get rid of this
|
|
79
|
+
*/
|
|
80
|
+
function write_parent_tsconfig(root, dir, id, config, example, transform) {
|
|
81
|
+
const out_file = path.join(root, `node_modules/${id}/tsconfig.json`);
|
|
82
|
+
|
|
83
|
+
let normalized = normalize_config(out_file, config);
|
|
84
|
+
normalized = transform?.(normalized) ?? normalized;
|
|
85
|
+
|
|
86
|
+
write_if_changed(out_file, JSON.stringify(normalized, null, '\t'));
|
|
87
|
+
|
|
88
|
+
const user_config = load_user_tsconfig(dir);
|
|
89
|
+
|
|
90
|
+
if (user_config && modified_since_last_check(user_config.file)) {
|
|
91
|
+
// now that we've written the parent config, we can resolve the
|
|
92
|
+
// user config and validate that nothing important was overwritten
|
|
93
|
+
if (!extends_id(user_config.options, id)) {
|
|
94
|
+
console.warn(
|
|
95
|
+
styleText(
|
|
96
|
+
['bold', 'yellow'],
|
|
97
|
+
`${path.relative(process.cwd(), user_config.file)} should extend SvelteKit's built-in configuration:`
|
|
98
|
+
)
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
console.warn(JSON.stringify(example, null, ' '));
|
|
102
|
+
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const resolved = ts.parseJsonConfigFileContent(user_config.options, ts.sys, dir).options;
|
|
107
|
+
const warnings = validate_resolved_config(resolved, config.compilerOptions);
|
|
108
|
+
|
|
109
|
+
if (warnings.length > 0) {
|
|
110
|
+
console.warn(
|
|
111
|
+
styleText(
|
|
112
|
+
['bold', 'yellow'],
|
|
113
|
+
`Found issues while validating ${path.relative(process.cwd(), user_config.file)}`
|
|
114
|
+
)
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
for (const warning of warnings) {
|
|
118
|
+
console.warn(` - ${warning}`);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** @type {Map<string, number>} */
|
|
125
|
+
const mtimes = new Map();
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Returns true if the file was modified since we last got here,
|
|
129
|
+
* otherwise we can skip warnings
|
|
130
|
+
* @param {string} file
|
|
131
|
+
*/
|
|
132
|
+
function modified_since_last_check(file) {
|
|
133
|
+
const a = mtimes.get(file) ?? -1;
|
|
134
|
+
const b = fs.statSync(file).mtimeMs;
|
|
135
|
+
mtimes.set(file, b);
|
|
136
|
+
|
|
137
|
+
return b > a;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* @param {string} cwd
|
|
142
|
+
* @param {string} file
|
|
143
|
+
*/
|
|
144
|
+
function maybe_file(cwd, file) {
|
|
145
|
+
const resolved = path.resolve(cwd, file);
|
|
146
|
+
if (fs.existsSync(resolved)) {
|
|
147
|
+
return resolved;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** @param {string} cwd */
|
|
152
|
+
function load_user_tsconfig(cwd) {
|
|
153
|
+
const file = maybe_file(cwd, 'tsconfig.json') || maybe_file(cwd, 'jsconfig.json');
|
|
154
|
+
if (!file) return;
|
|
155
|
+
|
|
156
|
+
const options = load_tsconfig(file);
|
|
157
|
+
|
|
158
|
+
return {
|
|
159
|
+
file,
|
|
160
|
+
kind: path.basename(file),
|
|
161
|
+
options
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* @param {string} file
|
|
167
|
+
*/
|
|
168
|
+
function load_tsconfig(file) {
|
|
169
|
+
const options = ts.readConfigFile(file, ts.sys.readFile);
|
|
170
|
+
|
|
171
|
+
if (options.error) {
|
|
172
|
+
let message = `Failed to parse TypeScript config`;
|
|
173
|
+
|
|
174
|
+
if (typeof options.error.messageText === 'string') {
|
|
175
|
+
message += `: ${options.error.messageText}`;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const error = new Error(message);
|
|
179
|
+
error.stack = '';
|
|
180
|
+
|
|
181
|
+
if (options.error.file && options.error.start !== undefined) {
|
|
182
|
+
const line_start = options.error.file.text.lastIndexOf('\n', options.error.start);
|
|
183
|
+
|
|
184
|
+
const line =
|
|
185
|
+
line_start === -1
|
|
186
|
+
? 0
|
|
187
|
+
: options.error.file.text.slice(0, options.error.start).split('\n').length;
|
|
188
|
+
const column = options.error.start - line_start;
|
|
189
|
+
|
|
190
|
+
error.stack = `${error.message}\n at ${path.relative(process.cwd(), file)}:${line}:${column}`;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
throw error;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return options.config;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** Matches anything (except the empty string/string with newline), and separates any trailing `/*` */
|
|
200
|
+
const alias_key = /^(.+?)(\/\*)?$/;
|
|
201
|
+
|
|
202
|
+
/** Matches anything (except the empty string/string with newline), and separates any trailing `/*` or file extension */
|
|
203
|
+
const alias_value = /^(.+?)((\/\*)|(\.\w+))?$/;
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Generates tsconfig path aliases from kit's aliases and the package.json `imports` field.
|
|
207
|
+
* Related to vite alias creation.
|
|
208
|
+
*
|
|
209
|
+
* @param {import('types').ValidatedKitConfig} config
|
|
210
|
+
* @param {string} root
|
|
211
|
+
* @returns {Record<string, string[]>}
|
|
212
|
+
*/
|
|
213
|
+
function get_paths(config, root) {
|
|
214
|
+
const alias = {
|
|
215
|
+
...config.alias,
|
|
216
|
+
...get_subpath_imports(root)
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
/** @type {Record<string, string[]>} */
|
|
220
|
+
const paths = {};
|
|
221
|
+
|
|
222
|
+
for (const [key, value] of Object.entries(alias)) {
|
|
223
|
+
const key_match = alias_key.exec(key);
|
|
224
|
+
if (!key_match) throw new Error(`Invalid alias key: ${key}`);
|
|
225
|
+
|
|
226
|
+
const value_match = alias_value.exec(value);
|
|
227
|
+
if (!value_match) throw new Error(`Invalid alias value: ${value}`);
|
|
228
|
+
|
|
229
|
+
const resolved = path.resolve(root, remove_trailing_slashstar(value));
|
|
230
|
+
const slashstar = key_match[2];
|
|
231
|
+
|
|
232
|
+
if (slashstar) {
|
|
233
|
+
paths[key] = [resolved + '/*'];
|
|
234
|
+
} else {
|
|
235
|
+
paths[key] = [resolved];
|
|
236
|
+
const fileending = value_match[4];
|
|
237
|
+
|
|
238
|
+
if (!fileending && !(key + '/*' in alias)) {
|
|
239
|
+
paths[key + '/*'] = [resolved + '/*'];
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
return paths;
|
|
245
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { normalize_import_value, read_package_imports } from '../../../utils/imports.js';
|
|
3
|
+
import { posixify } from '../../../utils/os.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Without these, compilation will fail
|
|
7
|
+
* @type {Record<string, any>}
|
|
8
|
+
*/
|
|
9
|
+
export const ESSENTIAL_OPTIONS = {
|
|
10
|
+
// svelte-preprocess cannot figure out whether you have a value or a type, so tell TypeScript
|
|
11
|
+
// to enforce using \`import type\` instead of \`import\` for Types.
|
|
12
|
+
// Also, TypeScript doesn't know about import usages in the template because it only sees the
|
|
13
|
+
// script of a Svelte file. Therefore preserve all value imports.
|
|
14
|
+
verbatimModuleSyntax: true,
|
|
15
|
+
// Vite compiles modules one at a time
|
|
16
|
+
isolatedModules: true
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Options that are strongly recommended, either because not having them is silly or
|
|
21
|
+
* because the align TypeScript's behaviour with Vite's, but which can be overwritten
|
|
22
|
+
* @type {Record<string, any>}
|
|
23
|
+
*/
|
|
24
|
+
export const RECOMMENDED_OPTIONS = {
|
|
25
|
+
allowJs: true,
|
|
26
|
+
checkJs: true,
|
|
27
|
+
forceConsistentCasingInFileNames: true,
|
|
28
|
+
resolveJsonModule: true,
|
|
29
|
+
moduleDetection: 'force',
|
|
30
|
+
moduleResolution: 'bundler',
|
|
31
|
+
allowImportingTsExtensions: true,
|
|
32
|
+
module: 'esnext',
|
|
33
|
+
target: 'esnext',
|
|
34
|
+
skipLibCheck: true,
|
|
35
|
+
esModuleInterop: true,
|
|
36
|
+
noEmit: true // prevent tsconfig error "overwriting input files" - Vite handles the build and ignores this
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Validates that a tsconfig contains `"extends": "<id>"`
|
|
41
|
+
* @param {any} options
|
|
42
|
+
* @param {string} id
|
|
43
|
+
*/
|
|
44
|
+
export function extends_id(options, id) {
|
|
45
|
+
const o = options.extends;
|
|
46
|
+
return Array.isArray(o) ? o.includes(id) : o === id;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Makes paths relative to the output file
|
|
51
|
+
* @param {string} out
|
|
52
|
+
* @param {any} config
|
|
53
|
+
* @param {(input: any) => any} [transform] TODO get rid of this option
|
|
54
|
+
*/
|
|
55
|
+
export function normalize_config(out, config, transform) {
|
|
56
|
+
const dir = path.dirname(out);
|
|
57
|
+
const relative = (/** @type {string} */ file) => posixify(path.relative(dir, file));
|
|
58
|
+
|
|
59
|
+
const paths =
|
|
60
|
+
config.compilerOptions?.paths &&
|
|
61
|
+
Object.fromEntries(
|
|
62
|
+
Object.entries(config.compilerOptions?.paths).map(([k, v]) => [k, v.map(relative)])
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
const normalized = {
|
|
66
|
+
...config,
|
|
67
|
+
compilerOptions: {
|
|
68
|
+
...config.compilerOptions,
|
|
69
|
+
paths,
|
|
70
|
+
rootDirs: config.compilerOptions?.rootDirs?.map(relative)
|
|
71
|
+
},
|
|
72
|
+
include: config.include?.map(relative),
|
|
73
|
+
exclude: config.exclude?.map(relative)
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
return transform?.(normalized) ?? normalized;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* @param {any} resolved
|
|
81
|
+
* @param {any} options
|
|
82
|
+
*/
|
|
83
|
+
export function validate_resolved_config(resolved, options) {
|
|
84
|
+
const warnings = [];
|
|
85
|
+
|
|
86
|
+
const join = (/** @type {string[]} */ array) =>
|
|
87
|
+
array
|
|
88
|
+
.map((v) => JSON.stringify(v))
|
|
89
|
+
.join(', ')
|
|
90
|
+
.replace(/, ([^,]*)$/, ' and $1');
|
|
91
|
+
|
|
92
|
+
const missing_types = options.types?.filter(
|
|
93
|
+
(/** @type {string} */ type) => !resolved.types?.includes(type)
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
if (missing_types.length > 0) {
|
|
97
|
+
warnings.push(`"types" was overwritten. It must include ${join(missing_types)}`);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (options.paths) {
|
|
101
|
+
/** @type {Set<string>} */
|
|
102
|
+
const mismatch = new Set();
|
|
103
|
+
|
|
104
|
+
for (const [k, expected] of Object.entries(options.paths)) {
|
|
105
|
+
const actual = resolved.paths?.[k]?.map((/** @type {string} */ x) =>
|
|
106
|
+
path.resolve(/** @type {string} */ (resolved.pathsBasePath), x)
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
if (JSON.stringify(expected) !== JSON.stringify(actual)) {
|
|
110
|
+
mismatch.add(remove_trailing_slashstar(k));
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (mismatch.size > 0) {
|
|
115
|
+
const joined = join([...mismatch]);
|
|
116
|
+
|
|
117
|
+
warnings.push(`"paths" was overwritten. Imports from ${joined} may not typecheck`);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
for (const key in ESSENTIAL_OPTIONS) {
|
|
122
|
+
if (resolved[key] !== options[key]) {
|
|
123
|
+
warnings.push(`"${key}" was overwritten. It should be ${JSON.stringify(options[key])}`);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return warnings;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* @param {string} file
|
|
132
|
+
*/
|
|
133
|
+
export function remove_trailing_slashstar(file) {
|
|
134
|
+
if (file.endsWith('/*')) {
|
|
135
|
+
return file.slice(0, -2);
|
|
136
|
+
} else {
|
|
137
|
+
return file;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* @param {string} root
|
|
143
|
+
*/
|
|
144
|
+
export function get_subpath_imports(root) {
|
|
145
|
+
// Add all `#`-prefixed imports from package.json as path aliases
|
|
146
|
+
const imports = read_package_imports(root);
|
|
147
|
+
|
|
148
|
+
/** @type {Record<string, string>} */
|
|
149
|
+
const alias = {};
|
|
150
|
+
|
|
151
|
+
if (imports) {
|
|
152
|
+
for (const [key, raw_value] of Object.entries(imports)) {
|
|
153
|
+
if (!key.startsWith('#')) continue;
|
|
154
|
+
const value = normalize_import_value(raw_value);
|
|
155
|
+
if (value) {
|
|
156
|
+
alias[key] = value;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return alias;
|
|
162
|
+
}
|
|
@@ -609,7 +609,7 @@ function generate_params_type(params, outdir, config) {
|
|
|
609
609
|
(param) =>
|
|
610
610
|
`${/^\w+$/.test(param.name) ? param.name : `'${param.name}'`}${param.optional ? '?' : ''}: ${
|
|
611
611
|
param.matcher
|
|
612
|
-
? `
|
|
612
|
+
? `Kit.MatcherParam<(typeof import('${params_import}').params)[${JSON.stringify(param.matcher)}]>`
|
|
613
613
|
: 'string'
|
|
614
614
|
}${param.optional ? ' | undefined' : ''}`
|
|
615
615
|
)
|
|
@@ -717,6 +717,62 @@ export function tweak_types(content, is_server) {
|
|
|
717
717
|
return _modified;
|
|
718
718
|
}
|
|
719
719
|
|
|
720
|
+
/**
|
|
721
|
+
* @param {number} name_end position of the identifier the annotation is attached to
|
|
722
|
+
* @param {import('typescript').TypeNode} type_node
|
|
723
|
+
* @returns {string} the removed annotation's text
|
|
724
|
+
*/
|
|
725
|
+
function strip_type_annotation(name_end, type_node) {
|
|
726
|
+
let a = type_node.pos;
|
|
727
|
+
const b = type_node.end;
|
|
728
|
+
while (is_whitespace(content[a])) a += 1;
|
|
729
|
+
|
|
730
|
+
const type = content.slice(a, b);
|
|
731
|
+
code.remove(name_end, type_node.end);
|
|
732
|
+
modified = true;
|
|
733
|
+
return type;
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
/**
|
|
737
|
+
* Types the first parameter of an untyped arrow/function expression.
|
|
738
|
+
* @param {import('typescript').Expression | undefined} rhs
|
|
739
|
+
* @param {string} type
|
|
740
|
+
* @returns {boolean} `false` if there was nothing to annotate
|
|
741
|
+
*/
|
|
742
|
+
function annotate_first_param(rhs, type) {
|
|
743
|
+
if (
|
|
744
|
+
!rhs ||
|
|
745
|
+
!(ts.isArrowFunction(rhs) || ts.isFunctionExpression(rhs)) ||
|
|
746
|
+
!rhs.parameters.length
|
|
747
|
+
) {
|
|
748
|
+
return false;
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
const arg = rhs.parameters[0];
|
|
752
|
+
const add_parens = content[arg.pos - 1] !== '(';
|
|
753
|
+
|
|
754
|
+
if (add_parens) code.prependRight(arg.pos, '(');
|
|
755
|
+
|
|
756
|
+
if (arg.type) return false;
|
|
757
|
+
|
|
758
|
+
code.appendLeft(arg.name.end, `: ${type}` + (add_parens ? ')' : ''));
|
|
759
|
+
return true;
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
/**
|
|
763
|
+
* @param {import('typescript').Expression | undefined} initializer
|
|
764
|
+
* @param {(prop: import('typescript').PropertyAssignment) => void} callback
|
|
765
|
+
*/
|
|
766
|
+
function for_each_action(initializer, callback) {
|
|
767
|
+
if (initializer && ts.isObjectLiteralExpression(initializer)) {
|
|
768
|
+
for (const prop of initializer.properties) {
|
|
769
|
+
if (ts.isPropertyAssignment(prop) && ts.isIdentifier(prop.name)) {
|
|
770
|
+
callback(prop);
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
|
|
720
776
|
ast.forEachChild((node) => {
|
|
721
777
|
if (ts.isFunctionDeclaration(node) && node.name?.text && node.name?.text === 'load') {
|
|
722
778
|
// remove JSDoc comment above `export function load ...`
|
|
@@ -742,42 +798,13 @@ export function tweak_types(content, is_server) {
|
|
|
742
798
|
// edge case — remove JSDoc comment above individual export
|
|
743
799
|
replace_jsdoc_type_tags(declaration, declaration.initializer);
|
|
744
800
|
|
|
745
|
-
// remove type from `export const load: Load ...`
|
|
746
801
|
if (declaration.type) {
|
|
747
|
-
|
|
748
|
-
const b = declaration.type.end;
|
|
749
|
-
while (is_whitespace(content[a])) a += 1;
|
|
750
|
-
|
|
751
|
-
const type = content.slice(a, b);
|
|
752
|
-
code.remove(declaration.name.end, declaration.type.end);
|
|
802
|
+
const type = strip_type_annotation(declaration.name.end, declaration.type);
|
|
753
803
|
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
if (
|
|
757
|
-
rhs &&
|
|
758
|
-
(ts.isArrowFunction(rhs) || ts.isFunctionExpression(rhs)) &&
|
|
759
|
-
rhs.parameters.length
|
|
760
|
-
) {
|
|
761
|
-
const arg = rhs.parameters[0];
|
|
762
|
-
const add_parens = content[arg.pos - 1] !== '(';
|
|
763
|
-
|
|
764
|
-
if (add_parens) code.prependRight(arg.pos, '(');
|
|
765
|
-
|
|
766
|
-
if (arg && !arg.type) {
|
|
767
|
-
code.appendLeft(
|
|
768
|
-
arg.name.end,
|
|
769
|
-
`: Parameters<${type}>[0]` + (add_parens ? ')' : '')
|
|
770
|
-
);
|
|
771
|
-
} else {
|
|
772
|
-
// prevent "type X is imported but not used" (isn't silenced by @ts-nocheck) when svelte-check runs
|
|
773
|
-
code.append(`;null as any as ${type};`);
|
|
774
|
-
}
|
|
775
|
-
} else {
|
|
804
|
+
if (!annotate_first_param(declaration.initializer, `Parameters<${type}>[0]`)) {
|
|
776
805
|
// prevent "type X is imported but not used" (isn't silenced by @ts-nocheck) when svelte-check runs
|
|
777
806
|
code.append(`;null as any as ${type};`);
|
|
778
807
|
}
|
|
779
|
-
|
|
780
|
-
modified = true;
|
|
781
808
|
}
|
|
782
809
|
} else if (
|
|
783
810
|
is_server &&
|
|
@@ -789,69 +816,34 @@ export function tweak_types(content, is_server) {
|
|
|
789
816
|
const removed = replace_jsdoc_type_tags(node, declaration.initializer);
|
|
790
817
|
// ... and move type to each individual action
|
|
791
818
|
if (removed) {
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
rhs.pos,
|
|
809
|
-
`/** @param {import('./$types').RequestEvent} ${name} */ `
|
|
810
|
-
);
|
|
811
|
-
}
|
|
812
|
-
}
|
|
819
|
+
for_each_action(declaration.initializer, (prop) => {
|
|
820
|
+
const rhs = prop.initializer;
|
|
821
|
+
const replaced = replace_jsdoc_type_tags(prop, rhs);
|
|
822
|
+
if (
|
|
823
|
+
!replaced &&
|
|
824
|
+
rhs &&
|
|
825
|
+
(ts.isArrowFunction(rhs) || ts.isFunctionExpression(rhs)) &&
|
|
826
|
+
rhs.parameters?.[0]
|
|
827
|
+
) {
|
|
828
|
+
const name = ts.isIdentifier(rhs.parameters[0].name)
|
|
829
|
+
? rhs.parameters[0].name.text
|
|
830
|
+
: 'event';
|
|
831
|
+
code.prependRight(
|
|
832
|
+
rhs.pos,
|
|
833
|
+
`/** @param {import('./$types').RequestEvent} ${name} */ `
|
|
834
|
+
);
|
|
813
835
|
}
|
|
814
|
-
}
|
|
836
|
+
});
|
|
815
837
|
}
|
|
816
838
|
|
|
817
|
-
// remove type from `export const actions: Actions ...`
|
|
818
839
|
if (declaration.type) {
|
|
819
|
-
|
|
820
|
-
const b = declaration.type.end;
|
|
821
|
-
while (is_whitespace(content[a])) a += 1;
|
|
822
|
-
|
|
823
|
-
const type = content.slice(a, b);
|
|
824
|
-
code.remove(declaration.name.end, declaration.type.end);
|
|
840
|
+
const type = strip_type_annotation(declaration.name.end, declaration.type);
|
|
825
841
|
code.append(`;null as any as ${type};`);
|
|
826
|
-
modified = true;
|
|
827
842
|
|
|
828
843
|
// ... and move type to each individual action
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
if (ts.isPropertyAssignment(prop) && ts.isIdentifier(prop.name)) {
|
|
833
|
-
const rhs = prop.initializer;
|
|
834
|
-
|
|
835
|
-
if (
|
|
836
|
-
rhs &&
|
|
837
|
-
(ts.isArrowFunction(rhs) || ts.isFunctionExpression(rhs)) &&
|
|
838
|
-
rhs.parameters.length
|
|
839
|
-
) {
|
|
840
|
-
const arg = rhs.parameters[0];
|
|
841
|
-
const add_parens = content[arg.pos - 1] !== '(';
|
|
842
|
-
|
|
843
|
-
if (add_parens) code.prependRight(arg.pos, '(');
|
|
844
|
-
|
|
845
|
-
if (arg && !arg.type) {
|
|
846
|
-
code.appendLeft(
|
|
847
|
-
arg.name.end,
|
|
848
|
-
": import('./$types').RequestEvent" + (add_parens ? ')' : '')
|
|
849
|
-
);
|
|
850
|
-
}
|
|
851
|
-
}
|
|
852
|
-
}
|
|
853
|
-
}
|
|
854
|
-
}
|
|
844
|
+
for_each_action(declaration.initializer, (prop) => {
|
|
845
|
+
annotate_first_param(prop.initializer, "import('./$types').RequestEvent");
|
|
846
|
+
});
|
|
855
847
|
}
|
|
856
848
|
}
|
|
857
849
|
}
|
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
import { try_get_request_store } from './event.js';
|
|
3
3
|
|
|
4
4
|
export function get_origin() {
|
|
5
|
-
|
|
5
|
+
// `request.url` rather than `event.url`, which throws inside queries
|
|
6
|
+
const request = try_get_request_store()?.event.request;
|
|
7
|
+
return request && new URL(request.url).origin;
|
|
6
8
|
}
|
|
7
9
|
|
|
8
10
|
/**
|