@sveltejs/kit 1.0.0-next.393 → 1.0.0-next.394

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.
@@ -1,673 +1,3 @@
1
- import fs__default from 'fs';
2
- import path__default, { join } from 'path';
3
- import * as url from 'url';
4
-
5
- let FORCE_COLOR, NODE_DISABLE_COLORS, NO_COLOR, TERM, isTTY=true;
6
- if (typeof process !== 'undefined') {
7
- ({ FORCE_COLOR, NODE_DISABLE_COLORS, NO_COLOR, TERM } = process.env || {});
8
- isTTY = process.stdout && process.stdout.isTTY;
9
- }
10
-
11
- const $ = {
12
- enabled: !NODE_DISABLE_COLORS && NO_COLOR == null && TERM !== 'dumb' && (
13
- FORCE_COLOR != null && FORCE_COLOR !== '0' || isTTY
14
- ),
15
-
16
- // modifiers
17
- reset: init(0, 0),
18
- bold: init(1, 22),
19
- dim: init(2, 22),
20
- italic: init(3, 23),
21
- underline: init(4, 24),
22
- inverse: init(7, 27),
23
- hidden: init(8, 28),
24
- strikethrough: init(9, 29),
25
-
26
- // colors
27
- black: init(30, 39),
28
- red: init(31, 39),
29
- green: init(32, 39),
30
- yellow: init(33, 39),
31
- blue: init(34, 39),
32
- magenta: init(35, 39),
33
- cyan: init(36, 39),
34
- white: init(37, 39),
35
- gray: init(90, 39),
36
- grey: init(90, 39),
37
-
38
- // background colors
39
- bgBlack: init(40, 49),
40
- bgRed: init(41, 49),
41
- bgGreen: init(42, 49),
42
- bgYellow: init(43, 49),
43
- bgBlue: init(44, 49),
44
- bgMagenta: init(45, 49),
45
- bgCyan: init(46, 49),
46
- bgWhite: init(47, 49)
47
- };
48
-
49
- function run(arr, str) {
50
- let i=0, tmp, beg='', end='';
51
- for (; i < arr.length; i++) {
52
- tmp = arr[i];
53
- beg += tmp.open;
54
- end += tmp.close;
55
- if (!!~str.indexOf(tmp.close)) {
56
- str = str.replace(tmp.rgx, tmp.close + tmp.open);
57
- }
58
- }
59
- return beg + str + end;
60
- }
61
-
62
- function chain(has, keys) {
63
- let ctx = { has, keys };
64
-
65
- ctx.reset = $.reset.bind(ctx);
66
- ctx.bold = $.bold.bind(ctx);
67
- ctx.dim = $.dim.bind(ctx);
68
- ctx.italic = $.italic.bind(ctx);
69
- ctx.underline = $.underline.bind(ctx);
70
- ctx.inverse = $.inverse.bind(ctx);
71
- ctx.hidden = $.hidden.bind(ctx);
72
- ctx.strikethrough = $.strikethrough.bind(ctx);
73
-
74
- ctx.black = $.black.bind(ctx);
75
- ctx.red = $.red.bind(ctx);
76
- ctx.green = $.green.bind(ctx);
77
- ctx.yellow = $.yellow.bind(ctx);
78
- ctx.blue = $.blue.bind(ctx);
79
- ctx.magenta = $.magenta.bind(ctx);
80
- ctx.cyan = $.cyan.bind(ctx);
81
- ctx.white = $.white.bind(ctx);
82
- ctx.gray = $.gray.bind(ctx);
83
- ctx.grey = $.grey.bind(ctx);
84
-
85
- ctx.bgBlack = $.bgBlack.bind(ctx);
86
- ctx.bgRed = $.bgRed.bind(ctx);
87
- ctx.bgGreen = $.bgGreen.bind(ctx);
88
- ctx.bgYellow = $.bgYellow.bind(ctx);
89
- ctx.bgBlue = $.bgBlue.bind(ctx);
90
- ctx.bgMagenta = $.bgMagenta.bind(ctx);
91
- ctx.bgCyan = $.bgCyan.bind(ctx);
92
- ctx.bgWhite = $.bgWhite.bind(ctx);
93
-
94
- return ctx;
95
- }
96
-
97
- function init(open, close) {
98
- let blk = {
99
- open: `\x1b[${open}m`,
100
- close: `\x1b[${close}m`,
101
- rgx: new RegExp(`\\x1b\\[${close}m`, 'g')
102
- };
103
- return function (txt) {
104
- if (this !== void 0 && this.has !== void 0) {
105
- !!~this.has.indexOf(open) || (this.has.push(open),this.keys.push(blk));
106
- return txt === void 0 ? this : $.enabled ? run(this.keys, txt+'') : txt+'';
107
- }
108
- return txt === void 0 ? chain([open], [blk]) : $.enabled ? run([blk], txt+'') : txt+'';
109
- };
110
- }
111
-
112
- /** @typedef {import('./types').Validator} Validator */
113
-
114
- const directives = object({
115
- 'child-src': string_array(),
116
- 'default-src': string_array(),
117
- 'frame-src': string_array(),
118
- 'worker-src': string_array(),
119
- 'connect-src': string_array(),
120
- 'font-src': string_array(),
121
- 'img-src': string_array(),
122
- 'manifest-src': string_array(),
123
- 'media-src': string_array(),
124
- 'object-src': string_array(),
125
- 'prefetch-src': string_array(),
126
- 'script-src': string_array(),
127
- 'script-src-elem': string_array(),
128
- 'script-src-attr': string_array(),
129
- 'style-src': string_array(),
130
- 'style-src-elem': string_array(),
131
- 'style-src-attr': string_array(),
132
- 'base-uri': string_array(),
133
- sandbox: string_array(),
134
- 'form-action': string_array(),
135
- 'frame-ancestors': string_array(),
136
- 'navigate-to': string_array(),
137
- 'report-uri': string_array(),
138
- 'report-to': string_array(),
139
- 'require-trusted-types-for': string_array(),
140
- 'trusted-types': string_array(),
141
- 'upgrade-insecure-requests': boolean(false),
142
- 'require-sri-for': string_array(),
143
- 'block-all-mixed-content': boolean(false),
144
- 'plugin-types': string_array(),
145
- referrer: string_array()
146
- });
147
-
148
- /** @type {Validator} */
149
- const options = object(
150
- {
151
- extensions: validate(['.svelte'], (input, keypath) => {
152
- if (!Array.isArray(input) || !input.every((page) => typeof page === 'string')) {
153
- throw new Error(`${keypath} must be an array of strings`);
154
- }
155
-
156
- input.forEach((extension) => {
157
- if (extension[0] !== '.') {
158
- throw new Error(`Each member of ${keypath} must start with '.' — saw '${extension}'`);
159
- }
160
-
161
- if (!/^(\.[a-z0-9]+)+$/i.test(extension)) {
162
- throw new Error(`File extensions must be alphanumeric — saw '${extension}'`);
163
- }
164
- });
165
-
166
- return input;
167
- }),
168
-
169
- kit: object({
170
- adapter: validate(null, (input, keypath) => {
171
- if (typeof input !== 'object' || !input.adapt) {
172
- let message = `${keypath} should be an object with an "adapt" method`;
173
-
174
- if (Array.isArray(input) || typeof input === 'string') {
175
- // for the early adapter adopters
176
- message += ', rather than the name of an adapter';
177
- }
178
-
179
- throw new Error(`${message}. See https://kit.svelte.dev/docs/adapters`);
180
- }
181
-
182
- return input;
183
- }),
184
-
185
- alias: validate({}, (input, keypath) => {
186
- if (typeof input !== 'object') {
187
- throw new Error(`${keypath} should be an object`);
188
- }
189
-
190
- for (const key in input) {
191
- assert_string(input[key], `${keypath}.${key}`);
192
- }
193
-
194
- return input;
195
- }),
196
-
197
- // TODO: remove this for the 1.0 release
198
- amp: error(
199
- (keypath) =>
200
- `${keypath} has been removed. See https://kit.svelte.dev/docs/seo#amp for details on how to support AMP`
201
- ),
202
-
203
- appDir: validate('_app', (input, keypath) => {
204
- assert_string(input, keypath);
205
-
206
- if (input) {
207
- if (input.startsWith('/') || input.endsWith('/')) {
208
- throw new Error(
209
- "config.kit.appDir cannot start or end with '/'. See https://kit.svelte.dev/docs/configuration"
210
- );
211
- }
212
- } else {
213
- throw new Error(`${keypath} cannot be empty`);
214
- }
215
-
216
- return input;
217
- }),
218
-
219
- browser: object({
220
- hydrate: boolean(true),
221
- router: boolean(true)
222
- }),
223
-
224
- csp: object({
225
- mode: list(['auto', 'hash', 'nonce']),
226
- directives,
227
- reportOnly: directives
228
- }),
229
-
230
- // TODO: remove this for the 1.0 release
231
- endpointExtensions: error(
232
- (keypath) => `${keypath} has been renamed to config.kit.moduleExtensions`
233
- ),
234
-
235
- files: object({
236
- assets: string('static'),
237
- hooks: string(join('src', 'hooks')),
238
- lib: string(join('src', 'lib')),
239
- params: string(join('src', 'params')),
240
- routes: string(join('src', 'routes')),
241
- serviceWorker: string(join('src', 'service-worker')),
242
- template: string(join('src', 'app.html'))
243
- }),
244
-
245
- // TODO: remove this for the 1.0 release
246
- headers: error(
247
- (keypath) =>
248
- `${keypath} has been removed. See https://github.com/sveltejs/kit/pull/3384 for details`
249
- ),
250
-
251
- // TODO: remove this for the 1.0 release
252
- host: error(
253
- (keypath) =>
254
- `${keypath} has been removed. See https://github.com/sveltejs/kit/pull/3384 for details`
255
- ),
256
-
257
- // TODO remove for 1.0
258
- hydrate: error((keypath) => `${keypath} has been moved to config.kit.browser.hydrate`),
259
-
260
- inlineStyleThreshold: number(0),
261
-
262
- methodOverride: object({
263
- parameter: string('_method'),
264
- allowed: validate([], (input, keypath) => {
265
- if (!Array.isArray(input) || !input.every((method) => typeof method === 'string')) {
266
- throw new Error(`${keypath} must be an array of strings`);
267
- }
268
-
269
- if (input.map((i) => i.toUpperCase()).includes('GET')) {
270
- throw new Error(`${keypath} cannot contain "GET"`);
271
- }
272
-
273
- return input;
274
- })
275
- }),
276
-
277
- moduleExtensions: string_array(['.js', '.ts']),
278
-
279
- outDir: string('.svelte-kit'),
280
-
281
- package: object({
282
- dir: string('package'),
283
- // excludes all .d.ts and filename starting with _
284
- exports: fun((filepath) => !/^_|\/_|\.d\.ts$/.test(filepath)),
285
- files: fun(() => true),
286
- emitTypes: boolean(true)
287
- }),
288
-
289
- paths: object({
290
- base: validate('', (input, keypath) => {
291
- assert_string(input, keypath);
292
-
293
- if (input !== '' && (input.endsWith('/') || !input.startsWith('/'))) {
294
- throw new Error(
295
- `${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`
296
- );
297
- }
298
-
299
- return input;
300
- }),
301
- assets: validate('', (input, keypath) => {
302
- assert_string(input, keypath);
303
-
304
- if (input) {
305
- if (!/^[a-z]+:\/\//.test(input)) {
306
- throw new Error(
307
- `${keypath} option must be an absolute path, if specified. See https://kit.svelte.dev/docs/configuration#paths`
308
- );
309
- }
310
-
311
- if (input.endsWith('/')) {
312
- throw new Error(
313
- `${keypath} option must not end with '/'. See https://kit.svelte.dev/docs/configuration#paths`
314
- );
315
- }
316
- }
317
-
318
- return input;
319
- })
320
- }),
321
-
322
- prerender: object({
323
- concurrency: number(1),
324
- crawl: boolean(true),
325
- createIndexFiles: error(
326
- (keypath) =>
327
- `${keypath} has been removed — it is now controlled by the trailingSlash option. See https://kit.svelte.dev/docs/configuration#trailingslash`
328
- ),
329
- default: boolean(false),
330
- enabled: boolean(true),
331
- entries: validate(['*'], (input, keypath) => {
332
- if (!Array.isArray(input) || !input.every((page) => typeof page === 'string')) {
333
- throw new Error(`${keypath} must be an array of strings`);
334
- }
335
-
336
- input.forEach((page) => {
337
- if (page !== '*' && page[0] !== '/') {
338
- throw new Error(
339
- `Each member of ${keypath} must be either '*' or an absolute path beginning with '/' — saw '${page}'`
340
- );
341
- }
342
- });
343
-
344
- return input;
345
- }),
346
-
347
- // TODO: remove this for the 1.0 release
348
- force: validate(undefined, (input, keypath) => {
349
- if (typeof input !== 'undefined') {
350
- const newSetting = input ? 'continue' : 'fail';
351
- const needsSetting = newSetting === 'continue';
352
- throw new Error(
353
- `${keypath} has been removed in favor of \`onError\`. In your case, set \`onError\` to "${newSetting}"${
354
- needsSetting ? '' : ' (or leave it undefined)'
355
- } to get the same behavior as you would with \`force: ${JSON.stringify(input)}\``
356
- );
357
- }
358
- }),
359
-
360
- onError: validate('fail', (input, keypath) => {
361
- if (typeof input === 'function') return input;
362
- if (['continue', 'fail'].includes(input)) return input;
363
- throw new Error(
364
- `${keypath} should be either a custom function or one of "continue" or "fail"`
365
- );
366
- }),
367
-
368
- origin: validate('http://sveltekit-prerender', (input, keypath) => {
369
- assert_string(input, keypath);
370
-
371
- let origin;
372
-
373
- try {
374
- origin = new URL(input).origin;
375
- } catch (e) {
376
- throw new Error(`${keypath} must be a valid origin`);
377
- }
378
-
379
- if (input !== origin) {
380
- throw new Error(`${keypath} must be a valid origin (${origin} rather than ${input})`);
381
- }
382
-
383
- return origin;
384
- }),
385
-
386
- // TODO: remove this for the 1.0 release
387
- pages: error((keypath) => `${keypath} has been renamed to \`entries\`.`)
388
- }),
389
-
390
- // TODO: remove this for the 1.0 release
391
- protocol: error(
392
- (keypath) =>
393
- `${keypath} has been removed. See https://github.com/sveltejs/kit/pull/3384 for details`
394
- ),
395
-
396
- // TODO remove for 1.0
397
- router: error((keypath) => `${keypath} has been moved to config.kit.browser.router`),
398
-
399
- routes: fun((filepath) => !/(?:(?:^_|\/_)|(?:^\.|\/\.)(?!well-known))/.test(filepath)),
400
-
401
- serviceWorker: object({
402
- register: boolean(true),
403
- files: fun((filename) => !/\.DS_Store/.test(filename))
404
- }),
405
-
406
- // TODO remove this for 1.0
407
- ssr: error(
408
- (keypath) =>
409
- `${keypath} has been removed — use the handle hook instead: https://kit.svelte.dev/docs/hooks#handle`
410
- ),
411
-
412
- // TODO remove this for 1.0
413
- target: error((keypath) => `${keypath} is no longer required, and should be removed`),
414
-
415
- trailingSlash: list(['never', 'always', 'ignore']),
416
-
417
- version: object({
418
- name: string(Date.now().toString()),
419
- pollInterval: number(0)
420
- }),
421
-
422
- // TODO remove this for 1.0
423
- vite: error((keypath) => `${keypath} has been removed — use vite.config.js instead`)
424
- })
425
- },
426
- true
427
- );
428
-
429
- /**
430
- * @param {Record<string, Validator>} children
431
- * @param {boolean} [allow_unknown]
432
- * @returns {Validator}
433
- */
434
- function object(children, allow_unknown = false) {
435
- return (input, keypath) => {
436
- /** @type {Record<string, any>} */
437
- const output = {};
438
-
439
- if ((input && typeof input !== 'object') || Array.isArray(input)) {
440
- throw new Error(`${keypath} should be an object`);
441
- }
442
-
443
- for (const key in input) {
444
- if (!(key in children)) {
445
- if (allow_unknown) {
446
- output[key] = input[key];
447
- } else {
448
- let message = `Unexpected option ${keypath}.${key}`;
449
-
450
- // special case
451
- if (keypath === 'config.kit' && key in options) {
452
- message += ` (did you mean config.${key}?)`;
453
- }
454
-
455
- throw new Error(message);
456
- }
457
- }
458
- }
459
-
460
- for (const key in children) {
461
- const validator = children[key];
462
- output[key] = validator(input && input[key], `${keypath}.${key}`);
463
- }
464
-
465
- return output;
466
- };
467
- }
468
-
469
- /**
470
- * @param {any} fallback
471
- * @param {(value: any, keypath: string) => any} fn
472
- * @returns {Validator}
473
- */
474
- function validate(fallback, fn) {
475
- return (input, keypath) => {
476
- return input === undefined ? fallback : fn(input, keypath);
477
- };
478
- }
479
-
480
- /**
481
- * @param {string | null} fallback
482
- * @param {boolean} allow_empty
483
- * @returns {Validator}
484
- */
485
- function string(fallback, allow_empty = true) {
486
- return validate(fallback, (input, keypath) => {
487
- assert_string(input, keypath);
488
-
489
- if (!allow_empty && input === '') {
490
- throw new Error(`${keypath} cannot be empty`);
491
- }
492
-
493
- return input;
494
- });
495
- }
496
-
497
- /**
498
- * @param {string[] | undefined} [fallback]
499
- * @returns {Validator}
500
- */
501
- function string_array(fallback) {
502
- return validate(fallback, (input, keypath) => {
503
- if (input === undefined) return input;
504
-
505
- if (!Array.isArray(input) || input.some((value) => typeof value !== 'string')) {
506
- throw new Error(`${keypath} must be an array of strings, if specified`);
507
- }
508
-
509
- return input;
510
- });
511
- }
512
-
513
- /**
514
- * @param {number} fallback
515
- * @returns {Validator}
516
- */
517
- function number(fallback) {
518
- return validate(fallback, (input, keypath) => {
519
- if (typeof input !== 'number') {
520
- throw new Error(`${keypath} should be a number, if specified`);
521
- }
522
- return input;
523
- });
524
- }
525
-
526
- /**
527
- * @param {boolean} fallback
528
- * @returns {Validator}
529
- */
530
- function boolean(fallback) {
531
- return validate(fallback, (input, keypath) => {
532
- if (typeof input !== 'boolean') {
533
- throw new Error(`${keypath} should be true or false, if specified`);
534
- }
535
- return input;
536
- });
537
- }
538
-
539
- /**
540
- * @param {string[]} options
541
- * @returns {Validator}
542
- */
543
- function list(options, fallback = options[0]) {
544
- return validate(fallback, (input, keypath) => {
545
- if (!options.includes(input)) {
546
- // prettier-ignore
547
- const msg = options.length > 2
548
- ? `${keypath} should be one of ${options.slice(0, -1).map(input => `"${input}"`).join(', ')} or "${options[options.length - 1]}"`
549
- : `${keypath} should be either "${options[0]}" or "${options[1]}"`;
550
-
551
- throw new Error(msg);
552
- }
553
- return input;
554
- });
555
- }
556
-
557
- /**
558
- * @param {(filename: string) => boolean} fallback
559
- * @returns {Validator}
560
- */
561
- function fun(fallback) {
562
- return validate(fallback, (input, keypath) => {
563
- if (typeof input !== 'function') {
564
- throw new Error(`${keypath} should be a function, if specified`);
565
- }
566
- return input;
567
- });
568
- }
569
-
570
- /**
571
- * @param {string} input
572
- * @param {string} keypath
573
- */
574
- function assert_string(input, keypath) {
575
- if (typeof input !== 'string') {
576
- throw new Error(`${keypath} should be a string, if specified`);
577
- }
578
- }
579
-
580
- /** @param {(keypath?: string) => string} fn */
581
- function error(fn) {
582
- return validate(undefined, (input, keypath) => {
583
- if (input !== undefined) {
584
- throw new Error(fn(keypath));
585
- }
586
- });
587
- }
588
-
589
- /**
590
- * Loads the template (src/app.html by default) and validates that it has the
591
- * required content.
592
- * @param {string} cwd
593
- * @param {import('types').ValidatedConfig} config
594
- */
595
- function load_template(cwd, config) {
596
- const { template } = config.kit.files;
597
- const relative = path__default.relative(cwd, template);
598
-
599
- if (fs__default.existsSync(template)) {
600
- const contents = fs__default.readFileSync(template, 'utf8');
601
-
602
- // TODO remove this for 1.0
603
- const match = /%svelte\.([a-z]+)%/.exec(contents);
604
- if (match) {
605
- throw new Error(
606
- `%svelte.${match[1]}% in ${relative} should be replaced with %sveltekit.${match[1]}%`
607
- );
608
- }
609
-
610
- const expected_tags = ['%sveltekit.head%', '%sveltekit.body%'];
611
- expected_tags.forEach((tag) => {
612
- if (contents.indexOf(tag) === -1) {
613
- throw new Error(`${relative} is missing ${tag}`);
614
- }
615
- });
616
- } else {
617
- throw new Error(`${relative} does not exist`);
618
- }
619
-
620
- return fs__default.readFileSync(template, 'utf-8');
621
- }
622
-
623
- /**
624
- * Loads and validates svelte.config.js
625
- * @param {{ cwd?: string }} options
626
- * @returns {Promise<import('types').ValidatedConfig>}
627
- */
628
- async function load_config({ cwd = process.cwd() } = {}) {
629
- const config_file = path__default.join(cwd, 'svelte.config.js');
630
-
631
- if (!fs__default.existsSync(config_file)) {
632
- return process_config({}, { cwd });
633
- }
634
-
635
- const config = await import(`${url.pathToFileURL(config_file).href}?ts=${Date.now()}`);
636
-
637
- return process_config(config.default, { cwd });
638
- }
639
-
640
- /**
641
- * @param {import('types').Config} config
642
- * @returns {import('types').ValidatedConfig}
643
- */
644
- function process_config(config, { cwd = process.cwd() } = {}) {
645
- const validated = validate_config(config);
646
-
647
- validated.kit.outDir = path__default.resolve(cwd, validated.kit.outDir);
648
-
649
- for (const key in validated.kit.files) {
650
- // @ts-expect-error this is typescript at its stupidest
651
- validated.kit.files[key] = path__default.resolve(cwd, validated.kit.files[key]);
652
- }
653
-
654
- return validated;
655
- }
656
-
657
- /**
658
- * @param {import('types').Config} config
659
- * @returns {import('types').ValidatedConfig}
660
- */
661
- function validate_config(config) {
662
- if (typeof config !== 'object') {
663
- throw new Error(
664
- 'svelte.config.js must have a configuration object as its default export. See https://kit.svelte.dev/docs/configuration'
665
- );
666
- }
667
-
668
- return options(config, 'config');
669
- }
670
-
671
1
  /**
672
2
  * @param {unknown} err
673
3
  * @return {Error}
@@ -679,4 +9,4 @@ function coalesce_to_error(err) {
679
9
  : new Error(JSON.stringify(err));
680
10
  }
681
11
 
682
- export { $, load_template as a, coalesce_to_error as c, load_config as l };
12
+ export { coalesce_to_error as c };