I am working with a browser extension project and want to have a different URL used in background.js during development time and build time. I want to do this without having to remember to change the code between development and build. With a server project I'd simply use dotenv/environment variables but that's not available to extensions which effectively run client side.
In background.js I have a fetch using this api_base_url (we develop the API too);
...
const api_base_url = 'http://127.0.0.1:8000/v1/'
...
Before I build (web-ext build) I have to manually that to something like;
...
const api_base_url = 'http://a.domain.com/v1/'
...
Ideally it would be something like;
...
const api_base_url = ENV['API_BASE_URL']
...
and I'd have a .env in local dev of;
API_BASE_URL='http://127.0.0.1:8000/v1/'
and .env.production (or .env.build) of;
API_BASE_URL='http://a.domain.com/v1/'
This is also a problem in manifest.json where I need to whitelist the different URLs in permissions e.g.
"permissions": [
"storage",
"tabs",
"https://a.domain.com/v1/*",
"http://127.0.0.1:8000/v1/*"
]
This isn't a run-time per-user option so browser.storage and options.js isn't what we're looking for.
I have figured this out but the basic answer is to add webpack and use
dotenv-webpackfor entry files likebackground.jsandcopy-webpack-pluginfor non-entry files likemanifest.json. These plugins will replace string occurrences ofprocess.env.YOUR_VARIABLE_NAMEwith the value fromprocess.env.YOUR_VARIABLE_NAME.This literally happens and it took me a few tries to understand it.
Here is the webpack config;
I have made a complete working example here; https://github.com/paulmwatson/web-ext-environments