Skip to content
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

Merged
merged 7 commits into from
Jul 8, 2021
Merged

Conversation

jthegedus
Copy link
Contributor

@jthegedus jthegedus commented Jul 1, 2021

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpx changeset and following the prompts

Summary

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 in config.kit.vite.mode
  • svelte-kit dev defaults this val to development
  • svelte-kit build defaults this val to production

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:

import { dev, mode } from '$app/env';

if (dev && mode !== 'development') {
	throw Error('mode should equal development when dev is true');
}

I am unclear on how the test suite operates.

@changeset-bot
Copy link

changeset-bot bot commented Jul 1, 2021

🦋 Changeset detected

Latest commit: 168ea2b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Patch

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.
Copy link
Member

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

Copy link
Member

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

Copy link
Contributor Author

@jthegedus jthegedus Jul 2, 2021

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

Copy link
Member

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

Copy link
Contributor Author

@jthegedus jthegedus Jul 4, 2021

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.

@Rich-Harris
Copy link
Member

Thanks — could you add a changeset and update the docs please? https://github.com/sveltejs/kit/blob/master/documentation/docs/05-modules.md

@benmccann benmccann changed the title fix(kit): expose Vite.js mode from $app/env [fix] expose Vite.js mode from $app/env Jul 3, 2021
@benmccann benmccann merged commit 1ec368a into sveltejs:master Jul 8, 2021
@jthegedus jthegedus deleted the patch-1 branch July 9, 2021 00:40
sidharthv96 added a commit to sidharthv96/kit that referenced this pull request Jul 11, 2021
* '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)
  ...
@Rich-Harris Rich-Harris mentioned this pull request Jul 19, 2022
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants