@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.
Files changed (125) hide show
  1. package/README.md +12 -9
  2. package/package.json +94 -67
  3. package/postinstall.js +38 -0
  4. package/scripts/special-types/$env+dynamic+private.md +10 -0
  5. package/scripts/special-types/$env+dynamic+public.md +8 -0
  6. package/scripts/special-types/$env+static+private.md +19 -0
  7. package/scripts/special-types/$env+static+public.md +7 -0
  8. package/scripts/special-types/$lib.md +5 -0
  9. package/src/cli.js +108 -0
  10. package/src/constants.js +7 -0
  11. package/src/core/adapt/builder.js +206 -0
  12. package/src/core/adapt/index.js +31 -0
  13. package/src/core/config/default-error.html +56 -0
  14. package/src/core/config/index.js +110 -0
  15. package/src/core/config/options.js +504 -0
  16. package/src/core/config/types.d.ts +1 -0
  17. package/src/core/env.js +121 -0
  18. package/src/core/generate_manifest/index.js +93 -0
  19. package/src/core/prerender/crawl.js +198 -0
  20. package/src/core/prerender/entities.js +2252 -0
  21. package/src/core/prerender/prerender.js +431 -0
  22. package/src/core/prerender/queue.js +80 -0
  23. package/src/core/sync/create_manifest_data/index.js +488 -0
  24. package/src/core/sync/create_manifest_data/types.d.ts +37 -0
  25. package/src/core/sync/sync.js +70 -0
  26. package/src/core/sync/utils.js +33 -0
  27. package/src/core/sync/write_ambient.js +53 -0
  28. package/src/core/sync/write_client_manifest.js +106 -0
  29. package/src/core/sync/write_matchers.js +25 -0
  30. package/src/core/sync/write_root.js +91 -0
  31. package/src/core/sync/write_tsconfig.js +195 -0
  32. package/src/core/sync/write_types/index.js +678 -0
  33. package/src/core/utils.js +70 -0
  34. package/src/exports/hooks/index.js +1 -0
  35. package/src/exports/hooks/sequence.js +44 -0
  36. package/src/exports/index.js +45 -0
  37. package/src/exports/node/index.js +173 -0
  38. package/src/exports/node/polyfills.js +41 -0
  39. package/src/exports/vite/build/build_server.js +378 -0
  40. package/src/exports/vite/build/build_service_worker.js +90 -0
  41. package/src/exports/vite/build/utils.js +180 -0
  42. package/src/exports/vite/dev/index.js +577 -0
  43. package/src/exports/vite/graph_analysis/index.js +277 -0
  44. package/src/exports/vite/graph_analysis/types.d.ts +5 -0
  45. package/src/exports/vite/graph_analysis/utils.js +30 -0
  46. package/src/exports/vite/index.js +598 -0
  47. package/src/exports/vite/preview/index.js +189 -0
  48. package/src/exports/vite/types.d.ts +3 -0
  49. package/src/exports/vite/utils.js +157 -0
  50. package/src/runtime/app/env.js +1 -0
  51. package/src/runtime/app/environment.js +11 -0
  52. package/src/runtime/app/forms.js +114 -0
  53. package/src/runtime/app/navigation.js +23 -0
  54. package/src/runtime/app/paths.js +1 -0
  55. package/src/runtime/app/stores.js +102 -0
  56. package/src/runtime/client/ambient.d.ts +26 -0
  57. package/src/runtime/client/client.js +1583 -0
  58. package/src/runtime/client/fetcher.js +107 -0
  59. package/src/runtime/client/parse.js +60 -0
  60. package/src/runtime/client/singletons.js +21 -0
  61. package/src/runtime/client/start.js +37 -0
  62. package/src/runtime/client/types.d.ts +84 -0
  63. package/src/runtime/client/utils.js +159 -0
  64. package/src/runtime/components/error.svelte +16 -0
  65. package/{assets → src/runtime}/components/layout.svelte +0 -0
  66. package/src/runtime/control.js +98 -0
  67. package/src/runtime/env/dynamic/private.js +1 -0
  68. package/src/runtime/env/dynamic/public.js +1 -0
  69. package/src/runtime/env-private.js +6 -0
  70. package/src/runtime/env-public.js +6 -0
  71. package/src/runtime/env.js +6 -0
  72. package/src/runtime/hash.js +16 -0
  73. package/src/runtime/paths.js +11 -0
  74. package/src/runtime/server/cookie.js +127 -0
  75. package/src/runtime/server/data/index.js +136 -0
  76. package/src/runtime/server/endpoint.js +90 -0
  77. package/src/runtime/server/index.js +340 -0
  78. package/src/runtime/server/page/actions.js +243 -0
  79. package/src/runtime/server/page/crypto.js +239 -0
  80. package/src/runtime/server/page/csp.js +250 -0
  81. package/src/runtime/server/page/fetch.js +301 -0
  82. package/src/runtime/server/page/index.js +304 -0
  83. package/src/runtime/server/page/load_data.js +124 -0
  84. package/src/runtime/server/page/render.js +342 -0
  85. package/src/runtime/server/page/respond_with_error.js +104 -0
  86. package/src/runtime/server/page/serialize_data.js +87 -0
  87. package/src/runtime/server/page/types.d.ts +41 -0
  88. package/src/runtime/server/utils.js +179 -0
  89. package/src/utils/array.js +9 -0
  90. package/src/utils/error.js +22 -0
  91. package/src/utils/escape.js +46 -0
  92. package/src/utils/filesystem.js +137 -0
  93. package/src/utils/functions.js +16 -0
  94. package/src/utils/http.js +55 -0
  95. package/src/utils/misc.js +1 -0
  96. package/src/utils/routing.js +117 -0
  97. package/src/utils/unit_test.js +11 -0
  98. package/src/utils/url.js +142 -0
  99. package/svelte-kit.js +1 -1
  100. package/types/ambient.d.ts +426 -0
  101. package/types/index.d.ts +444 -0
  102. package/types/internal.d.ts +378 -0
  103. package/types/private.d.ts +224 -0
  104. package/CHANGELOG.md +0 -484
  105. package/assets/components/error.svelte +0 -13
  106. package/assets/runtime/app/env.js +0 -5
  107. package/assets/runtime/app/navigation.js +0 -44
  108. package/assets/runtime/app/paths.js +0 -1
  109. package/assets/runtime/app/stores.js +0 -93
  110. package/assets/runtime/chunks/utils.js +0 -22
  111. package/assets/runtime/internal/singletons.js +0 -23
  112. package/assets/runtime/internal/start.js +0 -779
  113. package/assets/runtime/paths.js +0 -12
  114. package/dist/chunks/index.js +0 -3516
  115. package/dist/chunks/index2.js +0 -587
  116. package/dist/chunks/index3.js +0 -246
  117. package/dist/chunks/index4.js +0 -528
  118. package/dist/chunks/index5.js +0 -763
  119. package/dist/chunks/index6.js +0 -322
  120. package/dist/chunks/standard.js +0 -99
  121. package/dist/chunks/utils.js +0 -83
  122. package/dist/cli.js +0 -550
  123. package/dist/ssr.js +0 -2588
  124. package/types.d.ts +0 -89
  125. package/types.internal.d.ts +0 -191
@@ -0,0 +1,56 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8" />
5
+ <title>%sveltekit.error.message%</title>
6
+
7
+ <style>
8
+ body {
9
+ font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen,
10
+ Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
11
+ display: flex;
12
+ align-items: center;
13
+ justify-content: center;
14
+ height: 100vh;
15
+ }
16
+
17
+ .error {
18
+ display: flex;
19
+ align-items: center;
20
+ max-width: 32rem;
21
+ margin: 0 1rem;
22
+ }
23
+
24
+ .status {
25
+ font-weight: 200;
26
+ font-size: 3rem;
27
+ line-height: 1;
28
+ position: relative;
29
+ top: -0.05rem;
30
+ }
31
+
32
+ .message {
33
+ border-left: 1px solid #ccc;
34
+ padding: 0 0 0 1rem;
35
+ margin: 0 0 0 1rem;
36
+ min-height: 2.5rem;
37
+ display: flex;
38
+ align-items: center;
39
+ }
40
+
41
+ .message h1 {
42
+ font-weight: 400;
43
+ font-size: 1em;
44
+ margin: 0;
45
+ }
46
+ </style>
47
+ </head>
48
+ <body>
49
+ <div class="error">
50
+ <span class="status">%sveltekit.status%</span>
51
+ <div class="message">
52
+ <h1>%sveltekit.error.message%</h1>
53
+ </div>
54
+ </div>
55
+ </body>
56
+ </html>
@@ -0,0 +1,110 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import * as url from 'url';
4
+ import options from './options.js';
5
+
6
+ /**
7
+ * Loads the template (src/app.html by default) and validates that it has the
8
+ * required content.
9
+ * @param {string} cwd
10
+ * @param {import('types').ValidatedConfig} config
11
+ */
12
+ export function load_template(cwd, config) {
13
+ const { appTemplate } = config.kit.files;
14
+ const relative = path.relative(cwd, appTemplate);
15
+
16
+ if (fs.existsSync(appTemplate)) {
17
+ const contents = fs.readFileSync(appTemplate, 'utf8');
18
+
19
+ // TODO remove this for 1.0
20
+ const match = /%svelte\.([a-z]+)%/.exec(contents);
21
+ if (match) {
22
+ throw new Error(
23
+ `%svelte.${match[1]}% in ${relative} should be replaced with %sveltekit.${match[1]}%`
24
+ );
25
+ }
26
+
27
+ const expected_tags = ['%sveltekit.head%', '%sveltekit.body%'];
28
+ expected_tags.forEach((tag) => {
29
+ if (contents.indexOf(tag) === -1) {
30
+ throw new Error(`${relative} is missing ${tag}`);
31
+ }
32
+ });
33
+ } else {
34
+ throw new Error(`${relative} does not exist`);
35
+ }
36
+
37
+ return fs.readFileSync(appTemplate, 'utf-8');
38
+ }
39
+
40
+ /**
41
+ * Loads the error page (src/error.html by default) if it exists.
42
+ * Falls back to a generic error page content.
43
+ * @param {import('types').ValidatedConfig} config
44
+ */
45
+ export function load_error_page(config) {
46
+ const { errorTemplate } = config.kit.files;
47
+ return fs.readFileSync(errorTemplate, 'utf-8');
48
+ }
49
+
50
+ /**
51
+ * Loads and validates svelte.config.js
52
+ * @param {{ cwd?: string }} options
53
+ * @returns {Promise<import('types').ValidatedConfig>}
54
+ */
55
+ export async function load_config({ cwd = process.cwd() } = {}) {
56
+ const config_file = path.join(cwd, 'svelte.config.js');
57
+
58
+ if (!fs.existsSync(config_file)) {
59
+ return process_config({}, { cwd });
60
+ }
61
+
62
+ const config = await import(`${url.pathToFileURL(config_file).href}?ts=${Date.now()}`);
63
+
64
+ return process_config(config.default, { cwd });
65
+ }
66
+
67
+ /**
68
+ * @param {import('types').Config} config
69
+ * @returns {import('types').ValidatedConfig}
70
+ */
71
+ function process_config(config, { cwd = process.cwd() } = {}) {
72
+ const validated = validate_config(config);
73
+
74
+ validated.kit.outDir = path.resolve(cwd, validated.kit.outDir);
75
+
76
+ for (const key in validated.kit.files) {
77
+ // TODO remove for 1.0
78
+ if (key === 'template') continue;
79
+
80
+ if (key === 'hooks') {
81
+ validated.kit.files.hooks.client = path.resolve(cwd, validated.kit.files.hooks.client);
82
+ validated.kit.files.hooks.server = path.resolve(cwd, validated.kit.files.hooks.server);
83
+ } else {
84
+ // @ts-expect-error
85
+ validated.kit.files[key] = path.resolve(cwd, validated.kit.files[key]);
86
+ }
87
+ }
88
+
89
+ if (!fs.existsSync(validated.kit.files.errorTemplate)) {
90
+ validated.kit.files.errorTemplate = url.fileURLToPath(
91
+ new URL('./default-error.html', import.meta.url)
92
+ );
93
+ }
94
+
95
+ return validated;
96
+ }
97
+
98
+ /**
99
+ * @param {import('types').Config} config
100
+ * @returns {import('types').ValidatedConfig}
101
+ */
102
+ export function validate_config(config) {
103
+ if (typeof config !== 'object') {
104
+ throw new Error(
105
+ 'svelte.config.js must have a configuration object as its default export. See https://kit.svelte.dev/docs/configuration'
106
+ );
107
+ }
108
+
109
+ return options(config, 'config');
110
+ }
@@ -0,0 +1,504 @@
1
+ import { join } from 'path';
2
+
3
+ /** @typedef {import('./types').Validator} Validator */
4
+
5
+ const directives = object({
6
+ 'child-src': string_array(),
7
+ 'default-src': string_array(),
8
+ 'frame-src': string_array(),
9
+ 'worker-src': string_array(),
10
+ 'connect-src': string_array(),
11
+ 'font-src': string_array(),
12
+ 'img-src': string_array(),
13
+ 'manifest-src': string_array(),
14
+ 'media-src': string_array(),
15
+ 'object-src': string_array(),
16
+ 'prefetch-src': string_array(),
17
+ 'script-src': string_array(),
18
+ 'script-src-elem': string_array(),
19
+ 'script-src-attr': string_array(),
20
+ 'style-src': string_array(),
21
+ 'style-src-elem': string_array(),
22
+ 'style-src-attr': string_array(),
23
+ 'base-uri': string_array(),
24
+ sandbox: string_array(),
25
+ 'form-action': string_array(),
26
+ 'frame-ancestors': string_array(),
27
+ 'navigate-to': string_array(),
28
+ 'report-uri': string_array(),
29
+ 'report-to': string_array(),
30
+ 'require-trusted-types-for': string_array(),
31
+ 'trusted-types': string_array(),
32
+ 'upgrade-insecure-requests': boolean(false),
33
+ 'require-sri-for': string_array(),
34
+ 'block-all-mixed-content': boolean(false),
35
+ 'plugin-types': string_array(),
36
+ referrer: string_array()
37
+ });
38
+
39
+ /** @type {Validator} */
40
+ const options = object(
41
+ {
42
+ extensions: validate(['.svelte'], (input, keypath) => {
43
+ if (!Array.isArray(input) || !input.every((page) => typeof page === 'string')) {
44
+ throw new Error(`${keypath} must be an array of strings`);
45
+ }
46
+
47
+ input.forEach((extension) => {
48
+ if (extension[0] !== '.') {
49
+ throw new Error(`Each member of ${keypath} must start with '.' — saw '${extension}'`);
50
+ }
51
+
52
+ if (!/^(\.[a-z0-9]+)+$/i.test(extension)) {
53
+ throw new Error(`File extensions must be alphanumeric — saw '${extension}'`);
54
+ }
55
+ });
56
+
57
+ return input;
58
+ }),
59
+
60
+ kit: object({
61
+ adapter: validate(null, (input, keypath) => {
62
+ if (typeof input !== 'object' || !input.adapt) {
63
+ let message = `${keypath} should be an object with an "adapt" method`;
64
+
65
+ if (Array.isArray(input) || typeof input === 'string') {
66
+ // for the early adapter adopters
67
+ message += ', rather than the name of an adapter';
68
+ }
69
+
70
+ throw new Error(`${message}. See https://kit.svelte.dev/docs/adapters`);
71
+ }
72
+
73
+ return input;
74
+ }),
75
+
76
+ alias: validate({}, (input, keypath) => {
77
+ if (typeof input !== 'object') {
78
+ throw new Error(`${keypath} should be an object`);
79
+ }
80
+
81
+ for (const key in input) {
82
+ assert_string(input[key], `${keypath}.${key}`);
83
+ }
84
+
85
+ return input;
86
+ }),
87
+
88
+ // TODO: remove this for the 1.0 release
89
+ amp: error(
90
+ (keypath) =>
91
+ `${keypath} has been removed. See https://kit.svelte.dev/docs/seo#amp for details on how to support AMP`
92
+ ),
93
+
94
+ appDir: validate('_app', (input, keypath) => {
95
+ assert_string(input, keypath);
96
+
97
+ if (input) {
98
+ if (input.startsWith('/') || input.endsWith('/')) {
99
+ throw new Error(
100
+ "config.kit.appDir cannot start or end with '/'. See https://kit.svelte.dev/docs/configuration"
101
+ );
102
+ }
103
+ } else {
104
+ throw new Error(`${keypath} cannot be empty`);
105
+ }
106
+
107
+ return input;
108
+ }),
109
+
110
+ // TODO: remove this for the 1.0 release
111
+ browser: object({
112
+ hydrate: error(
113
+ (keypath) =>
114
+ `${keypath} has been removed. You can set it inside the top level +layout.js instead. See the PR for more information: https://github.com/sveltejs/kit/pull/6197`
115
+ ),
116
+ router: error(
117
+ (keypath) =>
118
+ `${keypath} has been removed. You can set it inside the top level +layout.js instead. See the PR for more information: https://github.com/sveltejs/kit/pull/6197`
119
+ )
120
+ }),
121
+
122
+ csp: object({
123
+ mode: list(['auto', 'hash', 'nonce']),
124
+ directives,
125
+ reportOnly: directives
126
+ }),
127
+
128
+ csrf: object({
129
+ checkOrigin: boolean(true)
130
+ }),
131
+
132
+ // TODO: remove this for the 1.0 release
133
+ endpointExtensions: error(
134
+ (keypath) => `${keypath} has been renamed to config.kit.moduleExtensions`
135
+ ),
136
+
137
+ env: object({
138
+ dir: string(process.cwd()),
139
+ publicPrefix: string('PUBLIC_')
140
+ }),
141
+
142
+ files: object({
143
+ assets: string('static'),
144
+ hooks: (input, keypath) => {
145
+ // TODO remove this for the 1.0 release
146
+ if (typeof input === 'string') {
147
+ throw new Error(
148
+ `${keypath} is an object with { server: string, client: string } now. See the PR for more information: https://github.com/sveltejs/kit/pull/6586`
149
+ );
150
+ }
151
+
152
+ return object({
153
+ client: string(join('src', 'hooks.client')),
154
+ server: string(join('src', 'hooks.server'))
155
+ })(input, keypath);
156
+ },
157
+ lib: string(join('src', 'lib')),
158
+ params: string(join('src', 'params')),
159
+ routes: string(join('src', 'routes')),
160
+ serviceWorker: string(join('src', 'service-worker')),
161
+ appTemplate: string(join('src', 'app.html')),
162
+ errorTemplate: string(join('src', 'error.html')),
163
+ // TODO: remove this for the 1.0 release
164
+ template: error(
165
+ () => 'config.kit.files.template has been renamed to config.kit.files.appTemplate'
166
+ )
167
+ }),
168
+
169
+ // TODO: remove this for the 1.0 release
170
+ headers: error(
171
+ (keypath) =>
172
+ `${keypath} has been removed. See https://github.com/sveltejs/kit/pull/3384 for details`
173
+ ),
174
+
175
+ // TODO: remove this for the 1.0 release
176
+ host: error(
177
+ (keypath) =>
178
+ `${keypath} has been removed. See https://github.com/sveltejs/kit/pull/3384 for details`
179
+ ),
180
+
181
+ // TODO remove for 1.0
182
+ hydrate: error((keypath) => `${keypath} has been moved to config.kit.browser.hydrate`),
183
+
184
+ inlineStyleThreshold: number(0),
185
+
186
+ methodOverride: error(
187
+ () =>
188
+ 'Method overrides have been removed in favor of actions. See the PR for more information: https://github.com/sveltejs/kit/pull/6469'
189
+ ),
190
+
191
+ moduleExtensions: string_array(['.js', '.ts']),
192
+
193
+ outDir: string('.svelte-kit'),
194
+
195
+ package: error((keypath) => `${keypath} has been removed — use @sveltejs/package instead`),
196
+
197
+ paths: object({
198
+ base: validate('', (input, keypath) => {
199
+ assert_string(input, keypath);
200
+
201
+ if (input !== '' && (input.endsWith('/') || !input.startsWith('/'))) {
202
+ throw new Error(
203
+ `${keypath} option must either be the empty string or a root-relative path that starts but doesn't end with '/'. See https://kit.svelte.dev/docs/configuration#paths`
204
+ );
205
+ }
206
+
207
+ return input;
208
+ }),
209
+ assets: validate('', (input, keypath) => {
210
+ assert_string(input, keypath);
211
+
212
+ if (input) {
213
+ if (!/^[a-z]+:\/\//.test(input)) {
214
+ throw new Error(
215
+ `${keypath} option must be an absolute path, if specified. See https://kit.svelte.dev/docs/configuration#paths`
216
+ );
217
+ }
218
+
219
+ if (input.endsWith('/')) {
220
+ throw new Error(
221
+ `${keypath} option must not end with '/'. See https://kit.svelte.dev/docs/configuration#paths`
222
+ );
223
+ }
224
+ }
225
+
226
+ return input;
227
+ })
228
+ }),
229
+
230
+ prerender: object({
231
+ concurrency: number(1),
232
+ crawl: boolean(true),
233
+ createIndexFiles: error(
234
+ (keypath) =>
235
+ `${keypath} has been removed — it is now controlled by the trailingSlash option. See https://kit.svelte.dev/docs/configuration#trailingslash`
236
+ ),
237
+ default: error(
238
+ (keypath) =>
239
+ `${keypath} has been removed. You can set it inside the top level +layout.js instead. See the PR for more information: https://github.com/sveltejs/kit/pull/6197`
240
+ ),
241
+ enabled: boolean(true),
242
+ entries: validate(['*'], (input, keypath) => {
243
+ if (!Array.isArray(input) || !input.every((page) => typeof page === 'string')) {
244
+ throw new Error(`${keypath} must be an array of strings`);
245
+ }
246
+
247
+ input.forEach((page) => {
248
+ if (page !== '*' && page[0] !== '/') {
249
+ throw new Error(
250
+ `Each member of ${keypath} must be either '*' or an absolute path beginning with '/' — saw '${page}'`
251
+ );
252
+ }
253
+ });
254
+
255
+ return input;
256
+ }),
257
+
258
+ // TODO: remove this for the 1.0 release
259
+ force: validate(undefined, (input, keypath) => {
260
+ if (typeof input !== 'undefined') {
261
+ const newSetting = input ? 'continue' : 'fail';
262
+ const needsSetting = newSetting === 'continue';
263
+ throw new Error(
264
+ `${keypath} has been removed in favor of \`onError\`. In your case, set \`onError\` to "${newSetting}"${
265
+ needsSetting ? '' : ' (or leave it undefined)'
266
+ } to get the same behavior as you would with \`force: ${JSON.stringify(input)}\``
267
+ );
268
+ }
269
+ }),
270
+
271
+ onError: validate('fail', (input, keypath) => {
272
+ if (typeof input === 'function') return input;
273
+ if (['continue', 'fail'].includes(input)) return input;
274
+ throw new Error(
275
+ `${keypath} should be either a custom function or one of "continue" or "fail"`
276
+ );
277
+ }),
278
+
279
+ origin: validate('http://sveltekit-prerender', (input, keypath) => {
280
+ assert_string(input, keypath);
281
+
282
+ let origin;
283
+
284
+ try {
285
+ origin = new URL(input).origin;
286
+ } catch (e) {
287
+ throw new Error(`${keypath} must be a valid origin`);
288
+ }
289
+
290
+ if (input !== origin) {
291
+ throw new Error(`${keypath} must be a valid origin (${origin} rather than ${input})`);
292
+ }
293
+
294
+ return origin;
295
+ }),
296
+
297
+ // TODO: remove this for the 1.0 release
298
+ pages: error((keypath) => `${keypath} has been renamed to \`entries\`.`)
299
+ }),
300
+
301
+ // TODO: remove this for the 1.0 release
302
+ protocol: error(
303
+ (keypath) =>
304
+ `${keypath} has been removed. See https://github.com/sveltejs/kit/pull/3384 for details`
305
+ ),
306
+
307
+ // TODO remove for 1.0
308
+ router: error((keypath) => `${keypath} has been moved to config.kit.browser.router`),
309
+
310
+ // TODO remove for 1.0
311
+ routes: error(
312
+ (keypath) =>
313
+ `${keypath} has been removed. See https://github.com/sveltejs/kit/discussions/5774 for details`
314
+ ),
315
+
316
+ serviceWorker: object({
317
+ register: boolean(true),
318
+ files: fun((filename) => !/\.DS_Store/.test(filename))
319
+ }),
320
+
321
+ // TODO remove this for 1.0
322
+ ssr: error(
323
+ (keypath) =>
324
+ `${keypath} has been removed — use the handle hook instead: https://kit.svelte.dev/docs/hooks#handle`
325
+ ),
326
+
327
+ // TODO remove this for 1.0
328
+ target: error((keypath) => `${keypath} is no longer required, and should be removed`),
329
+
330
+ trailingSlash: list(['never', 'always', 'ignore']),
331
+
332
+ version: object({
333
+ name: string(Date.now().toString()),
334
+ pollInterval: number(0)
335
+ }),
336
+
337
+ // TODO remove this for 1.0
338
+ vite: error((keypath) => `${keypath} has been removed — use vite.config.js instead`)
339
+ })
340
+ },
341
+ true
342
+ );
343
+
344
+ /**
345
+ * @param {Record<string, Validator>} children
346
+ * @param {boolean} [allow_unknown]
347
+ * @returns {Validator}
348
+ */
349
+ function object(children, allow_unknown = false) {
350
+ return (input, keypath) => {
351
+ /** @type {Record<string, any>} */
352
+ const output = {};
353
+
354
+ if ((input && typeof input !== 'object') || Array.isArray(input)) {
355
+ throw new Error(`${keypath} should be an object`);
356
+ }
357
+
358
+ for (const key in input) {
359
+ if (!(key in children)) {
360
+ if (allow_unknown) {
361
+ output[key] = input[key];
362
+ } else {
363
+ let message = `Unexpected option ${keypath}.${key}`;
364
+
365
+ // special case
366
+ if (keypath === 'config.kit' && key in options) {
367
+ message += ` (did you mean config.${key}?)`;
368
+ }
369
+
370
+ throw new Error(message);
371
+ }
372
+ }
373
+ }
374
+
375
+ for (const key in children) {
376
+ const validator = children[key];
377
+ output[key] = validator(input && input[key], `${keypath}.${key}`);
378
+ }
379
+
380
+ return output;
381
+ };
382
+ }
383
+
384
+ /**
385
+ * @param {any} fallback
386
+ * @param {(value: any, keypath: string) => any} fn
387
+ * @returns {Validator}
388
+ */
389
+ function validate(fallback, fn) {
390
+ return (input, keypath) => {
391
+ return input === undefined ? fallback : fn(input, keypath);
392
+ };
393
+ }
394
+
395
+ /**
396
+ * @param {string | null} fallback
397
+ * @param {boolean} allow_empty
398
+ * @returns {Validator}
399
+ */
400
+ function string(fallback, allow_empty = true) {
401
+ return validate(fallback, (input, keypath) => {
402
+ assert_string(input, keypath);
403
+
404
+ if (!allow_empty && input === '') {
405
+ throw new Error(`${keypath} cannot be empty`);
406
+ }
407
+
408
+ return input;
409
+ });
410
+ }
411
+
412
+ /**
413
+ * @param {string[] | undefined} [fallback]
414
+ * @returns {Validator}
415
+ */
416
+ function string_array(fallback) {
417
+ return validate(fallback, (input, keypath) => {
418
+ if (input === undefined) return input;
419
+
420
+ if (!Array.isArray(input) || input.some((value) => typeof value !== 'string')) {
421
+ throw new Error(`${keypath} must be an array of strings, if specified`);
422
+ }
423
+
424
+ return input;
425
+ });
426
+ }
427
+
428
+ /**
429
+ * @param {number} fallback
430
+ * @returns {Validator}
431
+ */
432
+ function number(fallback) {
433
+ return validate(fallback, (input, keypath) => {
434
+ if (typeof input !== 'number') {
435
+ throw new Error(`${keypath} should be a number, if specified`);
436
+ }
437
+ return input;
438
+ });
439
+ }
440
+
441
+ /**
442
+ * @param {boolean} fallback
443
+ * @returns {Validator}
444
+ */
445
+ function boolean(fallback) {
446
+ return validate(fallback, (input, keypath) => {
447
+ if (typeof input !== 'boolean') {
448
+ throw new Error(`${keypath} should be true or false, if specified`);
449
+ }
450
+ return input;
451
+ });
452
+ }
453
+
454
+ /**
455
+ * @param {string[]} options
456
+ * @returns {Validator}
457
+ */
458
+ function list(options, fallback = options[0]) {
459
+ return validate(fallback, (input, keypath) => {
460
+ if (!options.includes(input)) {
461
+ // prettier-ignore
462
+ const msg = options.length > 2
463
+ ? `${keypath} should be one of ${options.slice(0, -1).map(input => `"${input}"`).join(', ')} or "${options[options.length - 1]}"`
464
+ : `${keypath} should be either "${options[0]}" or "${options[1]}"`;
465
+
466
+ throw new Error(msg);
467
+ }
468
+ return input;
469
+ });
470
+ }
471
+
472
+ /**
473
+ * @param {(filename: string) => boolean} fallback
474
+ * @returns {Validator}
475
+ */
476
+ function fun(fallback) {
477
+ return validate(fallback, (input, keypath) => {
478
+ if (typeof input !== 'function') {
479
+ throw new Error(`${keypath} should be a function, if specified`);
480
+ }
481
+ return input;
482
+ });
483
+ }
484
+
485
+ /**
486
+ * @param {string} input
487
+ * @param {string} keypath
488
+ */
489
+ function assert_string(input, keypath) {
490
+ if (typeof input !== 'string') {
491
+ throw new Error(`${keypath} should be a string, if specified`);
492
+ }
493
+ }
494
+
495
+ /** @param {(keypath?: string) => string} fn */
496
+ function error(fn) {
497
+ return validate(undefined, (input, keypath) => {
498
+ if (input !== undefined) {
499
+ throw new Error(fn(keypath));
500
+ }
501
+ });
502
+ }
503
+
504
+ export default options;
@@ -0,0 +1 @@
1
+ export type Validator<T = any> = (input: T, keypath: string) => T;