feat (cli, unplugin): support custom namespace key to write and read env variables#2268
Draft
nikilok wants to merge 3 commits intoruntime-env:mainfrom
Draft
feat (cli, unplugin): support custom namespace key to write and read env variables#2268nikilok wants to merge 3 commits intoruntime-env:mainfrom
nikilok wants to merge 3 commits intoruntime-env:mainfrom
Conversation
…bles This feature makes it easy when in a module federation setup, each module federated app can read from a custom acessor instead of only reading it from globalThis.import_meta_env. This way you can have have multiple sets of enviorment variables on the base page, that each app loading in can read from.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The current import-meta-env/cli package can only modify an index.html with a common globalThis.import-meta-env key.
However when using a micro frontend architecture based on module federation, the host app could inject environment variables via the index.html modification, and the federated apps can get their own environment variables with a custom namespace injected into the federated apps JS file. By creating custom accessor keys we can have multiple sets of environment variables load on the host app, and each federated app can read of the correct keys.
The core idea behind this PR was to add support to the cli package to target any file besides index.html. This way I can target an existing remoteEntry.js and pre-pend to the top of the file a globalThis.[custom-key]={...}. Hence at runtime I can now call
Now when the remoteEntry is loaded on the host application, globalThis.my-custom-key will exist with all the keys.
Then in the unplugin package I can use that my-custom-key to read the environment variables and have it injected at runtime.