@sveltejs/kit 1.0.0-next.204 → 1.0.0-next.209

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.
@@ -12,7 +12,7 @@ import require$$0$3 from 'domain';
12
12
  import 'querystring';
13
13
  import require$$10 from 'vm';
14
14
  import CheapWatch from 'cheap-watch';
15
- import { r as rimraf, c as copy_assets, p as print_config_conflicts, a as resolve_entry, $ } from '../cli.js';
15
+ import { r as rimraf, c as copy_assets, p as print_config_conflicts, g as get_mime_lookup, a as resolve_entry, $ } from '../cli.js';
16
16
  import vite from 'vite';
17
17
  import { respond } from '../ssr.js';
18
18
  import { d as deep_merge, c as create_manifest_data, a as create_app } from './index2.js';
@@ -23,6 +23,7 @@ import { c as coalesce_to_error } from './error.js';
23
23
  import 'sade';
24
24
  import 'net';
25
25
  import './url.js';
26
+ import './misc.js';
26
27
  import 'node:http';
27
28
  import 'node:https';
28
29
  import 'node:zlib';
@@ -4277,7 +4278,7 @@ class Watcher extends EventEmitter {
4277
4278
  this.config.kit.files.lib,
4278
4279
  this.config.kit.files.routes,
4279
4280
  path__default.resolve(this.cwd, 'src'),
4280
- path__default.resolve(this.cwd, '.svelte-kit'),
4281
+ path__default.resolve(this.cwd, SVELTE_KIT),
4281
4282
  path__default.resolve(this.cwd, 'node_modules'),
4282
4283
  path__default.resolve(vite.searchForWorkspaceRoot(this.cwd), 'node_modules')
4283
4284
  ])
@@ -4302,7 +4303,7 @@ class Watcher extends EventEmitter {
4302
4303
  // don't warn on overriding defaults
4303
4304
  const [modified_vite_config] = deep_merge(default_config, vite_config);
4304
4305
 
4305
- const kit_plugin = await create_plugin(this.config, this.dir, this.cwd, () => {
4306
+ const kit_plugin = await create_plugin(this.config, this.dir, this.https, () => {
4306
4307
  if (!this.manifest) {
4307
4308
  throw new Error('Manifest is not available');
4308
4309
  }
@@ -4374,33 +4375,85 @@ class Watcher extends EventEmitter {
4374
4375
  cwd: this.cwd
4375
4376
  });
4376
4377
 
4377
- /** @type {import('types/internal').SSRManifest} */
4378
+ /** @type {import('types/app').SSRManifest} */
4378
4379
  this.manifest = {
4379
- assets: manifest_data.assets,
4380
- layout: manifest_data.layout,
4381
- error: manifest_data.error,
4382
- routes: manifest_data.routes.map((route) => {
4383
- if (route.type === 'page') {
4380
+ appDir: this.config.kit.appDir,
4381
+ assets: new Set(manifest_data.assets.map((asset) => asset.file)),
4382
+ _: {
4383
+ mime: get_mime_lookup(manifest_data),
4384
+ entry: {
4385
+ file: `/${SVELTE_KIT}/dev/runtime/internal/start.js`,
4386
+ css: [],
4387
+ js: []
4388
+ },
4389
+ nodes: manifest_data.components.map((id) => {
4390
+ return async () => {
4391
+ const url = `/${id}`;
4392
+
4393
+ if (!this.vite) throw new Error('Vite server has not been initialized');
4394
+
4395
+ const module = /** @type {SSRComponent} */ (await this.vite.ssrLoadModule(url));
4396
+ const node = await this.vite.moduleGraph.getModuleByUrl(url);
4397
+
4398
+ if (!node) throw new Error(`Could not find node for ${url}`);
4399
+
4400
+ const deps = new Set();
4401
+ find_deps(node, deps);
4402
+
4403
+ const styles = new Set();
4404
+
4405
+ for (const dep of deps) {
4406
+ const parsed = new URL(dep.url, 'http://localhost/');
4407
+ const query = parsed.searchParams;
4408
+
4409
+ // TODO what about .scss files, etc?
4410
+ if (
4411
+ dep.file.endsWith('.css') ||
4412
+ (query.has('svelte') && query.get('type') === 'style')
4413
+ ) {
4414
+ try {
4415
+ const mod = await this.vite.ssrLoadModule(dep.url);
4416
+ styles.add(mod.default);
4417
+ } catch {
4418
+ // this can happen with dynamically imported modules, I think
4419
+ // because the Vite module graph doesn't distinguish between
4420
+ // static and dynamic imports? TODO investigate, submit fix
4421
+ }
4422
+ }
4423
+ }
4424
+
4425
+ return {
4426
+ module,
4427
+ entry: url.endsWith('.svelte') ? url : url + '?import',
4428
+ css: [],
4429
+ js: [],
4430
+ styles: Array.from(styles)
4431
+ };
4432
+ };
4433
+ }),
4434
+ routes: manifest_data.routes.map((route) => {
4435
+ if (route.type === 'page') {
4436
+ return {
4437
+ type: 'page',
4438
+ pattern: route.pattern,
4439
+ params: get_params(route.params),
4440
+ a: route.a.map((id) => manifest_data.components.indexOf(id)),
4441
+ b: route.b.map((id) => manifest_data.components.indexOf(id))
4442
+ };
4443
+ }
4444
+
4384
4445
  return {
4385
- type: 'page',
4446
+ type: 'endpoint',
4386
4447
  pattern: route.pattern,
4387
4448
  params: get_params(route.params),
4388
- a: route.a,
4389
- b: route.b
4449
+ load: async () => {
4450
+ if (!this.vite) throw new Error('Vite server has not been initialized');
4451
+ const url = path__default.resolve(this.cwd, route.file);
4452
+ return await this.vite.ssrLoadModule(url);
4453
+ }
4390
4454
  };
4391
- }
4392
-
4393
- return {
4394
- type: 'endpoint',
4395
- pattern: route.pattern,
4396
- params: get_params(route.params),
4397
- load: async () => {
4398
- if (!this.vite) throw new Error('Vite server has not been initialized');
4399
- const url = path__default.resolve(this.cwd, route.file);
4400
- return await this.vite.ssrLoadModule(url);
4401
- }
4402
- };
4403
- })
4455
+ })
4456
+ }
4404
4457
  };
4405
4458
  }
4406
4459
 
@@ -4423,31 +4476,15 @@ function get_params(array) {
4423
4476
  // src/routes/[x]/[y]/[z]/svelte, create a function
4424
4477
  // that turns a RegExpExecArray into ({ x, y, z })
4425
4478
 
4426
- // input has already been decoded by decodeURI
4427
- // now handle the rest that decodeURIComponent would do
4428
- const d = /** @param {string} s */ (s) =>
4429
- s
4430
- .replace(/%23/g, '#')
4431
- .replace(/%3[Bb]/g, ';')
4432
- .replace(/%2[Cc]/g, ',')
4433
- .replace(/%2[Ff]/g, '/')
4434
- .replace(/%3[Ff]/g, '?')
4435
- .replace(/%3[Aa]/g, ':')
4436
- .replace(/%40/g, '@')
4437
- .replace(/%26/g, '&')
4438
- .replace(/%3[Dd]/g, '=')
4439
- .replace(/%2[Bb]/g, '+')
4440
- .replace(/%24/g, '$');
4441
-
4442
4479
  /** @param {RegExpExecArray} match */
4443
4480
  const fn = (match) => {
4444
4481
  /** @type {Record<string, string>} */
4445
4482
  const params = {};
4446
4483
  array.forEach((key, i) => {
4447
4484
  if (key.startsWith('...')) {
4448
- params[key.slice(3)] = d(match[i + 1] || '');
4485
+ params[key.slice(3)] = match[i + 1] || '';
4449
4486
  } else {
4450
- params[key] = d(match[i + 1]);
4487
+ params[key] = match[i + 1];
4451
4488
  }
4452
4489
  });
4453
4490
  return params;
@@ -4459,28 +4496,15 @@ function get_params(array) {
4459
4496
  /**
4460
4497
  * @param {import('types/config').ValidatedConfig} config
4461
4498
  * @param {string} dir
4462
- * @param {string} cwd
4463
- * @param {() => import('types/internal').SSRManifest} get_manifest
4499
+ * @param {boolean} https
4500
+ * @param {() => import('types/app').SSRManifest} get_manifest
4464
4501
  */
4465
- async function create_plugin(config, dir, cwd, get_manifest) {
4502
+ async function create_plugin(config, dir, https, get_manifest) {
4466
4503
  /**
4467
4504
  * @type {amp_validator.Validator?}
4468
4505
  */
4469
4506
  const validator = config.kit.amp ? await amphtmlValidator.getInstance() : null;
4470
4507
 
4471
- /**
4472
- * @param {import('vite').ModuleNode} node
4473
- * @param {Set<import('vite').ModuleNode>} deps
4474
- */
4475
- const find_deps = (node, deps) => {
4476
- for (const dep of node.importedModules) {
4477
- if (!deps.has(dep)) {
4478
- deps.add(dep);
4479
- find_deps(dep, deps);
4480
- }
4481
- }
4482
- };
4483
-
4484
4508
  /**
4485
4509
  * @param {vite.ViteDevServer} vite
4486
4510
  */
@@ -4550,27 +4574,16 @@ async function create_plugin(config, dir, cwd, get_manifest) {
4550
4574
  return res.end(err.reason || 'Invalid request body');
4551
4575
  }
4552
4576
 
4553
- const host = /** @type {string} */ (
4554
- config.kit.host || req.headers[config.kit.hostHeader || 'host']
4555
- );
4556
-
4557
4577
  const rendered = await respond(
4558
4578
  {
4579
+ url: new URL(`${https ? 'https' : 'http'}://${req.headers.host}${req.url}`),
4559
4580
  headers: /** @type {import('types/helper').RequestHeaders} */ (req.headers),
4560
4581
  method: req.method,
4561
- host,
4562
- path: parsed.pathname.replace(config.kit.paths.base, ''),
4563
- query: parsed.searchParams,
4564
4582
  rawBody: body
4565
4583
  },
4566
4584
  {
4567
4585
  amp: config.kit.amp,
4568
4586
  dev: true,
4569
- entry: {
4570
- file: `/${SVELTE_KIT}/dev/runtime/internal/start.js`,
4571
- css: [],
4572
- js: []
4573
- },
4574
4587
  floc: config.kit.floc,
4575
4588
  get_stack: (error) => {
4576
4589
  vite.ssrFixStacktrace(error);
@@ -4582,52 +4595,12 @@ async function create_plugin(config, dir, cwd, get_manifest) {
4582
4595
  },
4583
4596
  hooks,
4584
4597
  hydrate: config.kit.hydrate,
4598
+ manifest: get_manifest(),
4585
4599
  paths: {
4586
4600
  base: config.kit.paths.base,
4587
4601
  assets: config.kit.paths.assets ? SVELTE_KIT_ASSETS : config.kit.paths.base
4588
4602
  },
4589
- load_component: async (id) => {
4590
- const url = `/${id}`;
4591
-
4592
- const module = /** @type {SSRComponent} */ (await vite.ssrLoadModule(url));
4593
- const node = await vite.moduleGraph.getModuleByUrl(url);
4594
-
4595
- if (!node) throw new Error(`Could not find node for ${url}`);
4596
-
4597
- const deps = new Set();
4598
- find_deps(node, deps);
4599
-
4600
- const styles = new Set();
4601
-
4602
- for (const dep of deps) {
4603
- const parsed = new URL(dep.url, 'http://localhost/');
4604
- const query = parsed.searchParams;
4605
-
4606
- // TODO what about .scss files, etc?
4607
- if (
4608
- dep.file.endsWith('.css') ||
4609
- (query.has('svelte') && query.get('type') === 'style')
4610
- ) {
4611
- try {
4612
- const mod = await vite.ssrLoadModule(dep.url);
4613
- styles.add(mod.default);
4614
- } catch {
4615
- // this can happen with dynamically imported modules, I think
4616
- // because the Vite module graph doesn't distinguish between
4617
- // static and dynamic imports? TODO investigate, submit fix
4618
- }
4619
- }
4620
- }
4621
-
4622
- return {
4623
- module,
4624
- entry: url.endsWith('.svelte') ? url : url + '?import',
4625
- css: [],
4626
- js: [],
4627
- styles: Array.from(styles)
4628
- };
4629
- },
4630
- manifest: get_manifest(),
4603
+ prefix: '',
4631
4604
  prerender: config.kit.prerender.enabled,
4632
4605
  read: (file) => fs__default.readFileSync(path__default.join(config.kit.files.assets, file)),
4633
4606
  root,
@@ -4743,4 +4716,17 @@ function remove_html_middlewares(server) {
4743
4716
  }
4744
4717
  }
4745
4718
 
4719
+ /**
4720
+ * @param {import('vite').ModuleNode} node
4721
+ * @param {Set<import('vite').ModuleNode>} deps
4722
+ */
4723
+ function find_deps(node, deps) {
4724
+ for (const dep of node.importedModules) {
4725
+ if (!deps.has(dep)) {
4726
+ deps.add(dep);
4727
+ find_deps(dep, deps);
4728
+ }
4729
+ }
4730
+ }
4731
+
4746
4732
  export { dev };
@@ -1,5 +1,6 @@
1
1
  import fs__default from 'fs';
2
2
  import path__default from 'path';
3
+ import { s } from './misc.js';
3
4
  import { m as mkdirp, b as posixify } from '../cli.js';
4
5
 
5
6
  /**
@@ -99,8 +100,6 @@ function write_if_changed(file, code) {
99
100
  }
100
101
  }
101
102
 
102
- const s = JSON.stringify;
103
-
104
103
  /** @typedef {import('types/internal').ManifestData} ManifestData */
105
104
 
106
105
  /**
@@ -213,7 +212,7 @@ function generate_app(manifest_data) {
213
212
  while (l--) {
214
213
  pyramid = `
215
214
  {#if components[${l + 1}]}
216
- <svelte:component this={components[${l}]} {...(props_${l} || {})}>
215
+ <svelte:component this={components[${l}]} {...(props_${l} || {})}>
217
216
  ${pyramid.replace(/\n/g, '\n\t\t\t\t\t')}
218
217
  </svelte:component>
219
218
  {:else}
@@ -393,7 +392,7 @@ var mime = new Mime(standard, other);
393
392
  * @typedef {{
394
393
  * content: string;
395
394
  * dynamic: boolean;
396
- * spread: boolean;
395
+ * rest: boolean;
397
396
  * }} Part
398
397
  * @typedef {{
399
398
  * basename: string;
@@ -521,13 +520,13 @@ function create_manifest_data({ config, output, cwd = process.cwd() }) {
521
520
  if (last_part.dynamic) {
522
521
  last_segment.push({
523
522
  dynamic: false,
524
- spread: false,
523
+ rest: false,
525
524
  content: item.route_suffix
526
525
  });
527
526
  } else {
528
527
  last_segment[last_segment.length - 1] = {
529
528
  dynamic: false,
530
- spread: false,
529
+ rest: false,
531
530
  content: `${last_part.content}${item.route_suffix}`
532
531
  };
533
532
  }
@@ -544,6 +543,18 @@ function create_manifest_data({ config, output, cwd = process.cwd() }) {
544
543
  const params = parent_params.slice();
545
544
  params.push(...item.parts.filter((p) => p.dynamic).map((p) => p.content));
546
545
 
546
+ // TODO seems slightly backwards to derive the simple segment representation
547
+ // from the more complex form, rather than vice versa — maybe swap it round
548
+ const simple_segments = segments.map((segment) => {
549
+ return {
550
+ dynamic: segment.some((part) => part.dynamic),
551
+ rest: segment.some((part) => part.rest),
552
+ content: segment
553
+ .map((part) => (part.dynamic ? `[${part.content}]` : part.content))
554
+ .join('')
555
+ };
556
+ });
557
+
547
558
  if (item.is_dir) {
548
559
  const layout_reset = find_layout('__layout.reset', item.file);
549
560
  const layout = find_layout('__layout', item.file);
@@ -593,6 +604,7 @@ function create_manifest_data({ config, output, cwd = process.cwd() }) {
593
604
 
594
605
  routes.push({
595
606
  type: 'page',
607
+ segments: simple_segments,
596
608
  pattern,
597
609
  params,
598
610
  path,
@@ -604,6 +616,7 @@ function create_manifest_data({ config, output, cwd = process.cwd() }) {
604
616
 
605
617
  routes.push({
606
618
  type: 'endpoint',
619
+ segments: simple_segments,
607
620
  pattern,
608
621
  file: item.file,
609
622
  params
@@ -672,14 +685,13 @@ function comparator(a, b) {
672
685
  if (!a_sub_part) return 1; // b is more specific, so goes first
673
686
  if (!b_sub_part) return -1;
674
687
 
675
- // if spread, order later
676
- if (a_sub_part.spread && b_sub_part.spread) {
688
+ if (a_sub_part.rest && b_sub_part.rest) {
677
689
  // sort alphabetically
678
690
  return a_sub_part.content < b_sub_part.content ? -1 : 1;
679
691
  }
680
692
 
681
- // If one is ...spread order it later
682
- if (a_sub_part.spread !== b_sub_part.spread) return a_sub_part.spread ? 1 : -1;
693
+ // If one is ...rest order it later
694
+ if (a_sub_part.rest !== b_sub_part.rest) return a_sub_part.rest ? 1 : -1;
683
695
 
684
696
  if (a_sub_part.dynamic !== b_sub_part.dynamic) {
685
697
  return a_sub_part.dynamic ? 1 : -1;
@@ -721,7 +733,7 @@ function get_parts(part, file) {
721
733
  result.push({
722
734
  content,
723
735
  dynamic,
724
- spread: dynamic && /^\.{3}.+$/.test(content)
736
+ rest: dynamic && /^\.{3}.+$/.test(content)
725
737
  });
726
738
  });
727
739
 
@@ -735,7 +747,7 @@ function get_parts(part, file) {
735
747
  function get_pattern(segments, add_trailing_slash) {
736
748
  const path = segments
737
749
  .map((segment) => {
738
- return segment[0].spread
750
+ return segment[0].rest
739
751
  ? '(?:\\/(.*))?'
740
752
  : '\\/' +
741
753
  segment