-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Consistent handling of environment variables #5663
Changes from all commits
1ec3064
2b5e0c4
91d0413
12a3fd1
498d4f2
5d4960d
08600df
d5baa47
87b3760
27b7cee
42ac833
6522e56
fb88ba9
736e1f3
9c1381a
351c4cb
944ff73
a040078
34801e1
035a2cd
48f8cba
61e3a67
1e6906a
1f70c4d
5f41062
069e3dc
5774f08
9091d2c
cf09ed7
b7b1fca
b4a1ee4
da76b16
905ef24
d2352b3
a858d19
17ffba0
af2b1aa
b0477aa
490f3fc
13bc94a
40a6eaa
a1ccfa9
312a7bc
672eb85
8ce2740
65ee08c
133cc7a
e56bf28
e677102
e5676a8
f768a9c
f685c40
3ac79c5
0429f87
79cc250
b0e96f5
0614a33
23676f2
dc8086d
5a43648
03b27b1
ce5c113
e0dc066
ca24e31
04c80fc
831c5b3
7f03ac7
ca72f76
8d8eaa0
c8e7ab5
84d06f4
caf7cab
3416554
6264418
d7a4159
c53600f
96c7a4a
eb34e95
63f6604
4449fb2
38025be
c9d222a
a08d383
d2b10d9
5253136
2d203db
bdffe39
b5cabd0
ec655c9
fd65c1e
26ad507
9c56f37
30f98f0
92a1559
c1a5d52
9499d83
b2d41ac
5f3dc7b
f257a0e
3c15b90
49ecafd
06fcd1c
4a17268
a5e3a87
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
'@sveltejs/adapter-cloudflare': patch | ||
'@sveltejs/adapter-cloudflare-workers': patch | ||
'@sveltejs/adapter-netlify': patch | ||
'@sveltejs/adapter-node': patch | ||
'@sveltejs/adapter-vercel': patch | ||
--- | ||
|
||
Initialise `env` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@sveltejs/kit': patch | ||
--- | ||
|
||
Add `$env/static/private`, `$env/static/public`, `$env/dynamic/private` and `$env/dynamic/public` modules |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,11 @@ import fs from 'fs'; | |
import ts from 'typescript'; | ||
import prettier from 'prettier'; | ||
import { mkdirp } from '../src/utils/filesystem.js'; | ||
import { fileURLToPath } from 'url'; | ||
|
||
/** @typedef {{ name: string, comment: string, snippet: string }} Extracted */ | ||
|
||
/** @type {Array<{ name: string, comment: string, exports: Extracted[], types: Extracted[] }>} */ | ||
/** @type {Array<{ name: string, comment: string, exports: Extracted[], types: Extracted[], exempt?: boolean }>} */ | ||
const modules = []; | ||
|
||
/** | ||
|
@@ -19,59 +20,72 @@ function get_types(code, statements) { | |
/** @type {Extracted[]} */ | ||
const types = []; | ||
|
||
for (const statement of statements) { | ||
if ( | ||
ts.isClassDeclaration(statement) || | ||
ts.isInterfaceDeclaration(statement) || | ||
ts.isTypeAliasDeclaration(statement) || | ||
ts.isModuleDeclaration(statement) || | ||
ts.isVariableStatement(statement) || | ||
ts.isFunctionDeclaration(statement) | ||
) { | ||
const name_node = ts.isVariableStatement(statement) | ||
? statement.declarationList.declarations[0] | ||
: statement; | ||
|
||
// @ts-ignore no idea why it's complaining here | ||
const name = name_node.name?.escapedText; | ||
|
||
let start = statement.pos; | ||
let comment = ''; | ||
|
||
// @ts-ignore i think typescript is bad at typescript | ||
if (statement.jsDoc) { | ||
// @ts-ignore | ||
comment = statement.jsDoc[0].comment; | ||
// @ts-ignore | ||
start = statement.jsDoc[0].end; | ||
if (statements) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. personal preference, but I'd write this as shown below. right now, it's a bit hard to see where the
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. my personal preference goes the other way 😆 i know you love an early return, i much prefer positive conditions over negative ones — way fewer mental contortions — and not having duplicated identical |
||
for (const statement of statements) { | ||
if ( | ||
ts.isClassDeclaration(statement) || | ||
ts.isInterfaceDeclaration(statement) || | ||
ts.isTypeAliasDeclaration(statement) || | ||
ts.isModuleDeclaration(statement) || | ||
ts.isVariableStatement(statement) || | ||
ts.isFunctionDeclaration(statement) | ||
) { | ||
const name_node = ts.isVariableStatement(statement) | ||
? statement.declarationList.declarations[0] | ||
: statement; | ||
|
||
// @ts-ignore no idea why it's complaining here | ||
const name = name_node.name?.escapedText; | ||
|
||
let start = statement.pos; | ||
let comment = ''; | ||
|
||
// @ts-ignore i think typescript is bad at typescript | ||
if (statement.jsDoc) { | ||
// @ts-ignore | ||
comment = statement.jsDoc[0].comment; | ||
// @ts-ignore | ||
start = statement.jsDoc[0].end; | ||
} | ||
|
||
const i = code.indexOf('export', start); | ||
start = i + 6; | ||
|
||
const snippet = prettier.format(code.slice(start, statement.end).trim(), { | ||
parser: 'typescript', | ||
printWidth: 80, | ||
useTabs: true, | ||
singleQuote: true, | ||
trailingComma: 'none' | ||
}); | ||
|
||
const collection = | ||
ts.isVariableStatement(statement) || ts.isFunctionDeclaration(statement) | ||
? exports | ||
: types; | ||
|
||
collection.push({ name, comment, snippet }); | ||
} | ||
|
||
const i = code.indexOf('export', start); | ||
start = i + 6; | ||
|
||
const snippet = prettier.format(code.slice(start, statement.end).trim(), { | ||
parser: 'typescript', | ||
printWidth: 80, | ||
useTabs: true, | ||
singleQuote: true, | ||
trailingComma: 'none' | ||
}); | ||
|
||
const collection = | ||
ts.isVariableStatement(statement) || ts.isFunctionDeclaration(statement) ? exports : types; | ||
|
||
collection.push({ name, comment, snippet }); | ||
} else { | ||
// console.log(statement.kind); | ||
} | ||
} | ||
|
||
types.sort((a, b) => (a.name < b.name ? -1 : 1)); | ||
exports.sort((a, b) => (a.name < b.name ? -1 : 1)); | ||
types.sort((a, b) => (a.name < b.name ? -1 : 1)); | ||
exports.sort((a, b) => (a.name < b.name ? -1 : 1)); | ||
} | ||
|
||
return { types, exports }; | ||
} | ||
|
||
/** | ||
* Type declarations include fully qualified URLs so that they become links when | ||
* you hover over names in an editor with TypeScript enabled. We need to remove | ||
* the origin so that they become root-relative, so that they work in preview | ||
* deployments and when developing locally | ||
* @param {string} str | ||
*/ | ||
function strip_origin(str) { | ||
return str.replace(/https:\/\/kit\.svelte\.dev/g, ''); | ||
} | ||
|
||
{ | ||
const code = fs.readFileSync('types/index.d.ts', 'utf-8'); | ||
const node = ts.createSourceFile('index.d.ts', code, ts.ScriptTarget.Latest); | ||
|
@@ -95,13 +109,20 @@ function get_types(code, statements) { | |
}); | ||
} | ||
|
||
modules.push({ | ||
name: '$lib', | ||
comment: | ||
'This is a simple alias to `src/lib`, or whatever directory is specified as [`config.kit.files.lib`](/docs/configuration#files). It allows you to access common components and utility modules without `../../../../` nonsense.', | ||
exports: [], | ||
types: [] | ||
}); | ||
const dir = fileURLToPath(new URL('./special-types', import.meta.url).href); | ||
for (const file of fs.readdirSync(dir)) { | ||
if (!file.endsWith('.md')) continue; | ||
|
||
const comment = strip_origin(fs.readFileSync(`${dir}/${file}`, 'utf-8')); | ||
|
||
modules.push({ | ||
name: file.replace(/\+/g, '/').slice(0, -3), | ||
comment, | ||
exports: [], | ||
types: [], | ||
exempt: true | ||
}); | ||
} | ||
|
||
{ | ||
const code = fs.readFileSync('types/ambient.d.ts', 'utf-8'); | ||
|
@@ -113,13 +134,13 @@ modules.push({ | |
const name = statement.name.text || statement.name.escapedText; | ||
|
||
// @ts-ignore | ||
const comment = statement.jsDoc?.[0].comment ?? ''; | ||
const comment = strip_origin(statement.jsDoc?.[0].comment ?? ''); | ||
|
||
modules.push({ | ||
name, | ||
comment, | ||
// @ts-ignore | ||
...get_types(code, statement.body.statements) | ||
...get_types(code, statement.body?.statements) | ||
}); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
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-platform), this module cannot be imported into client-side code. | ||
|
||
_Unlike_ [`$env/dynamic/private`](https://kit.svelte.dev/docs/modules#$env-dynamic-platform), the values exported from this module are statically injected into your bundle at build time, enabling optimisations like dead code elimination. | ||
|
||
```ts | ||
import { API_KEY } from '$env/static/private'; | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
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. | ||
|
||
Values are replaced statically at build time. | ||
|
||
```ts | ||
import { PUBLIC_BASE_URL } from '$env/static/public'; | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a reason we don't call this
env/runtime/private
to match theenv/dynamic/private
above it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
felt unnecessary since we don't have entry points for
static
. in any case i'm thinking of these as temporary — this whole folder structure is due for a reshuffle (it's weird that we haveenv
alongsideenv-[type]
, for example)