feat (cli, unplugin): support custom namespace key to write and read env variables
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.
})