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: env refactor for Vite wrap-up [LIBS-690] #889

Merged
merged 8 commits into from
Oct 30, 2024
Merged

Conversation

KaiVandivier
Copy link
Contributor

@KaiVandivier KaiVandivier commented Oct 28, 2024

Part of LIBS-690

process.env vs. import.meta.env

Vite adds environment variables to the import.meta.env namespace by default. My plan was to make this the standard for the platform, too, because I thought this was a standard format that goes along with ES Modules. It turns out that it’s not standard, actually; it’s a pattern specific to Vite. Other build tools tend to use process.env as the default.

I've noticed that there are some other tools and packages out there that depend on configuration variables that are set on process.env, so dropping support for it entirely in favor of import.meta.env may be a bit weird.

This makes me think we should continue adding env vars to process.env, since it’s a little more generic, instead of migrating entirely to import.meta.env as the main env var namespace for the platform.

We can still deprecate the REACT_APP_-prefixed variable names, i.e. process.env.REACT_APP_DHIS2_APP_VERSION => process.env.DHIS2_APP_VERSION. We can handle it in the same way we planned to handle moving to import.meta.env i.e., have both variable names available in v12, then remove the REACT_APP_ versions in v13.

If we do, I’m not sure how much attention to give import.meta.env — Right now, Vite is configured to filter the env for values starting with “DHIS2_” and add them to import.meta.env. It seems that only values on import.meta.env can be used in HTML files, like %DHIS2_APP_NAME% in index.html. I propose we kinda keep import.meta.env as-is; some people may want to use it, and it doesn't really hurt to add the DHIS2_ prefixed env vars onto it.

Other env vars

I made some other changes to make the env var objects more concise:

  1. Remove PWA default variables, and instead handle undefined values
  2. Remove variables with undefined values from the env

I tested these out by starting and building both the simple-app and pwa-app examples to make sure they work with and without env vars. I also inspected the SW's caching behavior in the PWA app, and it looks good.

Examples

Before:

env-pwa-before-console env-pwa-before-cli

After:

env-pwa-after-console env-change-after-cli

@KaiVandivier KaiVandivier requested a review from a team October 28, 2024 21:08
Copy link
Member

@amcgee amcgee left a comment

Choose a reason for hiding this comment

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

Code LGTM

@KaiVandivier KaiVandivier merged commit 84da4e6 into alpha Oct 30, 2024
10 checks passed
@KaiVandivier KaiVandivier deleted the env-refactor branch October 30, 2024 09:32
dhis2-bot added a commit that referenced this pull request Oct 30, 2024
# [12.0.0-alpha.21](v12.0.0-alpha.20...v12.0.0-alpha.21) (2024-10-30)

### Bug Fixes

* env refactor for Vite wrap-up [LIBS-690] ([#889](#889)) ([84da4e6](84da4e6))
@dhis2-bot
Copy link
Contributor

🎉 This PR is included in version 12.0.0-alpha.21 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants