-
-
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
[fix] expose Vite.js mode
from $app/env
#1789
Conversation
🦋 Changeset detectedLatest commit: 168ea2b The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
@@ -15,6 +15,10 @@ declare module '$app/env' { | |||
* `true` when prerendering, `false` otherwise. | |||
*/ | |||
export const prerendering: boolean; | |||
/** | |||
* The Vite.js mode the app is running in. Configure in `config.kit.vite.mode`. By default, `svelte-kit dev` runs in `development` mode and `svelte-kit build` runs in `production` mode. |
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.
it's not quite clear from these docs and the docs above for dev
how the two are different from each other
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.
It is technically redundant, but only because we'd be reflecting Vite's behaviour and Vite's behaviour is redundant https://vitejs.dev/guide/env-and-mode.html#env-variables. In practice I think it's beneficial to have a boolean dev
value, and if people have found that it's valuable to expose mode
then it makes sense to have both i think
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.
@benmccann Yes, I should expand on these docs. Mode is used to infer .env.[mode]
dotenv files to load. As these values are statically replaced during a build, we can utilise mode
to load the config for our target env.
While this is similar to dev
I'd argue not redundant. As I raised in #1613 (comment) there are many instances for building and running a SvelteKit app and in each instance I may want to use different config and conditionally execute code when dev=false
in prod
builds.
This is also why #1258 is useful as currently setting the mode
in svelte.config.js
requires computing the env and is less ergonomic than a CLI flag. (I currently do export MODE="" && svelte-kit build
just to then vite: {mode: process.env.MODE}
)
Example use cases for mode
:
command | purpose | envs |
---|---|---|
run dev server against local resources | svelte-kit dev |
NODE_ENV=development |
mode=development (default) |
||
env.development |
||
dev=true |
||
run production against prod resources | svelte-kit build |
NODE_ENV=production |
mode=production (default) |
||
env.production |
||
dev=false |
||
run production build locally against local resources | svelte-kit preview |
NODE_ENV=production |
mode=production (default) ❌ |
||
We want to load env.development here but default VITE_MODE=production was used during build |
env.production ❌ |
|
not useful to resolve issue | dev=false |
|
build & deploy to staging against staging resources | svelte-kit build |
mode=production |
VITE_MODE=production (default) ❌ |
||
We want to load env.staging here but default VITE_MODE=production was used during build |
env.production ❌ |
|
not useful to resolve issue | dev=false |
|
build & deploy to production against production resources | svelte-kit build |
NODE_ENV=production |
mode=production (default) :check: |
||
env.production :check: |
||
dev=false |
||
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.
That's a very long answer I don't quite know how to digest 😄 Basically my question is are these representing different values. E.g. one is NODE_ENV
and one is VITE_MODE
? We should document what underlying flag these each represent
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.
Yes. One is essentially NODE_ENV
, one is mode
and specific to Vite.js (I don't think it can be set via VITE_MODE env var). As far as I understand dev
cannot represent all the states that mode
can, and therefore is not redundant.
Thanks — could you add a changeset and update the docs please? https://github.com/sveltejs/kit/blob/master/documentation/docs/05-modules.md |
mode
from $app/env
mode
from $app/env
* 'master' of github.com:sidharthv96/kit: (1114 commits) Version Packages (next) (sveltejs#1858) Bump vite-plugin-svelte to 1.0.0-next.12 (sveltejs#1869) [fix] preserve user defined config and files on `svelte-kit package` (sveltejs#1735) [fix] handle undefined body on endpoint output (sveltejs#1808) [fix] copy essentials files from root on packaging (sveltejs#1747) [docs] sort config alphabetically (sveltejs#1867) add config.kit.package.emitTypes option (sveltejs#1852) [fix] add $lib alias to js/tsconfig (sveltejs#1860) Pass along custom properties added to Error (sveltejs#1821) Version Packages (next) (sveltejs#1840) Improve grammar in packages FAQ Docs for writing an adapter (sveltejs#1846) Additional documentation around pnpx changeset usage [feat] expose Vite.js `mode` from `$app/env` (sveltejs#1789) Service worker files exclusion support (sveltejs#1645) chore: Enable `vite.server.fs.strict` internally by default (sveltejs#1842) Test with the latest version of Svelte (sveltejs#1848) [docs] don't need to run pnpm install twice Improve HN example docs [fix] correct `ReadOnlyFormData` generator implementation (sveltejs#1837) ...
Before submitting the PR, please make sure you do the following
Tests
pnpm test
and lint the project withpnpm lint
andpnpm check
Changesets
pnpx changeset
and following the promptsSummary
dev
utility is great, but projects often have more than two environments (dev
,prod
). Exposing the Vite.js Mode value would simplify the code for these multi-envs projects.mode
can be set inconfig.kit.vite.mode
svelte-kit dev
defaults this val todevelopment
svelte-kit build
defaults this val toproduction
Related
This is related to #1258 which asks for the CLI flag
--mode
to be exposed.Other
Not sure how to write a test for this, maybe:
I am unclear on how the test suite operates.