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

Support missing import.meta.env #413

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

princed
Copy link

@princed princed commented Jun 20, 2022

Currently vite storybook builder breaks when any env var used in code via import.meta.env is missing while building storybooks.

Apparently that happens because Rollup does direct inline replacements and turns code like

const getMyVar = () => import.meta.env.VITE_MISSING_VAR === 'true'

into something like

const getMyVar = () => {"BASE_URL":"/","MODE":"production","DEV":false,"PROD":true}.VITE_MISSING_VAR === "true";

which is syntactically incorrect.
See my reproduction repo for an example — https://github.com/princed/vite-sb-meta-env-repro.

To mitigate this vite itself adds a replacement from import.meta.env. to ({}). to catch missing direct replacements and produce correct code. I opted for identical solution here.

A similar workaround is also possible on the user side:

viteFinal(config) {
   config.define = {
     'import.meta.env.': `({}).`,
   };
   return config; 
}

@IanVS
Copy link
Member

IanVS commented Jun 21, 2022

Thanks for this PR. It seems like maybe the vite solution still has a kink to work out? vitejs/vite#8663. Maybe we should hold off until that issue is solved, before adding this.

@@ -22,7 +22,7 @@ export const allowedEnvPrefix = ['VITE_', 'STORYBOOK_'];
* Customized version of stringifyProcessEnvs from @storybook/core-common which
* uses import.meta.env instead of process.env and checks for allowed variables.
*/
export function stringifyProcessEnvs(raw: EnvsRaw, envPrefix: UserConfig['envPrefix']) {
export function stringifyProcessEnvs(raw: EnvsRaw, envPrefix: UserConfig['envPrefix'], isBuild: boolean) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what the preferred way to do this is, but happy to change it if needed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use a mode of either production or development, to avoid a boolean trap.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like mode would be confusing here, because it's possible to build in development mode and serve in production. And vite itself uses command to derive this information:
https://github.com/vitejs/vite/blob/908c9e4cdd2cceb0f01495e38066ffe33c21ddb8/packages/vite/src/node/plugins/define.ts#L12

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you be okay with command: 'build' | 'serve' as an argument?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, thanks.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

@princed
Copy link
Author

princed commented Jun 22, 2022

It seems like maybe the vite solution still has a kink to work out? vitejs/vite#8663. Maybe we should hold off until that issue is solved, before adding this.

Happy to wait and see whether Vite team finds another approach to the problem, but it seems unlikely though.
We also could just merge this for now, and then handle changes (if any) when adding Vite 3 support (which will surely require some dedicated effort).

@IanVS
Copy link
Member

IanVS commented Jul 15, 2022

Vite 3 has been released, and vitejs/vite#8663 is not yet solved. I'm happy to proceed with this if the underlying problem is still an issue in Vite 3 (would you mind confirming?).

@princed
Copy link
Author

princed commented Jul 17, 2022

Don't mind at all, but will only be able to do that in a week.

@princed
Copy link
Author

princed commented Jul 24, 2022

So with Vite 3 and vitejs/vite#8663 not fixed, even this PR doesn't fix building storybooks ¯\_(ツ)_/¯

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.

2 participants