Vite: Support environment variables in viteFinal define config #19905
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Issue: Ref: https://github.com/jcollicoat/vite-storybook/issues/1
What I did
@jcollicoat discovered that setting environment variables with vite's
define
option inside ofviteFinal
had no effect. I realized this was because we were setting a define rule forimport.meta.env
(setting it to a stringified object of the env variables we know about before handling viteFinal), so instead of the full string replacement ofimport.meta.env.STORYBOOK_FOO
, vite was findingimport.meta.env
, turning it into an object, then trying to read theFOO
property, which isn't there.This is solved by moving the call to
stringifyProcessEnvs
after we getviteFinal
(which is actually what we do in 6.5: https://github.com/storybookjs/builder-vite/blob/5f3da981f9ac89f871afaacc052f1551c4fbdde9/packages/builder-vite/build.ts#L31-L34). Note that destructuring env vars is not supported by the webpack builder.I also removed the fallback for
XSTORYBOOK_EXAMPLE_APP
which was added back when we used that variable in other places, but we no longer do, so there's no need to set it.How to test
Should I add a sandbox story to test environment variables, similar to what we have in 6.5? (https://github.com/storybookjs/builder-vite/blob/5f3da981f9ac89f871afaacc052f1551c4fbdde9/examples/react/stories/EnvironmentVariables.stories.jsx)