I have been trying to do write a function that works both in node and the browser, but in each case it has to load different external libraries, to finally do the same processing (in the end the output would be the same.)
How would I go about doing it? This is a more in-detail explanation:
- Write a function that accepts an image
- if the environment is node (currently user sets an argument to "node" or "browser") it loads some stuff
- if the env is browser, it does some other stuff
I want that either a NodeJS user, or a Browser user could load it, and the function would run the right code.
I know it may be a bit vague but I can add any information. A very simple example is below:
export async function greeting(browserOrNode){
if(browserOrNode==="browser"){
//run X code supported in browser
} else{
const { myHelper } = await import("./helpers")
// run code supported in node
}
}
Is there any other way I should be thinking on doing it?
Another option I considered was to export two functions one myFnForBrowser
another one myFnForNode
but I am not sure whether that is professional.