@sveltejs/kit 1.0.0-next.451 → 1.0.0-next.454

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sveltejs/kit",
3
- "version": "1.0.0-next.451",
3
+ "version": "1.0.0-next.454",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/sveltejs/kit",
@@ -53,7 +53,8 @@
53
53
  "!src/core/**/fixtures",
54
54
  "!src/core/**/test",
55
55
  "types",
56
- "svelte-kit.js"
56
+ "svelte-kit.js",
57
+ "scripts/special-types"
57
58
  ],
58
59
  "exports": {
59
60
  "./package.json": "./package.json",
@@ -0,0 +1,8 @@
1
+ This module provides access to runtime environment variables, as defined by the platform you're running on. For example if you're using [`adapter-node`](https://github.com/sveltejs/kit/tree/master/packages/adapter-node) (or running [`vite preview`](https://kit.svelte.dev/docs/cli)), this is equivalent to `process.env`. This module only includes variables that _do not_ begin with [`config.kit.env.publicPrefix`](https://kit.svelte.dev/docs/configuration#kit-env-publicprefix).
2
+
3
+ This module cannot be imported into client-side code.
4
+
5
+ ```ts
6
+ import { env } from '$env/dynamic/private';
7
+ console.log(env.DEPLOYMENT_SPECIFIC_VARIABLE);
8
+ ```
@@ -0,0 +1,8 @@
1
+ Similar to [`$env/dynamic/private`](https://kit.svelte.dev/docs/modules#$env-dynamic-private), but only includes variables that begin with [`config.kit.env.publicPrefix`](https://kit.svelte.dev/docs/configuration#kit-env-publicprefix) (which defaults to `PUBLIC_`), and can therefore safely be exposed to client-side code.
2
+
3
+ Note that public dynamic environment variables must all be sent from the server to the client, causing larger network requests — when possible, use `$env/static/public` instead.
4
+
5
+ ```ts
6
+ import { env } from '$env/dynamic/public';
7
+ console.log(env.PUBLIC_DEPLOYMENT_SPECIFIC_VARIABLE);
8
+ ```
@@ -0,0 +1,19 @@
1
+ Environment variables [loaded by Vite](https://vitejs.dev/guide/env-and-mode.html#env-files) from `.env` files and `process.env`. Like [`$env/dynamic/private`](https://kit.svelte.dev/docs/modules#$env-dynamic-private), this module cannot be imported into client-side code. This module only includes variables that _do not_ begin with [`config.kit.env.publicPrefix`](https://kit.svelte.dev/docs/configuration#kit-env-publicprefix).
2
+
3
+ _Unlike_ [`$env/dynamic/private`](https://kit.svelte.dev/docs/modules#$env-dynamic-private), the values exported from this module are statically injected into your bundle at build time, enabling optimisations like dead code elimination.
4
+
5
+ ```ts
6
+ import { API_KEY } from '$env/static/private';
7
+ ```
8
+
9
+ Note that all environment variables referenced in your code should be declared (for example in an `.env` file), even if they don't have a value until the app is deployed:
10
+
11
+ ```
12
+ MY_FEATURE_FLAG=""
13
+ ```
14
+
15
+ You can override `.env` values from the command line like so:
16
+
17
+ ```bash
18
+ MY_FEATURE_FLAG="enabled" npm run dev
19
+ ```
@@ -0,0 +1,7 @@
1
+ Similar to [`$env/static/private`](https://kit.svelte.dev/docs/modules#$env-static-private), except that it only includes environment variables that begin with [`config.kit.env.publicPrefix`](https://kit.svelte.dev/docs/configuration#kit-env-publicprefix) (which defaults to `PUBLIC_`), and can therefore safely be exposed to client-side code.
2
+
3
+ Values are replaced statically at build time.
4
+
5
+ ```ts
6
+ import { PUBLIC_BASE_URL } from '$env/static/public';
7
+ ```
@@ -0,0 +1 @@
1
+ This is a simple alias to `src/lib`, or whatever directory is specified as [`config.kit.files.lib`](https://kit.svelte.dev/docs/configuration#files). It allows you to access common components and utility modules without `../../../../` nonsense.
package/src/core/env.js CHANGED
@@ -34,7 +34,7 @@ export function create_dynamic_module(type) {
34
34
  * @param {Record<string, string>} env
35
35
  * @returns {string}
36
36
  */
37
- export function create_types(id, env) {
37
+ export function create_static_types(id, env) {
38
38
  const declarations = Object.keys(env)
39
39
  .filter((k) => valid_identifier.test(k))
40
40
  .map((k) => `\texport const ${k}: string;`)
@@ -43,6 +43,21 @@ export function create_types(id, env) {
43
43
  return `declare module '${id}' {\n${declarations}\n}`;
44
44
  }
45
45
 
46
+ /**
47
+ * @param {string} id
48
+ * @param {Record<string, string>} env
49
+ * @returns {string}
50
+ */
51
+ export function create_dynamic_types(id, env) {
52
+ const properties = Object.keys(env)
53
+ .filter((k) => valid_identifier.test(k))
54
+ .map((k) => `\t\t${k}: string;`);
55
+
56
+ properties.push(`\t\t[key: string]: string | undefined;`);
57
+
58
+ return `declare module '${id}' {\n\texport const env: {\n${properties.join('\n')}\n\t}\n}`;
59
+ }
60
+
46
61
  export const reserved = new Set([
47
62
  'do',
48
63
  'if',
@@ -1,10 +1,43 @@
1
+ import fs from 'fs';
1
2
  import path from 'path';
2
3
  import { get_env } from '../../exports/vite/utils.js';
3
4
  import { GENERATED_COMMENT } from '../../constants.js';
4
- import { create_types } from '../env.js';
5
+ import { create_dynamic_types, create_static_types } from '../env.js';
5
6
  import { write_if_changed } from './utils.js';
7
+ import { fileURLToPath } from 'url';
6
8
 
7
- const types_reference = '/// <reference types="@sveltejs/kit" />\n\n';
9
+ const descriptions_dir = fileURLToPath(new URL('../../../scripts/special-types', import.meta.url));
10
+
11
+ /** @param {string} filename */
12
+ function read_description(filename) {
13
+ const content = fs.readFileSync(`${descriptions_dir}/${filename}`, 'utf8');
14
+ return `/**\n${content
15
+ .trim()
16
+ .split('\n')
17
+ .map((line) => ` * ${line}`)
18
+ .join('\n')}\n */`;
19
+ }
20
+
21
+ /**
22
+ * @param {{ public: Record<string, string>, private: Record<string, string> }} env
23
+ */
24
+ const template = (env) => `
25
+ ${GENERATED_COMMENT}
26
+
27
+ /// <reference types="@sveltejs/kit" />
28
+
29
+ ${read_description('$env+static+private.md')}
30
+ ${create_static_types('$env/static/private', env.private)}
31
+
32
+ ${read_description('$env+static+public.md')}
33
+ ${create_static_types('$env/static/public', env.public)}
34
+
35
+ ${read_description('$env+dynamic+private.md')}
36
+ ${create_dynamic_types('$env/dynamic/private', env.private)}
37
+
38
+ ${read_description('$env+dynamic+public.md')}
39
+ ${create_dynamic_types('$env/dynamic/public', env.public)}
40
+ `;
8
41
 
9
42
  /**
10
43
  * Writes ambient declarations including types reference to @sveltejs/kit,
@@ -16,12 +49,5 @@ const types_reference = '/// <reference types="@sveltejs/kit" />\n\n';
16
49
  export function write_ambient(config, mode) {
17
50
  const env = get_env(config.env, mode);
18
51
 
19
- write_if_changed(
20
- path.join(config.outDir, 'ambient.d.ts'),
21
- GENERATED_COMMENT +
22
- types_reference +
23
- create_types('$env/static/public', env.public) +
24
- '\n\n' +
25
- create_types('$env/static/private', env.private)
26
- );
52
+ write_if_changed(path.join(config.outDir, 'ambient.d.ts'), template(env));
27
53
  }
@@ -195,6 +195,7 @@ function kit() {
195
195
  async config(config, config_env) {
196
196
  vite_config_env = config_env;
197
197
  svelte_config = await load_config();
198
+
198
199
  env = get_env(svelte_config.kit.env, vite_config_env.mode);
199
200
 
200
201
  // The config is created in build_server for SSR mode and passed inline
@@ -44,6 +44,23 @@ function update_scroll_positions(index) {
44
44
  scroll_positions[index] = scroll_state();
45
45
  }
46
46
 
47
+ /** @type {Record<string, true>} */
48
+ let warned_about_attributes = {};
49
+
50
+ function check_for_removed_attributes() {
51
+ const attrs = ['prefetch', 'noscroll', 'reload'];
52
+ for (const attr of attrs) {
53
+ if (document.querySelector(`[sveltekit\\:${attr}]`)) {
54
+ if (!warned_about_attributes[attr]) {
55
+ warned_about_attributes[attr] = true;
56
+ console.error(
57
+ `The sveltekit:${attr} attribute has been replaced with data-sveltekit-${attr}`
58
+ );
59
+ }
60
+ }
61
+ }
62
+ }
63
+
47
64
  /**
48
65
  * @param {{
49
66
  * target: Element;
@@ -271,6 +288,8 @@ export function create_client({ target, base, trailing_slash }) {
271
288
  };
272
289
  root.$set(navigation_result.props);
273
290
  tick().then(() => (console.warn = warn));
291
+
292
+ check_for_removed_attributes();
274
293
  } else {
275
294
  root.$set(navigation_result.props);
276
295
  }
@@ -369,6 +388,8 @@ export function create_client({ target, base, trailing_slash }) {
369
388
  hydrate: true
370
389
  });
371
390
  console.warn = warn;
391
+
392
+ check_for_removed_attributes();
372
393
  } else {
373
394
  root = new Root({
374
395
  target,
@@ -1138,7 +1159,7 @@ export function create_client({ target, base, trailing_slash }) {
1138
1159
  /** @param {Event} event */
1139
1160
  const trigger_prefetch = (event) => {
1140
1161
  const a = find_anchor(event);
1141
- if (a && a.href && a.hasAttribute('sveltekit:prefetch')) {
1162
+ if (a && a.href && a.hasAttribute('data-sveltekit-prefetch')) {
1142
1163
  prefetch(get_href(a));
1143
1164
  }
1144
1165
  };
@@ -1195,7 +1216,7 @@ export function create_client({ target, base, trailing_slash }) {
1195
1216
  if (
1196
1217
  a.hasAttribute('download') ||
1197
1218
  rel.includes('external') ||
1198
- a.hasAttribute('sveltekit:reload')
1219
+ a.hasAttribute('data-sveltekit-reload')
1199
1220
  ) {
1200
1221
  return;
1201
1222
  }
@@ -1222,7 +1243,7 @@ export function create_client({ target, base, trailing_slash }) {
1222
1243
 
1223
1244
  navigate({
1224
1245
  url,
1225
- scroll: a.hasAttribute('sveltekit:noscroll') ? scroll_state() : null,
1246
+ scroll: a.hasAttribute('data-sveltekit-noscroll') ? scroll_state() : null,
1226
1247
  keepfocus: false,
1227
1248
  redirect_chain: [],
1228
1249
  details: {
@@ -1,4 +1,3 @@
1
- /** @type {App.PrivateEnv} */
2
1
  export let env = {};
3
2
 
4
3
  /** @type {(environment: Record<string, string>) => void} */
@@ -1,4 +1,3 @@
1
- /** @type {App.PublicEnv} */
2
1
  export let env = {};
3
2
 
4
3
  /** @type {(environment: Record<string, string>) => void} */
@@ -10,14 +10,10 @@
10
10
  * interface PageData {}
11
11
  *
12
12
  * interface Platform {}
13
- *
14
- * interface PrivateEnv {}
15
- *
16
- * interface PublicEnv {}
17
13
  * }
18
14
  * ```
19
15
  *
20
- * By populating these interfaces, you will gain type safety when using `env`, `event.locals` and `event.platform`.
16
+ * By populating these interfaces, you will gain type safety when using `event.locals`, `event.platform`, and `data` from `load` functions.
21
17
  *
22
18
  * Note that since it's an ambient declaration file, you have to be careful when using `import` statements. Once you add an `import`
23
19
  * at the top level, the declaration file is no longer considered ambient and you lose access to these typings in other files.
@@ -60,16 +56,6 @@ declare namespace App {
60
56
  * If your adapter provides [platform-specific context](https://kit.svelte.dev/docs/adapters#supported-environments-platform-specific-context) via `event.platform`, you can specify it here.
61
57
  */
62
58
  export interface Platform {}
63
-
64
- /**
65
- * The interface that defines the dynamic environment variables exported from `$env/dynamic/private`.
66
- */
67
- export interface PrivateEnv extends Record<string, string> {}
68
-
69
- /**
70
- * The interface that defines the dynamic environment variables exported from `$env/dynamic/public`.
71
- */
72
- export interface PublicEnv extends Record<string, string> {}
73
59
  }
74
60
 
75
61
  /**
@@ -94,39 +80,6 @@ declare module '$app/environment' {
94
80
  export const prerendering: boolean;
95
81
  }
96
82
 
97
- /**
98
- * This module provides access to runtime environment variables, as defined by the platform you're running on. For example
99
- * if you're using [`adapter-node`](https://github.com/sveltejs/kit/tree/master/packages/adapter-node) (or running
100
- * [`vite preview`](https://kit.svelte.dev/docs/cli)), this is equivalent to `process.env`. This module only includes
101
- * variables that _do not_ begin with [`config.kit.env.publicPrefix`](https://kit.svelte.dev/docs/configuration#kit-env-publicprefix).
102
- *
103
- * This module cannot be imported into client-side code.
104
- *
105
- * ```ts
106
- * import { env } from '$env/dynamic/private';
107
- * console.log(env.DEPLOYMENT_SPECIFIC_VARIABLE);
108
- * ```
109
- */
110
- declare module '$env/dynamic/private' {
111
- export let env: App.PrivateEnv;
112
- }
113
-
114
- /**
115
- * Similar to [`$env/dynamic/private`](https://kit.svelte.dev/docs/modules#$env-dynamic-private), but only includes
116
- * variables that begin with [`config.kit.env.publicPrefix`](https://kit.svelte.dev/docs/configuration#kit-env-publicprefix)
117
- * (which defaults to `PUBLIC_`), and can therefore safely be exposed to client-side code
118
- *
119
- * Note that public dynamic environment variables must all be sent from the server to the client, causing larger network requests — when possible, use `$env/static/public` instead.
120
- *
121
- * ```ts
122
- * import { env } from '$env/dynamic/public';
123
- * console.log(env.PUBLIC_DEPLOYMENT_SPECIFIC_VARIABLE);
124
- * ```
125
- */
126
- declare module '$env/dynamic/public' {
127
- export let env: App.PublicEnv;
128
- }
129
-
130
83
  /**
131
84
  * ```ts
132
85
  * import {
@@ -169,7 +122,7 @@ declare module '$app/navigation' {
169
122
  * 1. ensuring that the code for the page is loaded, and
170
123
  * 2. calling the page's load function with the appropriate options.
171
124
  *
172
- * This is the same behaviour that SvelteKit triggers when the user taps or mouses over an `<a>` element with `sveltekit:prefetch`.
125
+ * This is the same behaviour that SvelteKit triggers when the user taps or mouses over an `<a>` element with `data-sveltekit-prefetch`.
173
126
  * If the next navigation is to `href`, the values returned from load will be used, making navigation instantaneous.
174
127
  * Returns a Promise that resolves when the prefetch is complete.
175
128
  *