@sveltejs/kit 1.0.0-next.43 → 1.0.0-next.430

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 (104) hide show
  1. package/README.md +12 -9
  2. package/package.json +95 -63
  3. package/src/cli.js +112 -0
  4. package/src/core/adapt/builder.js +207 -0
  5. package/src/core/adapt/index.js +19 -0
  6. package/src/core/config/index.js +86 -0
  7. package/src/core/config/options.js +488 -0
  8. package/src/core/config/types.d.ts +1 -0
  9. package/src/core/constants.js +5 -0
  10. package/src/core/env.js +97 -0
  11. package/src/core/generate_manifest/index.js +99 -0
  12. package/src/core/prerender/crawl.js +194 -0
  13. package/src/core/prerender/prerender.js +378 -0
  14. package/src/core/prerender/queue.js +80 -0
  15. package/src/core/sync/create_manifest_data/index.js +506 -0
  16. package/src/core/sync/create_manifest_data/types.d.ts +40 -0
  17. package/src/core/sync/sync.js +59 -0
  18. package/src/core/sync/utils.js +44 -0
  19. package/src/core/sync/write_ambient.js +27 -0
  20. package/src/core/sync/write_client_manifest.js +82 -0
  21. package/src/core/sync/write_matchers.js +25 -0
  22. package/src/core/sync/write_root.js +91 -0
  23. package/src/core/sync/write_tsconfig.js +195 -0
  24. package/src/core/sync/write_types.js +775 -0
  25. package/src/core/utils.js +70 -0
  26. package/src/hooks.js +26 -0
  27. package/src/index/index.js +45 -0
  28. package/src/index/private.js +33 -0
  29. package/src/node/index.js +145 -0
  30. package/src/node/polyfills.js +40 -0
  31. package/src/runtime/app/env.js +11 -0
  32. package/src/runtime/app/navigation.js +22 -0
  33. package/src/runtime/app/paths.js +1 -0
  34. package/src/runtime/app/stores.js +102 -0
  35. package/src/runtime/client/ambient.d.ts +17 -0
  36. package/src/runtime/client/client.js +1289 -0
  37. package/src/runtime/client/fetcher.js +60 -0
  38. package/src/runtime/client/parse.js +36 -0
  39. package/src/runtime/client/singletons.js +21 -0
  40. package/src/runtime/client/start.js +46 -0
  41. package/src/runtime/client/types.d.ts +105 -0
  42. package/src/runtime/client/utils.js +113 -0
  43. package/src/runtime/components/error.svelte +16 -0
  44. package/{assets → src/runtime}/components/layout.svelte +0 -0
  45. package/src/runtime/env/dynamic/private.js +1 -0
  46. package/src/runtime/env/dynamic/public.js +1 -0
  47. package/src/runtime/env-private.js +7 -0
  48. package/src/runtime/env-public.js +7 -0
  49. package/src/runtime/env.js +6 -0
  50. package/src/runtime/hash.js +16 -0
  51. package/src/runtime/paths.js +11 -0
  52. package/src/runtime/server/endpoint.js +58 -0
  53. package/src/runtime/server/index.js +448 -0
  54. package/src/runtime/server/page/cookie.js +25 -0
  55. package/src/runtime/server/page/crypto.js +239 -0
  56. package/src/runtime/server/page/csp.js +249 -0
  57. package/src/runtime/server/page/fetch.js +266 -0
  58. package/src/runtime/server/page/index.js +416 -0
  59. package/src/runtime/server/page/load_data.js +135 -0
  60. package/src/runtime/server/page/render.js +362 -0
  61. package/src/runtime/server/page/respond_with_error.js +94 -0
  62. package/src/runtime/server/page/types.d.ts +44 -0
  63. package/src/runtime/server/utils.js +116 -0
  64. package/src/utils/error.js +22 -0
  65. package/src/utils/escape.js +104 -0
  66. package/src/utils/filesystem.js +108 -0
  67. package/src/utils/http.js +55 -0
  68. package/src/utils/misc.js +1 -0
  69. package/src/utils/routing.js +108 -0
  70. package/src/utils/url.js +97 -0
  71. package/src/vite/build/build_server.js +337 -0
  72. package/src/vite/build/build_service_worker.js +90 -0
  73. package/src/vite/build/utils.js +160 -0
  74. package/src/vite/dev/index.js +551 -0
  75. package/src/vite/index.js +574 -0
  76. package/src/vite/preview/index.js +186 -0
  77. package/src/vite/types.d.ts +3 -0
  78. package/src/vite/utils.js +345 -0
  79. package/svelte-kit.js +1 -1
  80. package/types/ambient.d.ts +357 -0
  81. package/types/index.d.ts +343 -0
  82. package/types/internal.d.ts +308 -0
  83. package/types/private.d.ts +209 -0
  84. package/CHANGELOG.md +0 -431
  85. package/assets/components/error.svelte +0 -13
  86. package/assets/runtime/app/env.js +0 -5
  87. package/assets/runtime/app/navigation.js +0 -41
  88. package/assets/runtime/app/paths.js +0 -1
  89. package/assets/runtime/app/stores.js +0 -93
  90. package/assets/runtime/chunks/utils.js +0 -19
  91. package/assets/runtime/internal/singletons.js +0 -23
  92. package/assets/runtime/internal/start.js +0 -770
  93. package/assets/runtime/paths.js +0 -12
  94. package/dist/.DS_Store +0 -0
  95. package/dist/chunks/index.js +0 -3521
  96. package/dist/chunks/index2.js +0 -587
  97. package/dist/chunks/index3.js +0 -246
  98. package/dist/chunks/index4.js +0 -538
  99. package/dist/chunks/index5.js +0 -761
  100. package/dist/chunks/index6.js +0 -322
  101. package/dist/chunks/standard.js +0 -99
  102. package/dist/chunks/utils.js +0 -83
  103. package/dist/cli.js +0 -546
  104. package/dist/ssr.js +0 -2581
@@ -0,0 +1,488 @@
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
+ browser: validate({ hydrate: true, router: true }, (input, keypath) => {
111
+ const value = object({ hydrate: boolean(true), router: boolean(true) })(input, keypath);
112
+ if (!value.hydrate && value.router) {
113
+ throw new Error(
114
+ 'config.kit.browser.router cannot be true if config.kit.browser.hydrate is false'
115
+ );
116
+ }
117
+ return value;
118
+ }),
119
+
120
+ csp: object({
121
+ mode: list(['auto', 'hash', 'nonce']),
122
+ directives,
123
+ reportOnly: directives
124
+ }),
125
+
126
+ // TODO: remove this for the 1.0 release
127
+ endpointExtensions: error(
128
+ (keypath) => `${keypath} has been renamed to config.kit.moduleExtensions`
129
+ ),
130
+
131
+ env: object({
132
+ dir: string(process.cwd()),
133
+ publicPrefix: string('PUBLIC_')
134
+ }),
135
+
136
+ files: object({
137
+ assets: string('static'),
138
+ hooks: string(join('src', 'hooks')),
139
+ lib: string(join('src', 'lib')),
140
+ params: string(join('src', 'params')),
141
+ routes: string(join('src', 'routes')),
142
+ serviceWorker: string(join('src', 'service-worker')),
143
+ template: string(join('src', 'app.html'))
144
+ }),
145
+
146
+ // TODO: remove this for the 1.0 release
147
+ headers: error(
148
+ (keypath) =>
149
+ `${keypath} has been removed. See https://github.com/sveltejs/kit/pull/3384 for details`
150
+ ),
151
+
152
+ // TODO: remove this for the 1.0 release
153
+ host: error(
154
+ (keypath) =>
155
+ `${keypath} has been removed. See https://github.com/sveltejs/kit/pull/3384 for details`
156
+ ),
157
+
158
+ // TODO remove for 1.0
159
+ hydrate: error((keypath) => `${keypath} has been moved to config.kit.browser.hydrate`),
160
+
161
+ inlineStyleThreshold: number(0),
162
+
163
+ methodOverride: object({
164
+ parameter: string('_method'),
165
+ allowed: validate([], (input, keypath) => {
166
+ if (!Array.isArray(input) || !input.every((method) => typeof method === 'string')) {
167
+ throw new Error(`${keypath} must be an array of strings`);
168
+ }
169
+
170
+ if (input.map((i) => i.toUpperCase()).includes('GET')) {
171
+ throw new Error(`${keypath} cannot contain "GET"`);
172
+ }
173
+
174
+ return input;
175
+ })
176
+ }),
177
+
178
+ moduleExtensions: string_array(['.js', '.ts']),
179
+
180
+ outDir: string('.svelte-kit'),
181
+
182
+ package: error((keypath) => `${keypath} has been removed — use @sveltejs/package instead`),
183
+
184
+ paths: object({
185
+ base: validate('', (input, keypath) => {
186
+ assert_string(input, keypath);
187
+
188
+ if (input !== '' && (input.endsWith('/') || !input.startsWith('/'))) {
189
+ throw new Error(
190
+ `${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`
191
+ );
192
+ }
193
+
194
+ return input;
195
+ }),
196
+ assets: validate('', (input, keypath) => {
197
+ assert_string(input, keypath);
198
+
199
+ if (input) {
200
+ if (!/^[a-z]+:\/\//.test(input)) {
201
+ throw new Error(
202
+ `${keypath} option must be an absolute path, if specified. See https://kit.svelte.dev/docs/configuration#paths`
203
+ );
204
+ }
205
+
206
+ if (input.endsWith('/')) {
207
+ throw new Error(
208
+ `${keypath} option must not end with '/'. See https://kit.svelte.dev/docs/configuration#paths`
209
+ );
210
+ }
211
+ }
212
+
213
+ return input;
214
+ })
215
+ }),
216
+
217
+ prerender: object({
218
+ concurrency: number(1),
219
+ crawl: boolean(true),
220
+ createIndexFiles: error(
221
+ (keypath) =>
222
+ `${keypath} has been removed — it is now controlled by the trailingSlash option. See https://kit.svelte.dev/docs/configuration#trailingslash`
223
+ ),
224
+ default: boolean(false),
225
+ enabled: boolean(true),
226
+ entries: validate(['*'], (input, keypath) => {
227
+ if (!Array.isArray(input) || !input.every((page) => typeof page === 'string')) {
228
+ throw new Error(`${keypath} must be an array of strings`);
229
+ }
230
+
231
+ input.forEach((page) => {
232
+ if (page !== '*' && page[0] !== '/') {
233
+ throw new Error(
234
+ `Each member of ${keypath} must be either '*' or an absolute path beginning with '/' — saw '${page}'`
235
+ );
236
+ }
237
+ });
238
+
239
+ return input;
240
+ }),
241
+
242
+ // TODO: remove this for the 1.0 release
243
+ force: validate(undefined, (input, keypath) => {
244
+ if (typeof input !== 'undefined') {
245
+ const newSetting = input ? 'continue' : 'fail';
246
+ const needsSetting = newSetting === 'continue';
247
+ throw new Error(
248
+ `${keypath} has been removed in favor of \`onError\`. In your case, set \`onError\` to "${newSetting}"${
249
+ needsSetting ? '' : ' (or leave it undefined)'
250
+ } to get the same behavior as you would with \`force: ${JSON.stringify(input)}\``
251
+ );
252
+ }
253
+ }),
254
+
255
+ onError: validate('fail', (input, keypath) => {
256
+ if (typeof input === 'function') return input;
257
+ if (['continue', 'fail'].includes(input)) return input;
258
+ throw new Error(
259
+ `${keypath} should be either a custom function or one of "continue" or "fail"`
260
+ );
261
+ }),
262
+
263
+ origin: validate('http://sveltekit-prerender', (input, keypath) => {
264
+ assert_string(input, keypath);
265
+
266
+ let origin;
267
+
268
+ try {
269
+ origin = new URL(input).origin;
270
+ } catch (e) {
271
+ throw new Error(`${keypath} must be a valid origin`);
272
+ }
273
+
274
+ if (input !== origin) {
275
+ throw new Error(`${keypath} must be a valid origin (${origin} rather than ${input})`);
276
+ }
277
+
278
+ return origin;
279
+ }),
280
+
281
+ // TODO: remove this for the 1.0 release
282
+ pages: error((keypath) => `${keypath} has been renamed to \`entries\`.`)
283
+ }),
284
+
285
+ // TODO: remove this for the 1.0 release
286
+ protocol: error(
287
+ (keypath) =>
288
+ `${keypath} has been removed. See https://github.com/sveltejs/kit/pull/3384 for details`
289
+ ),
290
+
291
+ // TODO remove for 1.0
292
+ router: error((keypath) => `${keypath} has been moved to config.kit.browser.router`),
293
+
294
+ // TODO remove for 1.0
295
+ routes: error(
296
+ (keypath) =>
297
+ `${keypath} has been removed. See https://github.com/sveltejs/kit/discussions/5774 for details`
298
+ ),
299
+
300
+ serviceWorker: object({
301
+ register: boolean(true),
302
+ files: fun((filename) => !/\.DS_Store/.test(filename))
303
+ }),
304
+
305
+ // TODO remove this for 1.0
306
+ ssr: error(
307
+ (keypath) =>
308
+ `${keypath} has been removed — use the handle hook instead: https://kit.svelte.dev/docs/hooks#handle`
309
+ ),
310
+
311
+ // TODO remove this for 1.0
312
+ target: error((keypath) => `${keypath} is no longer required, and should be removed`),
313
+
314
+ trailingSlash: list(['never', 'always', 'ignore']),
315
+
316
+ version: object({
317
+ name: string(Date.now().toString()),
318
+ pollInterval: number(0)
319
+ }),
320
+
321
+ // TODO remove this for 1.0
322
+ vite: error((keypath) => `${keypath} has been removed — use vite.config.js instead`)
323
+ })
324
+ },
325
+ true
326
+ );
327
+
328
+ /**
329
+ * @param {Record<string, Validator>} children
330
+ * @param {boolean} [allow_unknown]
331
+ * @returns {Validator}
332
+ */
333
+ function object(children, allow_unknown = false) {
334
+ return (input, keypath) => {
335
+ /** @type {Record<string, any>} */
336
+ const output = {};
337
+
338
+ if ((input && typeof input !== 'object') || Array.isArray(input)) {
339
+ throw new Error(`${keypath} should be an object`);
340
+ }
341
+
342
+ for (const key in input) {
343
+ if (!(key in children)) {
344
+ if (allow_unknown) {
345
+ output[key] = input[key];
346
+ } else {
347
+ let message = `Unexpected option ${keypath}.${key}`;
348
+
349
+ // special case
350
+ if (keypath === 'config.kit' && key in options) {
351
+ message += ` (did you mean config.${key}?)`;
352
+ }
353
+
354
+ throw new Error(message);
355
+ }
356
+ }
357
+ }
358
+
359
+ for (const key in children) {
360
+ const validator = children[key];
361
+ output[key] = validator(input && input[key], `${keypath}.${key}`);
362
+ }
363
+
364
+ return output;
365
+ };
366
+ }
367
+
368
+ /**
369
+ * @param {any} fallback
370
+ * @param {(value: any, keypath: string) => any} fn
371
+ * @returns {Validator}
372
+ */
373
+ function validate(fallback, fn) {
374
+ return (input, keypath) => {
375
+ return input === undefined ? fallback : fn(input, keypath);
376
+ };
377
+ }
378
+
379
+ /**
380
+ * @param {string | null} fallback
381
+ * @param {boolean} allow_empty
382
+ * @returns {Validator}
383
+ */
384
+ function string(fallback, allow_empty = true) {
385
+ return validate(fallback, (input, keypath) => {
386
+ assert_string(input, keypath);
387
+
388
+ if (!allow_empty && input === '') {
389
+ throw new Error(`${keypath} cannot be empty`);
390
+ }
391
+
392
+ return input;
393
+ });
394
+ }
395
+
396
+ /**
397
+ * @param {string[] | undefined} [fallback]
398
+ * @returns {Validator}
399
+ */
400
+ function string_array(fallback) {
401
+ return validate(fallback, (input, keypath) => {
402
+ if (input === undefined) return input;
403
+
404
+ if (!Array.isArray(input) || input.some((value) => typeof value !== 'string')) {
405
+ throw new Error(`${keypath} must be an array of strings, if specified`);
406
+ }
407
+
408
+ return input;
409
+ });
410
+ }
411
+
412
+ /**
413
+ * @param {number} fallback
414
+ * @returns {Validator}
415
+ */
416
+ function number(fallback) {
417
+ return validate(fallback, (input, keypath) => {
418
+ if (typeof input !== 'number') {
419
+ throw new Error(`${keypath} should be a number, if specified`);
420
+ }
421
+ return input;
422
+ });
423
+ }
424
+
425
+ /**
426
+ * @param {boolean} fallback
427
+ * @returns {Validator}
428
+ */
429
+ function boolean(fallback) {
430
+ return validate(fallback, (input, keypath) => {
431
+ if (typeof input !== 'boolean') {
432
+ throw new Error(`${keypath} should be true or false, if specified`);
433
+ }
434
+ return input;
435
+ });
436
+ }
437
+
438
+ /**
439
+ * @param {string[]} options
440
+ * @returns {Validator}
441
+ */
442
+ function list(options, fallback = options[0]) {
443
+ return validate(fallback, (input, keypath) => {
444
+ if (!options.includes(input)) {
445
+ // prettier-ignore
446
+ const msg = options.length > 2
447
+ ? `${keypath} should be one of ${options.slice(0, -1).map(input => `"${input}"`).join(', ')} or "${options[options.length - 1]}"`
448
+ : `${keypath} should be either "${options[0]}" or "${options[1]}"`;
449
+
450
+ throw new Error(msg);
451
+ }
452
+ return input;
453
+ });
454
+ }
455
+
456
+ /**
457
+ * @param {(filename: string) => boolean} fallback
458
+ * @returns {Validator}
459
+ */
460
+ function fun(fallback) {
461
+ return validate(fallback, (input, keypath) => {
462
+ if (typeof input !== 'function') {
463
+ throw new Error(`${keypath} should be a function, if specified`);
464
+ }
465
+ return input;
466
+ });
467
+ }
468
+
469
+ /**
470
+ * @param {string} input
471
+ * @param {string} keypath
472
+ */
473
+ function assert_string(input, keypath) {
474
+ if (typeof input !== 'string') {
475
+ throw new Error(`${keypath} should be a string, if specified`);
476
+ }
477
+ }
478
+
479
+ /** @param {(keypath?: string) => string} fn */
480
+ function error(fn) {
481
+ return validate(undefined, (input, keypath) => {
482
+ if (input !== undefined) {
483
+ throw new Error(fn(keypath));
484
+ }
485
+ });
486
+ }
487
+
488
+ export default options;
@@ -0,0 +1 @@
1
+ export type Validator<T = any> = (input: T, keypath: string) => T;
@@ -0,0 +1,5 @@
1
+ // in `vite dev` and `vite preview`, we use a fake asset path so that we can
2
+ // serve local assets while verifying that requests are correctly prefixed
3
+ export const SVELTE_KIT_ASSETS = '/_svelte_kit_assets';
4
+
5
+ export const GENERATED_COMMENT = '// this file is generated — do not edit it\n';
@@ -0,0 +1,97 @@
1
+ import { GENERATED_COMMENT } from './constants.js';
2
+ import { runtime_base } from './utils.js';
3
+
4
+ /**
5
+ * @param {string} id
6
+ * @param {Record<string, string>} env
7
+ * @returns {string}
8
+ */
9
+ export function create_static_module(id, env) {
10
+ /** @type {string[]} */
11
+ const declarations = [];
12
+
13
+ for (const key in env) {
14
+ if (!valid_identifier.test(key) || reserved.has(key)) {
15
+ continue;
16
+ }
17
+
18
+ const comment = `/** @type {import('${id}').${key}} */`;
19
+ const declaration = `export const ${key} = ${JSON.stringify(env[key])};`;
20
+
21
+ declarations.push(`${comment}\n${declaration}`);
22
+ }
23
+
24
+ return GENERATED_COMMENT + declarations.join('\n\n');
25
+ }
26
+
27
+ /** @param {'public' | 'private'} type */
28
+ export function create_dynamic_module(type) {
29
+ return `export { env } from '${runtime_base}/env-${type}.js';`;
30
+ }
31
+
32
+ /**
33
+ * @param {string} id
34
+ * @param {Record<string, string>} env
35
+ * @returns {string}
36
+ */
37
+ export function create_types(id, env) {
38
+ const declarations = Object.keys(env)
39
+ .filter((k) => valid_identifier.test(k))
40
+ .map((k) => `\texport const ${k}: string;`)
41
+ .join('\n');
42
+
43
+ return `declare module '${id}' {\n${declarations}\n}`;
44
+ }
45
+
46
+ export const reserved = new Set([
47
+ 'do',
48
+ 'if',
49
+ 'in',
50
+ 'for',
51
+ 'let',
52
+ 'new',
53
+ 'try',
54
+ 'var',
55
+ 'case',
56
+ 'else',
57
+ 'enum',
58
+ 'eval',
59
+ 'null',
60
+ 'this',
61
+ 'true',
62
+ 'void',
63
+ 'with',
64
+ 'await',
65
+ 'break',
66
+ 'catch',
67
+ 'class',
68
+ 'const',
69
+ 'false',
70
+ 'super',
71
+ 'throw',
72
+ 'while',
73
+ 'yield',
74
+ 'delete',
75
+ 'export',
76
+ 'import',
77
+ 'public',
78
+ 'return',
79
+ 'static',
80
+ 'switch',
81
+ 'typeof',
82
+ 'default',
83
+ 'extends',
84
+ 'finally',
85
+ 'package',
86
+ 'private',
87
+ 'continue',
88
+ 'debugger',
89
+ 'function',
90
+ 'arguments',
91
+ 'interface',
92
+ 'protected',
93
+ 'implements',
94
+ 'instanceof'
95
+ ]);
96
+
97
+ export const valid_identifier = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
@@ -0,0 +1,99 @@
1
+ import { s } from '../../utils/misc.js';
2
+ import { parse_route_id } from '../../utils/routing.js';
3
+ import { get_mime_lookup } from '../utils.js';
4
+
5
+ /**
6
+ * Generates the data used to write the server-side manifest.js file. This data is used in the Vite
7
+ * build process, to power routing, etc.
8
+ * @param {{
9
+ * build_data: import('types').BuildData;
10
+ * relative_path: string;
11
+ * routes: import('types').RouteData[];
12
+ * format?: 'esm' | 'cjs'
13
+ * }} opts
14
+ */
15
+ export function generate_manifest({ build_data, relative_path, routes, format = 'esm' }) {
16
+ /** @typedef {{ index: number, path: string }} LookupEntry */
17
+ /** @type {Map<import('types').PageNode, LookupEntry>} */
18
+ const bundled_nodes = new Map();
19
+
20
+ build_data.manifest_data.nodes.forEach((node, i) => {
21
+ bundled_nodes.set(node, {
22
+ path: `${relative_path}/nodes/${i}.js`,
23
+ index: i
24
+ });
25
+ });
26
+
27
+ /** @type {(path: string) => string} */
28
+ const load =
29
+ format === 'esm'
30
+ ? (path) => `import('${path}')`
31
+ : (path) => `Promise.resolve().then(() => require('${path}'))`;
32
+
33
+ /** @type {(path: string) => string} */
34
+ const loader = (path) => `() => ${load(path)}`;
35
+
36
+ const assets = build_data.manifest_data.assets.map((asset) => asset.file);
37
+ if (build_data.service_worker) {
38
+ assets.push(build_data.service_worker);
39
+ }
40
+
41
+ /** @param {import('types').PageNode | undefined} id */
42
+ const get_index = (id) => id && /** @type {LookupEntry} */ (bundled_nodes.get(id)).index;
43
+
44
+ const matchers = new Set();
45
+
46
+ // prettier-ignore
47
+ return `{
48
+ appDir: ${s(build_data.app_dir)},
49
+ assets: new Set(${s(assets)}),
50
+ mimeTypes: ${s(get_mime_lookup(build_data.manifest_data))},
51
+ _: {
52
+ entry: ${s(build_data.client.entry)},
53
+ nodes: [
54
+ ${Array.from(bundled_nodes.values()).map(node => loader(node.path)).join(',\n\t\t\t\t')}
55
+ ],
56
+ routes: [
57
+ ${routes.map(route => {
58
+ const { pattern, names, types } = parse_route_id(route.id);
59
+
60
+ types.forEach(type => {
61
+ if (type) matchers.add(type);
62
+ });
63
+
64
+ if (route.type === 'page') {
65
+ return `{
66
+ type: 'page',
67
+ id: ${s(route.id)},
68
+ pattern: ${pattern},
69
+ names: ${s(names)},
70
+ types: ${s(types)},
71
+ errors: ${s(route.errors.map(get_index))},
72
+ layouts: ${s(route.layouts.map(get_index))},
73
+ leaf: ${s(get_index(route.leaf))}
74
+ }`.replace(/^\t\t/gm, '');
75
+ } else {
76
+ if (!build_data.server.vite_manifest[route.file]) {
77
+ // this is necessary in cases where a .css file snuck in —
78
+ // perhaps it would be better to disallow these (and others?)
79
+ return null;
80
+ }
81
+
82
+ return `{
83
+ type: 'endpoint',
84
+ id: ${s(route.id)},
85
+ pattern: ${pattern},
86
+ names: ${s(names)},
87
+ types: ${s(types)},
88
+ load: ${loader(`${relative_path}/${build_data.server.vite_manifest[route.file].file}`)}
89
+ }`.replace(/^\t\t/gm, '');
90
+ }
91
+ }).filter(Boolean).join(',\n\t\t\t\t')}
92
+ ],
93
+ matchers: async () => {
94
+ ${Array.from(matchers).map(type => `const { match: ${type} } = await ${load(`${relative_path}/entries/matchers/${type}.js`)}`).join('\n\t\t\t\t')}
95
+ return { ${Array.from(matchers).join(', ')} };
96
+ }
97
+ }
98
+ }`.replace(/^\t/gm, '');
99
+ }