Skip to content

feat (cli, unplugin): support custom namespace key to write and read env variables#2268

Draft
nikilok wants to merge 3 commits intoruntime-env:mainfrom
nikilok:nikilok/add-accessor-unplugin-cli
Draft

feat (cli, unplugin): support custom namespace key to write and read env variables#2268
nikilok wants to merge 3 commits intoruntime-env:mainfrom
nikilok:nikilok/add-accessor-unplugin-cli

Conversation

@nikilok
Copy link

@nikilok nikilok commented Jan 6, 2026

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

import-meta-env -x .env.example --prepend remoteEntry.js -k my-custom-key

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.

 ImportMetaEnvPlugin.vite({
      example: ".env.example",
      env: ".env",
      accessorKey: "my-custom-key", // <-- This new custom key was added in.
    }) 

…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.
@nikilok nikilok marked this pull request as ready for review January 6, 2026 12:28
@nikilok nikilok changed the title feat: Add custom namespace key to allow env variable injection for multiple app feat (cli, unplugin): Add custom namespace key to allow env variable injection for multiple app Jan 6, 2026
@nikilok nikilok changed the title feat (cli, unplugin): Add custom namespace key to allow env variable injection for multiple app feat (cli, unplugin): support custom namespace key to write and read env variables Jan 6, 2026
@nikilok nikilok marked this pull request as draft January 6, 2026 12:32
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.

1 participant