@sveltejs/kit 1.0.0-next.451 → 1.0.0-next.452
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 +1 -1
- package/src/core/env.js +16 -1
- package/src/core/sync/write_ambient.js +36 -10
- package/src/runtime/env-private.js +0 -1
- package/src/runtime/env-public.js +0 -1
- package/types/ambient.d.ts +1 -48
package/package.json
CHANGED
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
|
|
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 {
|
|
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
|
|
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
|
}
|
package/types/ambient.d.ts
CHANGED
|
@@ -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 `
|
|
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 {
|