Adding array of values in env
My extension will run on several sites e.g. amazon, ebay, alibaba. Each site has some specific configurations like the website domain and the specific div classes that I use to locate products on the site. Currently I have these configurations in their own .env.amazon, .env.ebay, .env.alibaba and create a build for each ...
But we now want one build that will work on all three sites and any site we may add in the future (we'll be adding sites often). To my understanding, env files only take string values so my only option is to concatenate all the values into a pipe-separated string like so
And then modify my existing code to parse the values
Is this the only option? I'm worried about having one giant string bc
1. maintenance/readability - we'll be adding a lot of customers soon
2. collisions of div classes, for instance if ".product-image" is the appropriate class for amazon but also exists in ebay it could mess things up
I would like to keep them visually separated like in the env above if possible. I'm sure there are JSON based env-type files but does Plasmo work with those? I definitely want to keep the configuration in a separate file (not hardcoded) so that if we sign another customer onboarding them is as easy as adding to the config file
Thanks!
22 Replies
@Pallas
check out https://github.com/PlasmoHQ/plasmo/pull/757 .. it's in 0.83.0 .. you can use this in any context that
"$WHATEVER"
vars are processed by plasmo (like in a content script config)GitHub
feat: Introduces JSON type to non-public env variables by sleekslus...
Details
Note: I'm open to conversation on this idea, so please let me know your thoughts and whether something like this would be useful. Also, would be curious to know if any alternative solut...
Thanks @filthytone , it's not working for me. It's being read as-is, you can see the "json([ ... " wrapper. I checked that I'm in 0.83.0.
Here's my .env.development. Anything I'm doing wrong?
`
Gave +1 Rep to @filthytone
@hgil has reached level 4. GG!
Don’t use the plasmo_public prefix
Ah. Ok wasn't sure what was meant by non-public in the Github Issue. Thx let me try!
Works! Thx @filthytone . One note though, I directly copied the example from the Github Issue which had its own square brackets [], so at compilation I got an error "expected string" (instead it got another list []). I removed the brackets and all good.
Should i have based it off some documentation?
Gave +1 Rep to @filthytone
Yea has to be a string value so removing brackets was correct
The final result will be an array as you see
None of this is documented .. I had the exact same need but in a manifest context , so I implemented it
There’s technically a typescript error on the config object, but that easily fixed too
Oh you're sleekslush! Sweet man thanks a ton for implementing!!
Gave +1 Rep to @filthytone
Yeah figured about the documentation just wondering
no problem! and yea, lol same guy
you can fix the typescript error like this
and then rejoice w/ the typescript gods
Ok took me a bit to figure out but first I celebrated early when I said it worked. I actually had an empty list in matches. The fix was the code you just shared (I thought you meant there was a benign error). But in that code I I had to then add back the square brackets because previously i had
So now my .env matches what you had in the Github Issue
So now one more question for you before I spend too much time trying to get this to work:
I also need this to dynamically set class names in my getOverlayAnchorList()
Is that possible? Idk if I need to use process.env in that case
@filthytone also what about in a content.css lol
Hm actually just realized I need a map
It depends .. if normally you can use $WHATEVER and plasmo replaces it with some value, you can use the json directive .. it only works on non-public variables (not prefixed with PLASMO_PUBLIC)
And yeah you can use anything that JSON.parse will succeed with
So arrays and objects both work
@filthytone has reached level 4. GG!
There are some things that plasmo will inject vars that work with both private and public variable names, but there are other things that you can only really access the public prefixed ones
Ok yeah I think this is the latter case :/ When I run it it logs "$SHOPADVISOR_ANCHOR_CLASSES is not a valid query selector"
And those don’t have json support because they generally are accessed from process.env , which forces EVERYTHING to a string
That’s something nodejs does so that’s why only the private var names get the magical json treatment
Anywhere else you could use a json value for the variable , but you have to JSON.parse it yourself in the code that accesses it
Gotcha ok. Yeah I have some ideas of how to do it. Maybe just fetch a config on start and store in memory
Thanks a ton for your help with this!!
Look at data-env in plasmo .. I use that sometimes for runtime JSON data