@sveltejs/kit 1.0.0-next.35 → 1.0.0-next.352

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 (69) hide show
  1. package/README.md +12 -9
  2. package/assets/app/env.js +16 -0
  3. package/assets/app/navigation.js +24 -0
  4. package/assets/app/paths.js +1 -0
  5. package/assets/app/stores.js +97 -0
  6. package/assets/client/singletons.js +13 -0
  7. package/assets/client/start.js +1787 -0
  8. package/assets/components/error.svelte +18 -2
  9. package/assets/env.js +8 -0
  10. package/assets/paths.js +13 -0
  11. package/assets/server/index.js +3380 -0
  12. package/dist/chunks/constants.js +663 -0
  13. package/dist/chunks/filesystem.js +110 -0
  14. package/dist/chunks/index.js +1363 -0
  15. package/dist/chunks/index2.js +120 -0
  16. package/dist/chunks/index3.js +183 -0
  17. package/dist/chunks/index4.js +215 -0
  18. package/dist/chunks/index5.js +15748 -0
  19. package/dist/chunks/misc.js +78 -0
  20. package/dist/chunks/multipart-parser.js +444 -0
  21. package/dist/chunks/object.js +83 -0
  22. package/dist/chunks/plugin.js +558 -0
  23. package/dist/chunks/sync.js +856 -0
  24. package/dist/chunks/write_tsconfig.js +169 -0
  25. package/dist/cli.js +1028 -87
  26. package/dist/hooks.js +28 -0
  27. package/dist/node/polyfills.js +6654 -0
  28. package/dist/node.js +301 -0
  29. package/package.json +95 -55
  30. package/types/ambient.d.ts +307 -0
  31. package/types/index.d.ts +294 -0
  32. package/types/internal.d.ts +326 -0
  33. package/types/private.d.ts +235 -0
  34. package/CHANGELOG.md +0 -371
  35. package/assets/runtime/app/navigation.js +0 -23
  36. package/assets/runtime/app/navigation.js.map +0 -1
  37. package/assets/runtime/app/paths.js +0 -2
  38. package/assets/runtime/app/paths.js.map +0 -1
  39. package/assets/runtime/app/stores.js +0 -78
  40. package/assets/runtime/app/stores.js.map +0 -1
  41. package/assets/runtime/internal/singletons.js +0 -15
  42. package/assets/runtime/internal/singletons.js.map +0 -1
  43. package/assets/runtime/internal/start.js +0 -614
  44. package/assets/runtime/internal/start.js.map +0 -1
  45. package/assets/runtime/utils-85ebcc60.js +0 -18
  46. package/assets/runtime/utils-85ebcc60.js.map +0 -1
  47. package/dist/api.js +0 -28
  48. package/dist/api.js.map +0 -1
  49. package/dist/cli.js.map +0 -1
  50. package/dist/create_app.js +0 -502
  51. package/dist/create_app.js.map +0 -1
  52. package/dist/index.js +0 -327
  53. package/dist/index.js.map +0 -1
  54. package/dist/index2.js +0 -3497
  55. package/dist/index2.js.map +0 -1
  56. package/dist/index3.js +0 -296
  57. package/dist/index3.js.map +0 -1
  58. package/dist/index4.js +0 -311
  59. package/dist/index4.js.map +0 -1
  60. package/dist/index5.js +0 -221
  61. package/dist/index5.js.map +0 -1
  62. package/dist/index6.js +0 -730
  63. package/dist/index6.js.map +0 -1
  64. package/dist/renderer.js +0 -2429
  65. package/dist/renderer.js.map +0 -1
  66. package/dist/standard.js +0 -100
  67. package/dist/standard.js.map +0 -1
  68. package/dist/utils.js +0 -61
  69. package/dist/utils.js.map +0 -1
package/dist/cli.js CHANGED
@@ -1,93 +1,919 @@
1
- import { existsSync } from 'fs';
1
+ import chokidar from 'chokidar';
2
+ import fs__default from 'fs';
3
+ import path__default, { join, relative } from 'path';
2
4
  import sade from 'sade';
3
- import { $, l as load_config } from './index.js';
4
- import 'url';
5
- import 'path';
6
-
7
- var version = "1.0.0-next.35";
8
-
9
- async function get_config() {
10
- // TODO this is temporary, for the benefit of early adopters
11
- if (existsSync('snowpack.config.js') || existsSync('snowpack.config.cjs')) {
12
- // prettier-ignore
13
- console.error($.bold().red(
14
- 'SvelteKit now uses https://vitejs.dev. Please replace snowpack.config.js with vite.config.js:'
15
- ));
16
-
17
- // prettier-ignore
18
- console.error(`
19
- import { resolve } from 'path';
20
-
21
- export default {
22
- resolve: {
23
- alias: {
24
- $components: resolve('src/components')
5
+ import * as vite from 'vite';
6
+ import * as url from 'url';
7
+ import { networkInterfaces, release } from 'os';
8
+
9
+ let FORCE_COLOR, NODE_DISABLE_COLORS, NO_COLOR, TERM, isTTY=true;
10
+ if (typeof process !== 'undefined') {
11
+ ({ FORCE_COLOR, NODE_DISABLE_COLORS, NO_COLOR, TERM } = process.env);
12
+ isTTY = process.stdout && process.stdout.isTTY;
13
+ }
14
+
15
+ const $ = {
16
+ enabled: !NODE_DISABLE_COLORS && NO_COLOR == null && TERM !== 'dumb' && (
17
+ FORCE_COLOR != null && FORCE_COLOR !== '0' || isTTY
18
+ ),
19
+
20
+ // modifiers
21
+ reset: init(0, 0),
22
+ bold: init(1, 22),
23
+ dim: init(2, 22),
24
+ italic: init(3, 23),
25
+ underline: init(4, 24),
26
+ inverse: init(7, 27),
27
+ hidden: init(8, 28),
28
+ strikethrough: init(9, 29),
29
+
30
+ // colors
31
+ black: init(30, 39),
32
+ red: init(31, 39),
33
+ green: init(32, 39),
34
+ yellow: init(33, 39),
35
+ blue: init(34, 39),
36
+ magenta: init(35, 39),
37
+ cyan: init(36, 39),
38
+ white: init(37, 39),
39
+ gray: init(90, 39),
40
+ grey: init(90, 39),
41
+
42
+ // background colors
43
+ bgBlack: init(40, 49),
44
+ bgRed: init(41, 49),
45
+ bgGreen: init(42, 49),
46
+ bgYellow: init(43, 49),
47
+ bgBlue: init(44, 49),
48
+ bgMagenta: init(45, 49),
49
+ bgCyan: init(46, 49),
50
+ bgWhite: init(47, 49)
51
+ };
52
+
53
+ function run(arr, str) {
54
+ let i=0, tmp, beg='', end='';
55
+ for (; i < arr.length; i++) {
56
+ tmp = arr[i];
57
+ beg += tmp.open;
58
+ end += tmp.close;
59
+ if (!!~str.indexOf(tmp.close)) {
60
+ str = str.replace(tmp.rgx, tmp.close + tmp.open);
61
+ }
62
+ }
63
+ return beg + str + end;
64
+ }
65
+
66
+ function chain(has, keys) {
67
+ let ctx = { has, keys };
68
+
69
+ ctx.reset = $.reset.bind(ctx);
70
+ ctx.bold = $.bold.bind(ctx);
71
+ ctx.dim = $.dim.bind(ctx);
72
+ ctx.italic = $.italic.bind(ctx);
73
+ ctx.underline = $.underline.bind(ctx);
74
+ ctx.inverse = $.inverse.bind(ctx);
75
+ ctx.hidden = $.hidden.bind(ctx);
76
+ ctx.strikethrough = $.strikethrough.bind(ctx);
77
+
78
+ ctx.black = $.black.bind(ctx);
79
+ ctx.red = $.red.bind(ctx);
80
+ ctx.green = $.green.bind(ctx);
81
+ ctx.yellow = $.yellow.bind(ctx);
82
+ ctx.blue = $.blue.bind(ctx);
83
+ ctx.magenta = $.magenta.bind(ctx);
84
+ ctx.cyan = $.cyan.bind(ctx);
85
+ ctx.white = $.white.bind(ctx);
86
+ ctx.gray = $.gray.bind(ctx);
87
+ ctx.grey = $.grey.bind(ctx);
88
+
89
+ ctx.bgBlack = $.bgBlack.bind(ctx);
90
+ ctx.bgRed = $.bgRed.bind(ctx);
91
+ ctx.bgGreen = $.bgGreen.bind(ctx);
92
+ ctx.bgYellow = $.bgYellow.bind(ctx);
93
+ ctx.bgBlue = $.bgBlue.bind(ctx);
94
+ ctx.bgMagenta = $.bgMagenta.bind(ctx);
95
+ ctx.bgCyan = $.bgCyan.bind(ctx);
96
+ ctx.bgWhite = $.bgWhite.bind(ctx);
97
+
98
+ return ctx;
99
+ }
100
+
101
+ function init(open, close) {
102
+ let blk = {
103
+ open: `\x1b[${open}m`,
104
+ close: `\x1b[${close}m`,
105
+ rgx: new RegExp(`\\x1b\\[${close}m`, 'g')
106
+ };
107
+ return function (txt) {
108
+ if (this !== void 0 && this.has !== void 0) {
109
+ !!~this.has.indexOf(open) || (this.has.push(open),this.keys.push(blk));
110
+ return txt === void 0 ? this : $.enabled ? run(this.keys, txt+'') : txt+'';
111
+ }
112
+ return txt === void 0 ? chain([open], [blk]) : $.enabled ? run([blk], txt+'') : txt+'';
113
+ };
114
+ }
115
+
116
+ const get_runtime_path = /** @param {import('types').ValidatedKitConfig} config */ (config) =>
117
+ posixify_path(path__default.join(config.outDir, 'runtime'))
118
+ ;
119
+
120
+ /** @param {string} str */
121
+ function posixify_path(str) {
122
+ const parsed = path__default.parse(str);
123
+ return `/${parsed.dir.slice(parsed.root.length).split(path__default.sep).join('/')}/${parsed.base}`;
124
+ }
125
+
126
+ function noop() {}
127
+
128
+ /** @param {{ verbose: boolean }} opts */
129
+ function logger({ verbose }) {
130
+ /** @type {import('types').Logger} */
131
+ const log = (msg) => console.log(msg.replace(/^/gm, ' '));
132
+
133
+ /** @param {string} msg */
134
+ const err = (msg) => console.error(msg.replace(/^/gm, ' '));
135
+
136
+ log.success = (msg) => log($.green(`✔ ${msg}`));
137
+ log.error = (msg) => err($.bold().red(msg));
138
+ log.warn = (msg) => log($.bold().yellow(msg));
139
+
140
+ log.minor = verbose ? (msg) => log($.grey(msg)) : noop;
141
+ log.info = verbose ? log : noop;
142
+
143
+ return log;
144
+ }
145
+
146
+ /**
147
+ * Given an entry point like [cwd]/src/hooks, returns a filename like [cwd]/src/hooks.js or [cwd]/src/hooks/index.js
148
+ * @param {string} entry
149
+ * @returns {string|null}
150
+ */
151
+ function resolve_entry(entry) {
152
+ if (fs__default.existsSync(entry)) {
153
+ const stats = fs__default.statSync(entry);
154
+ if (stats.isDirectory()) {
155
+ return resolve_entry(path__default.join(entry, 'index'));
156
+ }
157
+
158
+ return entry;
159
+ } else {
160
+ const dir = path__default.dirname(entry);
161
+
162
+ if (fs__default.existsSync(dir)) {
163
+ const base = path__default.basename(entry);
164
+ const files = fs__default.readdirSync(dir);
165
+
166
+ const found = files.find((file) => file.replace(/\.[^.]+$/, '') === base);
167
+
168
+ if (found) return path__default.join(dir, found);
169
+ }
170
+ }
171
+
172
+ return null;
173
+ }
174
+
175
+ /** @param {import('types').ManifestData} manifest_data */
176
+ function get_mime_lookup(manifest_data) {
177
+ /** @type {Record<string, string>} */
178
+ const mime = {};
179
+
180
+ manifest_data.assets.forEach((asset) => {
181
+ if (asset.type) {
182
+ const ext = path__default.extname(asset.file);
183
+ mime[ext] = asset.type;
184
+ }
185
+ });
186
+
187
+ return mime;
188
+ }
189
+
190
+ /** @param {import('types').ValidatedKitConfig} config */
191
+ function get_aliases(config) {
192
+ /** @type {Record<string, string>} */
193
+ const alias = {
194
+ __GENERATED__: path__default.posix.join(config.outDir, 'generated'),
195
+ $app: `${get_runtime_path(config)}/app`,
196
+
197
+ // For now, we handle `$lib` specially here rather than make it a default value for
198
+ // `config.kit.alias` since it has special meaning for packaging, etc.
199
+ $lib: config.files.lib
200
+ };
201
+
202
+ for (const [key, value] of Object.entries(config.alias)) {
203
+ alias[key] = path__default.resolve(value);
204
+ }
205
+
206
+ return alias;
207
+ }
208
+
209
+ /** @typedef {import('./types').Validator} Validator */
210
+
211
+ /** @type {Validator} */
212
+ const options = object(
213
+ {
214
+ extensions: validate(['.svelte'], (input, keypath) => {
215
+ if (!Array.isArray(input) || !input.every((page) => typeof page === 'string')) {
216
+ throw new Error(`${keypath} must be an array of strings`);
217
+ }
218
+
219
+ input.forEach((extension) => {
220
+ if (extension[0] !== '.') {
221
+ throw new Error(`Each member of ${keypath} must start with '.' — saw '${extension}'`);
222
+ }
223
+
224
+ if (!/^(\.[a-z0-9]+)+$/i.test(extension)) {
225
+ throw new Error(`File extensions must be alphanumeric — saw '${extension}'`);
226
+ }
227
+ });
228
+
229
+ return input;
230
+ }),
231
+
232
+ kit: object({
233
+ adapter: validate(null, (input, keypath) => {
234
+ if (typeof input !== 'object' || !input.adapt) {
235
+ let message = `${keypath} should be an object with an "adapt" method`;
236
+
237
+ if (Array.isArray(input) || typeof input === 'string') {
238
+ // for the early adapter adopters
239
+ message += ', rather than the name of an adapter';
25
240
  }
241
+
242
+ throw new Error(`${message}. See https://kit.svelte.dev/docs/adapters`);
26
243
  }
27
- };
28
- `.replace(/^\t{3}/gm, '').replace(/\t/gm, ' ').trim());
29
- }
30
244
 
31
- try {
32
- return await load_config();
33
- } catch (error) {
34
- let message = error.message;
245
+ return input;
246
+ }),
35
247
 
36
- if (error.code === 'MODULE_NOT_FOUND') {
37
- if (existsSync('svelte.config.js')) {
38
- // TODO this is temporary, for the benefit of early adopters
39
- message =
40
- 'You must rename svelte.config.js to svelte.config.cjs, and snowpack.config.js to snowpack.config.cjs';
41
- } else {
42
- message = 'Missing svelte.config.cjs';
248
+ alias: validate({}, (input, keypath) => {
249
+ if (typeof input !== 'object') {
250
+ throw new Error(`${keypath} should be an object`);
251
+ }
252
+
253
+ for (const key in input) {
254
+ assert_string(input[key], `${keypath}.${key}`);
255
+ }
256
+
257
+ return input;
258
+ }),
259
+
260
+ // TODO: remove this for the 1.0 release
261
+ amp: error(
262
+ (keypath) =>
263
+ `${keypath} has been removed. See https://kit.svelte.dev/docs/seo#amp for details on how to support AMP`
264
+ ),
265
+
266
+ appDir: validate('_app', (input, keypath) => {
267
+ assert_string(input, keypath);
268
+
269
+ if (input) {
270
+ if (input.startsWith('/') || input.endsWith('/')) {
271
+ throw new Error(
272
+ "config.kit.appDir cannot start or end with '/'. See https://kit.svelte.dev/docs/configuration"
273
+ );
274
+ }
275
+ } else {
276
+ throw new Error(`${keypath} cannot be empty`);
277
+ }
278
+
279
+ return input;
280
+ }),
281
+
282
+ browser: object({
283
+ hydrate: boolean(true),
284
+ router: boolean(true)
285
+ }),
286
+
287
+ csp: object({
288
+ mode: list(['auto', 'hash', 'nonce']),
289
+ directives: object({
290
+ 'child-src': string_array(),
291
+ 'default-src': string_array(),
292
+ 'frame-src': string_array(),
293
+ 'worker-src': string_array(),
294
+ 'connect-src': string_array(),
295
+ 'font-src': string_array(),
296
+ 'img-src': string_array(),
297
+ 'manifest-src': string_array(),
298
+ 'media-src': string_array(),
299
+ 'object-src': string_array(),
300
+ 'prefetch-src': string_array(),
301
+ 'script-src': string_array(),
302
+ 'script-src-elem': string_array(),
303
+ 'script-src-attr': string_array(),
304
+ 'style-src': string_array(),
305
+ 'style-src-elem': string_array(),
306
+ 'style-src-attr': string_array(),
307
+ 'base-uri': string_array(),
308
+ sandbox: string_array(),
309
+ 'form-action': string_array(),
310
+ 'frame-ancestors': string_array(),
311
+ 'navigate-to': string_array(),
312
+ 'report-uri': string_array(),
313
+ 'report-to': string_array(),
314
+ 'require-trusted-types-for': string_array(),
315
+ 'trusted-types': string_array(),
316
+ 'upgrade-insecure-requests': boolean(false),
317
+ 'require-sri-for': string_array(),
318
+ 'block-all-mixed-content': boolean(false),
319
+ 'plugin-types': string_array(),
320
+ referrer: string_array()
321
+ })
322
+ }),
323
+
324
+ endpointExtensions: string_array(['.js', '.ts']),
325
+
326
+ files: object({
327
+ assets: string('static'),
328
+ hooks: string(join('src', 'hooks')),
329
+ lib: string(join('src', 'lib')),
330
+ params: string(join('src', 'params')),
331
+ routes: string(join('src', 'routes')),
332
+ serviceWorker: string(join('src', 'service-worker')),
333
+ template: string(join('src', 'app.html'))
334
+ }),
335
+
336
+ floc: boolean(false),
337
+
338
+ // TODO: remove this for the 1.0 release
339
+ headers: error(
340
+ (keypath) =>
341
+ `${keypath} has been removed. See https://github.com/sveltejs/kit/pull/3384 for details`
342
+ ),
343
+
344
+ // TODO: remove this for the 1.0 release
345
+ host: error(
346
+ (keypath) =>
347
+ `${keypath} has been removed. See https://github.com/sveltejs/kit/pull/3384 for details`
348
+ ),
349
+
350
+ // TODO remove for 1.0
351
+ hydrate: error((keypath) => `${keypath} has been moved to config.kit.browser.hydrate`),
352
+
353
+ inlineStyleThreshold: number(0),
354
+
355
+ methodOverride: object({
356
+ parameter: string('_method'),
357
+ allowed: validate([], (input, keypath) => {
358
+ if (!Array.isArray(input) || !input.every((method) => typeof method === 'string')) {
359
+ throw new Error(`${keypath} must be an array of strings`);
360
+ }
361
+
362
+ if (input.map((i) => i.toUpperCase()).includes('GET')) {
363
+ throw new Error(`${keypath} cannot contain "GET"`);
364
+ }
365
+
366
+ return input;
367
+ })
368
+ }),
369
+
370
+ outDir: string('.svelte-kit'),
371
+
372
+ package: object({
373
+ dir: string('package'),
374
+ // excludes all .d.ts and filename starting with _
375
+ exports: fun((filepath) => !/^_|\/_|\.d\.ts$/.test(filepath)),
376
+ files: fun(() => true),
377
+ emitTypes: boolean(true)
378
+ }),
379
+
380
+ paths: object({
381
+ base: validate('', (input, keypath) => {
382
+ assert_string(input, keypath);
383
+
384
+ if (input !== '' && (input.endsWith('/') || !input.startsWith('/'))) {
385
+ throw new Error(
386
+ `${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`
387
+ );
388
+ }
389
+
390
+ return input;
391
+ }),
392
+ assets: validate('', (input, keypath) => {
393
+ assert_string(input, keypath);
394
+
395
+ if (input) {
396
+ if (!/^[a-z]+:\/\//.test(input)) {
397
+ throw new Error(
398
+ `${keypath} option must be an absolute path, if specified. See https://kit.svelte.dev/docs/configuration#paths`
399
+ );
400
+ }
401
+
402
+ if (input.endsWith('/')) {
403
+ throw new Error(
404
+ `${keypath} option must not end with '/'. See https://kit.svelte.dev/docs/configuration#paths`
405
+ );
406
+ }
407
+ }
408
+
409
+ return input;
410
+ })
411
+ }),
412
+
413
+ prerender: object({
414
+ concurrency: number(1),
415
+ crawl: boolean(true),
416
+ createIndexFiles: error(
417
+ (keypath) =>
418
+ `${keypath} has been removed — it is now controlled by the trailingSlash option. See https://kit.svelte.dev/docs/configuration#trailingslash`
419
+ ),
420
+ default: boolean(false),
421
+ enabled: boolean(true),
422
+ entries: validate(['*'], (input, keypath) => {
423
+ if (!Array.isArray(input) || !input.every((page) => typeof page === 'string')) {
424
+ throw new Error(`${keypath} must be an array of strings`);
425
+ }
426
+
427
+ input.forEach((page) => {
428
+ if (page !== '*' && page[0] !== '/') {
429
+ throw new Error(
430
+ `Each member of ${keypath} must be either '*' or an absolute path beginning with '/' — saw '${page}'`
431
+ );
432
+ }
433
+ });
434
+
435
+ return input;
436
+ }),
437
+
438
+ // TODO: remove this for the 1.0 release
439
+ force: validate(undefined, (input, keypath) => {
440
+ if (typeof input !== 'undefined') {
441
+ const newSetting = input ? 'continue' : 'fail';
442
+ const needsSetting = newSetting === 'continue';
443
+ throw new Error(
444
+ `${keypath} has been removed in favor of \`onError\`. In your case, set \`onError\` to "${newSetting}"${
445
+ needsSetting ? '' : ' (or leave it undefined)'
446
+ } to get the same behavior as you would with \`force: ${JSON.stringify(input)}\``
447
+ );
448
+ }
449
+ }),
450
+
451
+ onError: validate('fail', (input, keypath) => {
452
+ if (typeof input === 'function') return input;
453
+ if (['continue', 'fail'].includes(input)) return input;
454
+ throw new Error(
455
+ `${keypath} should be either a custom function or one of "continue" or "fail"`
456
+ );
457
+ }),
458
+
459
+ // TODO: remove this for the 1.0 release
460
+ pages: error((keypath) => `${keypath} has been renamed to \`entries\`.`)
461
+ }),
462
+
463
+ // TODO: remove this for the 1.0 release
464
+ protocol: error(
465
+ (keypath) =>
466
+ `${keypath} has been removed. See https://github.com/sveltejs/kit/pull/3384 for details`
467
+ ),
468
+
469
+ // TODO remove for 1.0
470
+ router: error((keypath) => `${keypath} has been moved to config.kit.browser.router`),
471
+
472
+ routes: fun((filepath) => !/(?:(?:^_|\/_)|(?:^\.|\/\.)(?!well-known))/.test(filepath)),
473
+
474
+ serviceWorker: object({
475
+ register: boolean(true),
476
+ files: fun((filename) => !/\.DS_Store/.test(filename))
477
+ }),
478
+
479
+ // TODO remove this for 1.0
480
+ ssr: error(
481
+ (keypath) =>
482
+ `${keypath} has been removed — use the handle hook instead: https://kit.svelte.dev/docs/hooks#handle'`
483
+ ),
484
+
485
+ // TODO remove this for 1.0
486
+ target: error((keypath) => `${keypath} is no longer required, and should be removed`),
487
+
488
+ trailingSlash: list(['never', 'always', 'ignore']),
489
+
490
+ version: object({
491
+ name: string(Date.now().toString()),
492
+ pollInterval: number(0)
493
+ }),
494
+
495
+ vite: validate(
496
+ () => ({}),
497
+ (input, keypath) => {
498
+ if (typeof input === 'object') {
499
+ const config = input;
500
+ input = () => config;
501
+ }
502
+
503
+ if (typeof input !== 'function') {
504
+ throw new Error(
505
+ `${keypath} must be a Vite config object (https://vitejs.dev/config) or a function that returns one`
506
+ );
507
+ }
508
+
509
+ return input;
510
+ }
511
+ )
512
+ })
513
+ },
514
+ true
515
+ );
516
+
517
+ /**
518
+ * @param {Record<string, Validator>} children
519
+ * @param {boolean} [allow_unknown]
520
+ * @returns {Validator}
521
+ */
522
+ function object(children, allow_unknown) {
523
+ return (input, keypath) => {
524
+ /** @type {Record<string, any>} */
525
+ const output = {};
526
+
527
+ if ((input && typeof input !== 'object') || Array.isArray(input)) {
528
+ throw new Error(`${keypath} should be an object`);
529
+ }
530
+
531
+ for (const key in input) {
532
+ if (!(key in children)) {
533
+ if (allow_unknown) {
534
+ output[key] = input[key];
535
+ } else {
536
+ let message = `Unexpected option ${keypath}.${key}`;
537
+
538
+ // special case
539
+ if (keypath === 'config.kit' && key in options) {
540
+ message += ` (did you mean config.${key}?)`;
541
+ }
542
+
543
+ throw new Error(message);
544
+ }
43
545
  }
44
- } else if (error.name === 'SyntaxError') {
45
- message = 'Malformed svelte.config.cjs';
46
546
  }
47
547
 
48
- console.error($.bold().red(message));
49
- console.error($.grey(error.stack));
50
- process.exit(1);
548
+ for (const key in children) {
549
+ const validator = children[key];
550
+ output[key] = validator(input && input[key], `${keypath}.${key}`);
551
+ }
552
+
553
+ return output;
554
+ };
555
+ }
556
+
557
+ /**
558
+ * @param {any} fallback
559
+ * @param {(value: any, keypath: string) => any} fn
560
+ * @returns {Validator}
561
+ */
562
+ function validate(fallback, fn) {
563
+ return (input, keypath) => {
564
+ return input === undefined ? fallback : fn(input, keypath);
565
+ };
566
+ }
567
+
568
+ /**
569
+ * @param {string | null} fallback
570
+ * @param {boolean} allow_empty
571
+ * @returns {Validator}
572
+ */
573
+ function string(fallback, allow_empty = true) {
574
+ return validate(fallback, (input, keypath) => {
575
+ assert_string(input, keypath);
576
+
577
+ if (!allow_empty && input === '') {
578
+ throw new Error(`${keypath} cannot be empty`);
579
+ }
580
+
581
+ return input;
582
+ });
583
+ }
584
+
585
+ /**
586
+ * @param {string[] | undefined} [fallback]
587
+ * @returns {Validator}
588
+ */
589
+ function string_array(fallback) {
590
+ return validate(fallback, (input, keypath) => {
591
+ if (input === undefined) return input;
592
+
593
+ if (!Array.isArray(input) || input.some((value) => typeof value !== 'string')) {
594
+ throw new Error(`${keypath} must be an array of strings, if specified`);
595
+ }
596
+
597
+ return input;
598
+ });
599
+ }
600
+
601
+ /**
602
+ * @param {number} fallback
603
+ * @returns {Validator}
604
+ */
605
+ function number(fallback) {
606
+ return validate(fallback, (input, keypath) => {
607
+ if (typeof input !== 'number') {
608
+ throw new Error(`${keypath} should be a number, if specified`);
609
+ }
610
+ return input;
611
+ });
612
+ }
613
+
614
+ /**
615
+ * @param {boolean} fallback
616
+ * @returns {Validator}
617
+ */
618
+ function boolean(fallback) {
619
+ return validate(fallback, (input, keypath) => {
620
+ if (typeof input !== 'boolean') {
621
+ throw new Error(`${keypath} should be true or false, if specified`);
622
+ }
623
+ return input;
624
+ });
625
+ }
626
+
627
+ /**
628
+ * @param {string[]} options
629
+ * @returns {Validator}
630
+ */
631
+ function list(options, fallback = options[0]) {
632
+ return validate(fallback, (input, keypath) => {
633
+ if (!options.includes(input)) {
634
+ // prettier-ignore
635
+ const msg = options.length > 2
636
+ ? `${keypath} should be one of ${options.slice(0, -1).map(input => `"${input}"`).join(', ')} or "${options[options.length - 1]}"`
637
+ : `${keypath} should be either "${options[0]}" or "${options[1]}"`;
638
+
639
+ throw new Error(msg);
640
+ }
641
+ return input;
642
+ });
643
+ }
644
+
645
+ /**
646
+ * @param {(filename: string) => boolean} fallback
647
+ * @returns {Validator}
648
+ */
649
+ function fun(fallback) {
650
+ return validate(fallback, (input, keypath) => {
651
+ if (typeof input !== 'function') {
652
+ throw new Error(`${keypath} should be a function, if specified`);
653
+ }
654
+ return input;
655
+ });
656
+ }
657
+
658
+ /**
659
+ * @param {string} input
660
+ * @param {string} keypath
661
+ */
662
+ function assert_string(input, keypath) {
663
+ if (typeof input !== 'string') {
664
+ throw new Error(`${keypath} should be a string, if specified`);
665
+ }
666
+ }
667
+
668
+ /** @param {(keypath?: string) => string} fn */
669
+ function error(fn) {
670
+ return validate(undefined, (input, keypath) => {
671
+ if (input !== undefined) {
672
+ throw new Error(fn(keypath));
673
+ }
674
+ });
675
+ }
676
+
677
+ /**
678
+ * Loads the template (src/app.html by default) and validates that it has the
679
+ * required content.
680
+ * @param {string} cwd
681
+ * @param {import('types').ValidatedConfig} config
682
+ */
683
+ function load_template(cwd, config) {
684
+ const { template } = config.kit.files;
685
+ const relative = path__default.relative(cwd, template);
686
+
687
+ if (fs__default.existsSync(template)) {
688
+ const contents = fs__default.readFileSync(template, 'utf8');
689
+
690
+ // TODO remove this for 1.0
691
+ const match = /%svelte\.([a-z]+)%/.exec(contents);
692
+ if (match) {
693
+ throw new Error(
694
+ `%svelte.${match[1]}% in ${relative} should be replaced with %sveltekit.${match[1]}%`
695
+ );
696
+ }
697
+
698
+ const expected_tags = ['%sveltekit.head%', '%sveltekit.body%'];
699
+ expected_tags.forEach((tag) => {
700
+ if (contents.indexOf(tag) === -1) {
701
+ throw new Error(`${relative} is missing ${tag}`);
702
+ }
703
+ });
704
+ } else {
705
+ throw new Error(`${relative} does not exist`);
706
+ }
707
+
708
+ return fs__default.readFileSync(template, 'utf-8');
709
+ }
710
+
711
+ /**
712
+ * Loads and validates svelte.config.js
713
+ * @param {{ cwd?: string }} options
714
+ * @returns {Promise<import('types').ValidatedConfig>}
715
+ */
716
+ async function load_config({ cwd = process.cwd() } = {}) {
717
+ const config_file = path__default.join(cwd, 'svelte.config.js');
718
+
719
+ if (!fs__default.existsSync(config_file)) {
720
+ throw new Error(
721
+ 'You need to create a svelte.config.js file. See https://kit.svelte.dev/docs/configuration'
722
+ );
723
+ }
724
+
725
+ const config = await import(`${url.pathToFileURL(config_file).href}?ts=${Date.now()}`);
726
+
727
+ const validated = validate_config(config.default);
728
+
729
+ validated.kit.outDir = path__default.resolve(cwd, validated.kit.outDir);
730
+
731
+ for (const key in validated.kit.files) {
732
+ // @ts-expect-error this is typescript at its stupidest
733
+ validated.kit.files[key] = path__default.resolve(cwd, validated.kit.files[key]);
51
734
  }
735
+
736
+ return validated;
52
737
  }
53
738
 
54
- function handle_error(error) {
55
- console.log($.bold().red(`> ${error.message}`));
56
- console.log($.gray(error.stack));
739
+ /**
740
+ * @param {import('types').Config} config
741
+ * @returns {import('types').ValidatedConfig}
742
+ */
743
+ function validate_config(config) {
744
+ if (typeof config !== 'object') {
745
+ throw new Error(
746
+ 'svelte.config.js must have a configuration object as its default export. See https://kit.svelte.dev/docs/configuration'
747
+ );
748
+ }
749
+
750
+ return options(config, 'config');
751
+ }
752
+
753
+ /**
754
+ * Ensures the user does not override any config values that SvelteKit must control.
755
+ * @param {string[]} conflicts - array of conflicts in dotted notation
756
+ * @param {string=} pathPrefix - prepended in front of the path
757
+ * @param {string=} scope - used to prefix the whole error message
758
+ */
759
+ function print_config_conflicts(conflicts, pathPrefix, scope) {
760
+ const prefix = scope ? scope + ': ' : '';
761
+ const log = logger({ verbose: false });
762
+ conflicts.forEach((conflict) => {
763
+ log.error(
764
+ `${prefix}The value for ${pathPrefix}${conflict} specified in svelte.config.js has been ignored. This option is controlled by SvelteKit.`
765
+ );
766
+ });
767
+ }
768
+
769
+ /**
770
+ * @param {unknown} err
771
+ * @return {Error}
772
+ */
773
+ function coalesce_to_error(err) {
774
+ return err instanceof Error ||
775
+ (err && /** @type {any} */ (err).name && /** @type {any} */ (err).message)
776
+ ? /** @type {Error} */ (err)
777
+ : new Error(JSON.stringify(err));
778
+ }
779
+
780
+ /** @param {unknown} e */
781
+ function handle_error(e) {
782
+ const error = coalesce_to_error(e);
783
+
784
+ if (error.name === 'SyntaxError') throw error;
785
+
786
+ console.error($.bold().red(`> ${error.message}`));
787
+ if (error.stack) {
788
+ console.error($.gray(error.stack.split('\n').slice(1).join('\n')));
789
+ }
790
+
57
791
  process.exit(1);
58
792
  }
59
793
 
60
- async function launch(port) {
794
+ /**
795
+ * @param {number} port
796
+ * @param {boolean} https
797
+ * @param {string} base
798
+ */
799
+ async function launch(port, https, base) {
61
800
  const { exec } = await import('child_process');
62
- exec(`${process.platform == 'win32' ? 'start' : 'open'} http://localhost:${port}`);
801
+ let cmd = 'open';
802
+ if (process.platform == 'win32') {
803
+ cmd = 'start';
804
+ } else if (process.platform == 'linux') {
805
+ if (/microsoft/i.test(release())) {
806
+ cmd = 'cmd.exe /c start';
807
+ } else {
808
+ cmd = 'xdg-open';
809
+ }
810
+ }
811
+ exec(`${cmd} ${https ? 'https' : 'http'}://localhost:${port}${base}`);
63
812
  }
64
813
 
65
- const prog = sade('svelte-kit').version(version);
814
+ const prog = sade('svelte-kit').version('1.0.0-next.352');
66
815
 
67
816
  prog
68
817
  .command('dev')
69
818
  .describe('Start a development server')
70
- .option('-p, --port', 'Port', 3000)
71
- .option('-o, --open', 'Open a browser tab', false)
72
- .action(async ({ port, open }) => {
73
- process.env.NODE_ENV = 'development';
74
- const config = await get_config();
819
+ .option('-p, --port', 'Port')
820
+ .option('-o, --open', 'Open a browser tab')
821
+ .option('--host', 'Host (only use this on trusted networks)')
822
+ .option('--https', 'Use self-signed HTTPS certificate')
823
+ .option('-H', 'no longer supported, use --https instead') // TODO remove for 1.0
824
+ .action(async ({ port, host, https, open, H }) => {
825
+ let first = true;
826
+ let relaunching = false;
827
+ let uid = 1;
75
828
 
76
- const { dev } = await import('./index2.js');
829
+ /** @type {() => Promise<void>} */
830
+ let close;
77
831
 
78
- try {
79
- const watcher = await dev({ port, config });
832
+ async function start() {
833
+ const { plugins } = await import('./chunks/plugin.js');
834
+ const svelte_config = await load_config();
835
+ const vite_config = await svelte_config.kit.vite();
80
836
 
81
- watcher.on('stdout', (data) => {
82
- process.stdout.write(data);
83
- });
837
+ /** @type {import('vite').UserConfig} */
838
+ const config = {
839
+ ...vite_config,
840
+ plugins: [...(vite_config.plugins || []), plugins(svelte_config)]
841
+ };
842
+ config.server = config.server || {};
843
+
844
+ // optional config from command-line flags
845
+ // these should take precedence, but not print conflict warnings
846
+ if (host) {
847
+ config.server.host = host;
848
+ }
84
849
 
85
- watcher.on('stderr', (data) => {
86
- process.stderr.write(data);
850
+ // if https is already enabled then do nothing. it could be an object and we
851
+ // don't want to overwrite with a boolean
852
+ if (https && !vite_config?.server?.https) {
853
+ config.server.https = https;
854
+ }
855
+
856
+ if (port) {
857
+ config.server.port = port;
858
+ }
859
+
860
+ const server = await vite.createServer(config);
861
+ await server.listen(port);
862
+
863
+ const address_info = /** @type {import('net').AddressInfo} */ (
864
+ /** @type {import('http').Server} */ (server.httpServer).address()
865
+ );
866
+
867
+ const resolved_config = server.config;
868
+
869
+ welcome({
870
+ port: address_info.port,
871
+ host: address_info.address,
872
+ https: !!(https || resolved_config.server.https),
873
+ open: first && (open || !!resolved_config.server.open),
874
+ base: svelte_config.kit.paths.base,
875
+ loose: resolved_config.server.fs.strict === false,
876
+ allow: resolved_config.server.fs.allow
87
877
  });
88
878
 
89
- console.log($.bold().cyan(`> Listening on http://localhost:${watcher.port}`));
90
- if (open) launch(watcher.port);
879
+ first = false;
880
+
881
+ return server.close;
882
+ }
883
+
884
+ // TODO: we should probably replace this with something like vite-plugin-restart
885
+ async function relaunch() {
886
+ const id = uid;
887
+ relaunching = true;
888
+
889
+ try {
890
+ await close();
891
+ close = await start();
892
+
893
+ if (id !== uid) relaunch();
894
+ } catch (e) {
895
+ const error = /** @type {Error} */ (e);
896
+
897
+ console.error($.bold().red(`> ${error.message}`));
898
+ if (error.stack) {
899
+ console.error($.gray(error.stack.split('\n').slice(1).join('\n')));
900
+ }
901
+ }
902
+
903
+ relaunching = false;
904
+ }
905
+
906
+ try {
907
+ if (H) throw new Error('-H is no longer supported — use --https instead');
908
+
909
+ process.env.NODE_ENV = process.env.NODE_ENV || 'development';
910
+
911
+ close = await start();
912
+
913
+ chokidar.watch('svelte.config.js', { ignoreInitial: true }).on('change', () => {
914
+ if (relaunching) uid += 1;
915
+ else relaunch();
916
+ });
91
917
  } catch (error) {
92
918
  handle_error(error);
93
919
  }
@@ -96,56 +922,171 @@ prog
96
922
  prog
97
923
  .command('build')
98
924
  .describe('Create a production build of your app')
99
- .action(async () => {
100
- process.env.NODE_ENV = 'production';
101
- const config = await get_config();
925
+ .option('--verbose', 'Log more stuff', false)
926
+ .action(async ({ verbose }) => {
927
+ try {
928
+ process.env.NODE_ENV = process.env.NODE_ENV || 'production';
929
+ const config = await load_config();
102
930
 
103
- const { build } = await import('./index3.js');
931
+ const log = logger({ verbose });
104
932
 
105
- try {
106
- await build(config);
933
+ const { build } = await import('./chunks/index.js');
934
+ const { build_data, prerendered } = await build(config, { log });
935
+
936
+ console.log(
937
+ `\nRun ${$.bold().cyan('npm run preview')} to preview your production build locally.`
938
+ );
939
+
940
+ if (config.kit.adapter) {
941
+ const { adapt } = await import('./chunks/index3.js');
942
+ await adapt(config, build_data, prerendered, { log });
943
+
944
+ // this is necessary to close any open db connections, etc
945
+ process.exit(0);
946
+ }
947
+
948
+ console.log($.bold().yellow('\nNo adapter specified'));
949
+
950
+ // prettier-ignore
951
+ console.log(
952
+ `See ${$.bold().cyan('https://kit.svelte.dev/docs/adapters')} to learn how to configure your app to run on the platform of your choosing`
953
+ );
107
954
  } catch (error) {
108
955
  handle_error(error);
109
956
  }
110
957
  });
111
958
 
112
959
  prog
113
- .command('start')
960
+ .command('preview')
114
961
  .describe('Serve an already-built app')
115
962
  .option('-p, --port', 'Port', 3000)
116
963
  .option('-o, --open', 'Open a browser tab', false)
117
- .action(async ({ port, open }) => {
118
- process.env.NODE_ENV = 'production';
119
- const config = await get_config();
964
+ .option('--host', 'Host (only use this on trusted networks)', 'localhost')
965
+ .option('--https', 'Use self-signed HTTPS certificate', false)
966
+ .option('-H', 'no longer supported, use --https instead') // TODO remove for 1.0
967
+ .action(async ({ port, host, https, open, H }) => {
968
+ try {
969
+ if (H) throw new Error('-H is no longer supported — use --https instead');
120
970
 
121
- const { start } = await import('./index4.js');
971
+ process.env.NODE_ENV = process.env.NODE_ENV || 'production';
122
972
 
123
- try {
124
- await start({ port, config });
973
+ const { sveltekit_plugin } = await import('./chunks/index4.js');
974
+ const svelte_config = await load_config();
975
+ const vite_config = await svelte_config.kit.vite();
976
+
977
+ /** @type {import('vite').UserConfig} */
978
+ const config = {
979
+ ...vite_config,
980
+ plugins: [...(vite_config.plugins || []), sveltekit_plugin]
981
+ };
982
+ config.preview = config.preview || {};
125
983
 
126
- console.log($.bold().cyan(`> Listening on http://localhost:${port}`));
127
- if (open) if (open) launch(port);
984
+ // optional config from command-line flags
985
+ // these should take precedence, but not print conflict warnings
986
+ if (host) {
987
+ config.preview.host = host;
988
+ }
989
+ if (https) {
990
+ config.preview.https = https;
991
+ }
992
+ if (port) {
993
+ config.preview.port = port;
994
+ }
995
+
996
+ const preview_server = await vite.preview(config);
997
+
998
+ welcome({ port, host, https, open, base: preview_server.config.base });
128
999
  } catch (error) {
129
1000
  handle_error(error);
130
1001
  }
131
1002
  });
132
1003
 
133
1004
  prog
134
- .command('adapt')
135
- .describe('Customise your production build for different platforms')
136
- .option('--verbose', 'Log more stuff', false)
137
- .action(async ({ verbose }) => {
138
- process.env.NODE_ENV = 'production';
139
- const config = await get_config();
1005
+ .command('package')
1006
+ .describe('Create a package')
1007
+ .option('-w, --watch', 'Rerun when files change', false)
1008
+ .action(async ({ watch }) => {
1009
+ try {
1010
+ const config = await load_config();
1011
+ const packaging = await import('./chunks/index5.js');
140
1012
 
141
- const { adapt } = await import('./index6.js');
1013
+ await (watch ? packaging.watch(config) : packaging.build(config));
1014
+ } catch (error) {
1015
+ handle_error(error);
1016
+ }
1017
+ });
1018
+
1019
+ prog
1020
+ .command('sync')
1021
+ .describe('Synchronise generated files')
1022
+ .action(async () => {
1023
+ if (!fs__default.existsSync('svelte.config.js')) {
1024
+ console.warn('Missing svelte.config.js — skipping');
1025
+ return;
1026
+ }
142
1027
 
143
1028
  try {
144
- await adapt(config, { verbose });
1029
+ const config = await load_config();
1030
+ const sync = await import('./chunks/sync.js');
1031
+ sync.all(config);
145
1032
  } catch (error) {
146
1033
  handle_error(error);
147
1034
  }
148
1035
  });
149
1036
 
150
1037
  prog.parse(process.argv, { unknown: (arg) => `Unknown option: ${arg}` });
151
- //# sourceMappingURL=cli.js.map
1038
+
1039
+ /**
1040
+ * @param {{
1041
+ * open: boolean;
1042
+ * host: string;
1043
+ * https: boolean;
1044
+ * port: number;
1045
+ * base: string;
1046
+ * loose?: boolean;
1047
+ * allow?: string[];
1048
+ * cwd?: string;
1049
+ * }} param0
1050
+ */
1051
+ function welcome({ port, host, https, open, base, loose, allow, cwd }) {
1052
+ if (open) launch(port, https, base);
1053
+
1054
+ console.log($.bold().cyan(`\n SvelteKit v${'1.0.0-next.352'}\n`));
1055
+
1056
+ const protocol = https ? 'https:' : 'http:';
1057
+ const exposed = typeof host !== 'undefined' && host !== 'localhost' && host !== '127.0.0.1';
1058
+
1059
+ Object.values(networkInterfaces()).forEach((interfaces) => {
1060
+ if (!interfaces) return;
1061
+ interfaces.forEach((details) => {
1062
+ // @ts-ignore node18 returns a number
1063
+ if (details.family !== 'IPv4' && details.family !== 4) return;
1064
+
1065
+ // prettier-ignore
1066
+ if (details.internal) {
1067
+ console.log(` ${$.gray('local: ')} ${protocol}//${$.bold(`localhost:${port}`)}`);
1068
+ } else {
1069
+ if (details.mac === '00:00:00:00:00:00') return;
1070
+
1071
+ if (exposed) {
1072
+ console.log(` ${$.gray('network:')} ${protocol}//${$.bold(`${details.address}:${port}`)}`);
1073
+ if (loose) {
1074
+ console.log(`\n ${$.yellow('Serving with vite.server.fs.strict: false. Note that all files on your machine will be accessible to anyone on your network.')}`);
1075
+ } else if (allow?.length && cwd) {
1076
+ console.log(`\n ${$.yellow('Note that all files in the following directories will be accessible to anyone on your network: ' + allow.map(a => relative(cwd, a)).join(', '))}`);
1077
+ }
1078
+ } else {
1079
+ console.log(` ${$.gray('network: not exposed')}`);
1080
+ }
1081
+ }
1082
+ });
1083
+ });
1084
+
1085
+ if (!exposed) {
1086
+ console.log('\n Use --host to expose server to other devices on this network');
1087
+ }
1088
+
1089
+ console.log('\n');
1090
+ }
1091
+
1092
+ export { $, get_runtime_path as a, get_mime_lookup as b, coalesce_to_error as c, load_config as d, get_aliases as g, load_template as l, print_config_conflicts as p, resolve_entry as r };