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

respect --mode, and remove server, prod and mode from $app/env #5602

Merged
merged 11 commits into from
Jul 20, 2022

Conversation

Rich-Harris
Copy link
Member

@Rich-Harris Rich-Harris commented Jul 18, 2022

This mostly fixes #5573, but not quite. It fixes mode but dev/prod is broken — for some reason, with --mode custom, import.meta.env.DEV is true even for the built app. I haven't been able to replicate this in a plain Vite app, so it seems we're doing something iffy.

Closes #5573

The conflation of mode and command is unfortunate: import.meta.env.DEV should be true if command === 'serve' and vice versa. It should have nothing to do with mode. I'm not sure if that's part of what's happening here.

Slightly unrelated, but: looking at this stuff more closely, I'm inclined to remove mode from $app/env — it's a Vite concept, not a SvelteKit one, and should probably be expressed as import.meta.env.MODE.

Please don't delete this checklist! 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 pnpm changeset and following the prompts. All changesets should be patch until SvelteKit 1.0

@changeset-bot
Copy link

changeset-bot bot commented Jul 18, 2022

🦋 Changeset detected

Latest commit: aa2faf3

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

@Rich-Harris
Copy link
Member Author

I think I found a bug in Vite: vitejs/vite#9203. Might be easiest to just use define for the values inside $app/env, pending a fix

@bluwy
Copy link
Member

bluwy commented Jul 19, 2022

I think I found a bug in Vite: vitejs/vite#9203. Might be easiest to just use define for the values inside $app/env, pending a fix

Given this is intentional in Vite, perhaps we want to revert the patch in this PR to stay consistent.

@Rich-Harris
Copy link
Member Author

I'm surprised it's intentional to be honest — to my mind, DEV = command === 'serve' and PROD = command === 'build'. Any other distinction just isn't particularly useful, so I would prefer to use define as in this PR.

It does call the naming into question though. We currently have dev and prod; prod was added by @MathiasWP recently in #5251. To be completely honest I was uneasy about the change then, and I should have listened to my gut — dev is unambiguous since it aligns with vite dev, but prod is ambiguous, since we use it to mean vite build but it sounds like it means mode === 'production'.

Frankly I think we should remove mode, prod and server. They're redundant, and mode and prod introduce confusion.

image

@Rich-Harris Rich-Harris mentioned this pull request Jul 19, 2022
5 tasks
@Rich-Harris
Copy link
Member Author

Opened a PR to this branch that removes those redundant values: #5614

* remove redundant env

* changeset

* add meatier description

* huh, cant use those in comments, they get replaced dangerously
@Rich-Harris Rich-Harris merged commit c503c71 into master Jul 20, 2022
@Rich-Harris Rich-Harris deleted the gh-5573 branch July 20, 2022 00:54
@Rich-Harris Rich-Harris changed the title respect --mode respect --mode, and remove server, prod and mode from $app/env Jul 20, 2022
@bluwy
Copy link
Member

bluwy commented Jul 20, 2022

I'm surprised it's intentional to be honest — to my mind, DEV = command === 'serve' and PROD = command === 'build'. Any other distinction just isn't particularly useful, so I would prefer to use define as in this PR.

Late to the party, but this is because some cases you might want to have dev behavior builds. For example, a staging site where you get helpful dev errors locally before deploying to prod where you want to have errors captured via Sentry instead.

With this change, we're blocking this usecase unless we use import.meta.env.MODE to compare, but now we have to compare on the custom mode names instead, which isn't great if you have multiple modes lying around, e.g. multiple stagings.

I think we should still align with Vite here even if it may sound confusing. Having SK and Vite contradicting would be more confusing for users, espeically that we're a Vite plugin, users are reading both SK and Vite docs.

@Mlocik97
Copy link
Contributor

Mlocik97 commented Jul 20, 2022

I never used server, I had !browser (negation of browser is server, as per implementation that Mathias did), So it was just why to have both browser and server values when they are just negation of each other? I found it redundant too.

But for mode, that is not just boolean and can have different values, I'm not sure with this PR. I agree with bluwy in this.

@benmccann
Copy link
Member

I think there are three cases here:

  • running dev server
  • running in prod
  • running elsewhere like QA / staging

The question is whether QA / staging should be categorized as dev or prod. Vite says dev and this PR says prod. It's quite ambiguous. It's really neither and the problem is we're trying to jam three values into two. I wonder if the solution isn't instead to define DEV and PROD as checking their respective modes don't compute the value of one based off the other but only check if that's the mode that's set.

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.

Non-production builds still builds production SSR bundles
4 participants