From b1a40552aa6d9bd16d1f2bb5d7359dcc0101d2ea Mon Sep 17 00:00:00 2001 From: "S. Elliott Johnson" Date: Tue, 26 Jul 2022 11:46:26 -0600 Subject: [PATCH] feat: Consistent handling of environment variables (#5663) * feat: Add env config object * feat: Added types * feat: Add .d.ts import to tsconfig * feat: Generate env var dump code * fix: imports * fix: Remove old error mechanism * feat: Rough edges of server-side module protection * fix: Performance and add Vite overlay * feat: First full draft of static environment vars * fix unit tests * fix: Rich is smarter than I am * feat: Let know about Vite mode * feat: Added to sync CLI * feat: Added runtime variable support * feat: Make server.init multicalls a noop * feat: Set runtime env in preview * this doesn't work?!: Set runtime env in dev * feat: Added runtime env handler to cloudflare for testing * Update packages/kit/src/cli.js * Update packages/kit/src/core/sync/write_env.js * Update packages/kit/src/core/sync/write_env.js * Update packages/kit/src/vite/build/build_server.js * Update packages/kit/src/core/sync/write_env.js * make mode required * move env/runtime to a file * simplify write_env * Update packages/kit/src/vite/preview/index.js * remove unnecessary index.js suffix * simplify * tabs * make init sync, make options required * format * cast process.env * remove the overlay stuff, just throw an error * just throw * simplify traversal * make build-time traversal more efficient * move dev code into dev module * always set env * simplify * remove hard-coded .svelte-kit * rename env/runtime to env/platform and get it working in dev * remove outdated comment * docs * App.RuntimeEnv -> App.Env * add Env interface where necessary * update more adapters * changesets * tweak changesets * add a test * replace doc links * fix: Invalid js identifiers skipped * rename modules * fix some stuff, break some stuff * update tests * ok i think thats almost everything * oops * hide functions * separate PrivateEnv from PublicEnv * expose entry points * fix turbo config * fix * Update .changeset/beige-gorillas-tell.md * Update packages/kit/src/runtime/env-public.js * Update packages/kit/src/runtime/env-private.js * Update packages/kit/src/runtime/env-public.js * Update packages/kit/src/runtime/env-private.js * Update packages/kit/src/core/sync/sync.js Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> * Update packages/kit/src/vite/utils.js Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> * Update packages/kit/src/vite/utils.js Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> * Update packages/kit/test/apps/basics/.env Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> * Update packages/kit/test/apps/basics/.gitignore Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> * write_env in init, not update * lint * simplify valid identifier check, allow exports of reserved words * move types/ambient.d.ts to ambient.d.ts so it doesnt get nuked on update * Update documentation/docs/15-configuration.md Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> * remove env vars FAQ * feat: Testing for import analysis * Update packages/kit/types/ambient.d.ts * warn if env vars are invalid * oops * argh * Update packages/kit/scripts/extract-types.js Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> * explain why we strip the origin * Update packages/kit/types/ambient.d.ts Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> * temporarily disable * Revert "temporarily disable" This reverts commit c1a5d52fc86fd34ff0be2d2d641dfa66116c5d80. * tidy up * remove another unused import * Update packages/kit/src/vite/utils.js Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> * Update packages/kit/src/vite/utils.js Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> * Update packages/kit/src/vite/utils.js Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> * Update packages/kit/src/vite/utils.js Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> * Update packages/kit/src/core/sync/write_env.js Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> Co-authored-by: Rich Harris Co-authored-by: Rich Harris Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> --- .changeset/beige-gorillas-tell.md | 9 + .changeset/pretty-kings-care.md | 5 + documentation/docs/06-hooks.md | 3 - documentation/docs/15-configuration.md | 15 +- documentation/faq/60-env-vars.md | 9 - ...ackage-json.md => 60-read-package-json.md} | 0 .../adapter-cloudflare-workers/files/entry.js | 2 + packages/adapter-cloudflare/src/worker.js | 1 + packages/adapter-netlify/src/serverless.js | 4 + packages/adapter-node/src/handler.js | 1 + packages/adapter-vercel/files/serverless.js | 4 + .../templates/default/src/app.d.ts | 4 + .../templates/skeleton/src/app.d.ts | 2 + packages/kit/rollup.config.js | 4 + packages/kit/scripts/extract-types.js | 133 ++++++++------- .../special-types/$env+static+private.md | 7 + .../special-types/$env+static+public.md | 7 + packages/kit/scripts/special-types/$lib.md | 1 + packages/kit/src/cli.js | 5 +- packages/kit/src/core/config/index.spec.js | 3 + packages/kit/src/core/config/options.js | 4 + packages/kit/src/core/sync/sync.js | 26 ++- packages/kit/src/core/sync/utils.js | 53 ++++++ packages/kit/src/core/sync/write_env.js | 84 ++++++++++ packages/kit/src/core/sync/write_tsconfig.js | 21 +-- packages/kit/src/runtime/client/start.js | 2 + packages/kit/src/runtime/env-private.js | 7 + packages/kit/src/runtime/env-public.js | 7 + .../kit/src/runtime/env/dynamic/private.js | 1 + .../kit/src/runtime/env/dynamic/public.js | 1 + .../kit/src/runtime/server/page/render.js | 5 +- packages/kit/src/vite/build/build_server.js | 20 +++ packages/kit/src/vite/dev/index.js | 34 ++-- packages/kit/src/vite/index.js | 36 +++- packages/kit/src/vite/preview/index.js | 28 ++-- packages/kit/src/vite/utils.js | 140 +++++++++++++++- packages/kit/src/vite/utils.spec.js | 154 +++++++++++++++++- packages/kit/test/apps/basics/.env | 5 + packages/kit/test/apps/basics/.gitignore | 3 +- .../test/apps/basics/src/routes/env/index.js | 11 ++ .../apps/basics/src/routes/env/index.svelte | 16 ++ packages/kit/test/apps/basics/test/test.js | 20 +++ packages/kit/types/ambient.d.ts | 70 ++++++-- packages/kit/types/index.d.ts | 8 + packages/kit/types/internal.d.ts | 8 + .../src/lib/docs/server/modules.js | 6 +- turbo.json | 4 +- 47 files changed, 851 insertions(+), 142 deletions(-) create mode 100644 .changeset/beige-gorillas-tell.md create mode 100644 .changeset/pretty-kings-care.md delete mode 100644 documentation/faq/60-env-vars.md rename documentation/faq/{65-read-package-json.md => 60-read-package-json.md} (100%) create mode 100644 packages/kit/scripts/special-types/$env+static+private.md create mode 100644 packages/kit/scripts/special-types/$env+static+public.md create mode 100644 packages/kit/scripts/special-types/$lib.md create mode 100644 packages/kit/src/core/sync/write_env.js create mode 100644 packages/kit/src/runtime/env-private.js create mode 100644 packages/kit/src/runtime/env-public.js create mode 100644 packages/kit/src/runtime/env/dynamic/private.js create mode 100644 packages/kit/src/runtime/env/dynamic/public.js create mode 100644 packages/kit/test/apps/basics/.env create mode 100644 packages/kit/test/apps/basics/src/routes/env/index.js create mode 100644 packages/kit/test/apps/basics/src/routes/env/index.svelte diff --git a/.changeset/beige-gorillas-tell.md b/.changeset/beige-gorillas-tell.md new file mode 100644 index 000000000000..7912e6f4ea90 --- /dev/null +++ b/.changeset/beige-gorillas-tell.md @@ -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` diff --git a/.changeset/pretty-kings-care.md b/.changeset/pretty-kings-care.md new file mode 100644 index 000000000000..fc1fd37e5ce7 --- /dev/null +++ b/.changeset/pretty-kings-care.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +Add `$env/static/private`, `$env/static/public`, `$env/dynamic/private` and `$env/dynamic/public` modules diff --git a/documentation/docs/06-hooks.md b/documentation/docs/06-hooks.md index a6b28d5d28f5..f1769fb23051 100644 --- a/documentation/docs/06-hooks.md +++ b/documentation/docs/06-hooks.md @@ -38,9 +38,6 @@ declare namespace App { interface Locals { user: User; } - interface Platform {} - interface Session {} - interface Stuff {} } const getUserInformation: (cookie: string | null) => Promise; diff --git a/documentation/docs/15-configuration.md b/documentation/docs/15-configuration.md index c51d158e81b5..6bd606854ec2 100644 --- a/documentation/docs/15-configuration.md +++ b/documentation/docs/15-configuration.md @@ -29,7 +29,9 @@ const config = { // ... } }, - moduleExtensions: ['.js', '.ts'], + env: { + publicPrefix: 'PUBLIC_' + }, files: { assets: 'static', hooks: 'src/hooks', @@ -44,6 +46,7 @@ const config = { parameter: '_method', allowed: [] }, + moduleExtensions: ['.js', '.ts'], outDir: '.svelte-kit', package: { dir: 'package', @@ -157,9 +160,11 @@ When pages are prerendered, the CSP header is added via a `` ta > Note that most [Svelte transitions](https://svelte.dev/tutorial/transition) work by creating an inline `