Skip to content

Commit 9c42c90

Browse files
committed
Merge branch 'master' into feature/use-valid-html-attributes
2 parents 8e9224a + 3d12949 commit 9c42c90

File tree

23 files changed

+111
-82
lines changed

23 files changed

+111
-82
lines changed

Diff for: .changeset/dull-cheetahs-clean.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
[breaking] remove App.PrivateEnv and App.PublicEnv in favour of generated types

Diff for: .changeset/lovely-otters-lie.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'create-svelte': patch
3+
---
4+
5+
Remove App.PrivateEnv and App.PublicEnv placeholders

Diff for: .changeset/pre.json

+3
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@
199199
"dry-cars-dance",
200200
"dry-jokes-explain",
201201
"dry-yaks-clean",
202+
"dull-cheetahs-clean",
202203
"dull-ducks-report",
203204
"dull-emus-tap",
204205
"dull-lobsters-act",
@@ -545,6 +546,7 @@
545546
"lovely-foxes-visit",
546547
"lovely-hairs-applaud",
547548
"lovely-mice-search",
549+
"lovely-otters-lie",
548550
"lovely-seahorses-sell",
549551
"lucky-bottles-kick",
550552
"lucky-glasses-sell",
@@ -1164,6 +1166,7 @@
11641166
"two-tigers-cry",
11651167
"unlucky-cameras-appear",
11661168
"unlucky-dodos-greet",
1169+
"unlucky-elephants-appear",
11671170
"unlucky-mugs-dress",
11681171
"unlucky-oranges-exercise",
11691172
"unlucky-otters-tickle",

Diff for: .changeset/unlucky-elephants-appear.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
Include type descriptions for ambient declarations

Diff for: packages/create-svelte/CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# create-svelte
22

3+
## 2.0.0-next.166
4+
5+
### Patch Changes
6+
7+
- Remove App.PrivateEnv and App.PublicEnv placeholders ([#6413](https://github.com/sveltejs/kit/pull/6413))
8+
39
## 2.0.0-next.165
410

511
### Patch Changes

Diff for: packages/create-svelte/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "create-svelte",
3-
"version": "2.0.0-next.165",
3+
"version": "2.0.0-next.166",
44
"repository": {
55
"type": "git",
66
"url": "https://github.com/sveltejs/kit",

Diff for: packages/create-svelte/templates/default/src/app.d.ts

-4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,4 @@ declare namespace App {
99
// interface PageData {}
1010

1111
// interface Platform {}
12-
13-
// interface PrivateEnv {}
14-
15-
// interface PublicEnv {}
1612
}

Diff for: packages/create-svelte/templates/libskeleton/src/app.d.ts

-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,4 @@ declare namespace App {
77
// interface Locals {}
88
// interface PageData {}
99
// interface Platform {}
10-
// interface PrivateEnv {}
11-
// interface PublicEnv {}
1210
}

Diff for: packages/create-svelte/templates/skeleton/src/app.d.ts

-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,4 @@ declare namespace App {
55
// interface Locals {}
66
// interface PageData {}
77
// interface Platform {}
8-
// interface PrivateEnv {}
9-
// interface PublicEnv {}
108
}

Diff for: packages/kit/CHANGELOG.md

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
# @sveltejs/kit
22

3+
## 1.0.0-next.453
4+
5+
### Patch Changes
6+
7+
- Include type descriptions for ambient declarations ([#6416](https://github.com/sveltejs/kit/pull/6416))
8+
9+
## 1.0.0-next.452
10+
11+
### Patch Changes
12+
13+
- [breaking] remove App.PrivateEnv and App.PublicEnv in favour of generated types ([#6413](https://github.com/sveltejs/kit/pull/6413))
14+
315
## 1.0.0-next.451
416

517
### Patch Changes

Diff for: packages/kit/package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@sveltejs/kit",
3-
"version": "1.0.0-next.451",
3+
"version": "1.0.0-next.453",
44
"repository": {
55
"type": "git",
66
"url": "https://github.com/sveltejs/kit",
@@ -53,7 +53,8 @@
5353
"!src/core/**/fixtures",
5454
"!src/core/**/test",
5555
"types",
56-
"svelte-kit.js"
56+
"svelte-kit.js",
57+
"scripts/special-types"
5758
],
5859
"scripts": {
5960
"build": "npm run types",
Original file line numberDiff line numberDiff line change
@@ -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+
```
Original file line numberDiff line numberDiff line change
@@ -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+
```

Diff for: packages/kit/src/core/env.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export function create_dynamic_module(type) {
3434
* @param {Record<string, string>} env
3535
* @returns {string}
3636
*/
37-
export function create_types(id, env) {
37+
export function create_static_types(id, env) {
3838
const declarations = Object.keys(env)
3939
.filter((k) => valid_identifier.test(k))
4040
.map((k) => `\texport const ${k}: string;`)
@@ -43,6 +43,21 @@ export function create_types(id, env) {
4343
return `declare module '${id}' {\n${declarations}\n}`;
4444
}
4545

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+
4661
export const reserved = new Set([
4762
'do',
4863
'if',

Diff for: packages/kit/src/core/sync/write_ambient.js

+36-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,43 @@
1+
import fs from 'fs';
12
import path from 'path';
23
import { get_env } from '../../exports/vite/utils.js';
34
import { GENERATED_COMMENT } from '../../constants.js';
4-
import { create_types } from '../env.js';
5+
import { create_dynamic_types, create_static_types } from '../env.js';
56
import { write_if_changed } from './utils.js';
7+
import { fileURLToPath } from 'url';
68

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+
`;
841

942
/**
1043
* Writes ambient declarations including types reference to @sveltejs/kit,
@@ -16,12 +49,5 @@ const types_reference = '/// <reference types="@sveltejs/kit" />\n\n';
1649
export function write_ambient(config, mode) {
1750
const env = get_env(config.env, mode);
1851

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));
2753
}

Diff for: packages/kit/src/runtime/env-private.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/** @type {App.PrivateEnv} */
21
export let env = {};
32

43
/** @type {(environment: Record<string, string>) => void} */

Diff for: packages/kit/src/runtime/env-public.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/** @type {App.PublicEnv} */
21
export let env = {};
32

43
/** @type {(environment: Record<string, string>) => void} */

Diff for: packages/kit/test/apps/basics/test/client.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -672,12 +672,12 @@ test.describe.serial('Invalidation', () => {
672672

673673
await page.click('button');
674674
await page.waitForLoadState('networkidle');
675-
await page.waitForTimeout(0); // apparently necessary
675+
await page.waitForTimeout(200); // apparently necessary
676676
expect(await page.textContent('h1')).toBe('a: 2, b: 3');
677677

678678
await page.click('button');
679679
await page.waitForLoadState('networkidle');
680-
await page.waitForTimeout(0);
680+
await page.waitForTimeout(200);
681681
expect(await page.textContent('h1')).toBe('a: 4, b: 5');
682682
});
683683
});

Diff for: packages/kit/test/build-errors/apps/private-dynamic-env-dynamic-import/src/app.d.ts

-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,4 @@
44
declare namespace App {
55
// interface Locals {}
66
// interface Platform {}
7-
// interface PrivateEnv {}
8-
// interface PublicEnv {}
97
}

Diff for: packages/kit/test/build-errors/apps/private-dynamic-env/src/app.d.ts

-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,4 @@
44
declare namespace App {
55
// interface Locals {}
66
// interface Platform {}
7-
// interface PrivateEnv {}
8-
// interface PublicEnv {}
97
}

Diff for: packages/kit/test/build-errors/apps/private-static-env-dynamic-import/src/app.d.ts

-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,4 @@
44
declare namespace App {
55
// interface Locals {}
66
// interface Platform {}
7-
// interface PrivateEnv {}
8-
// interface PublicEnv {}
97
}

Diff for: packages/kit/test/build-errors/apps/private-static-env/src/app.d.ts

-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,4 @@
44
declare namespace App {
55
// interface Locals {}
66
// interface Platform {}
7-
// interface PrivateEnv {}
8-
// interface PublicEnv {}
97
}

Diff for: packages/kit/types/ambient.d.ts

+1-48
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,10 @@
1010
* interface PageData {}
1111
*
1212
* interface Platform {}
13-
*
14-
* interface PrivateEnv {}
15-
*
16-
* interface PublicEnv {}
1713
* }
1814
* ```
1915
*
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.
2117
*
2218
* Note that since it's an ambient declaration file, you have to be careful when using `import` statements. Once you add an `import`
2319
* 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 {
6056
* 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.
6157
*/
6258
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> {}
7359
}
7460

7561
/**
@@ -94,39 +80,6 @@ declare module '$app/environment' {
9480
export const prerendering: boolean;
9581
}
9682

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-
13083
/**
13184
* ```ts
13285
* import {

0 commit comments

Comments
 (0)