'Omit code as if it didn't exist if environment variable is set?
I'm porting a NextJS web app to a web/mobile app with capacitorjs, and I can't use getServerSideProps for mobile.
I was wondering if I can use the same files, but omit code based on a environment variable. Something like this for example: (pseudocode)
APP_TYPE=mobile && npm run build
#IF process.env.APP_TYPE==mobile THEN
const backendData = await fetch(url);
#ELSE
export async function getServerSideProps(context: { params?: { username: string } }) {
const backendData = await fetch(url);
}
#ENDIF
Then if APP_TYPE==mobile the build would run as if the getServerSideProps code didn't exist (and the opposite if APP_TYPE wasn't mobile).
Are there any methods to do this?
Solution 1:[1]
I found two interesting packages which do this Preprocessor.js and MetaScript.
Example (MetaScript):
// #ifdef FULL
console.log("Including extension");
// #include "path/to/extension.js"
// #else
console.log("Not including extension");
// #endif
Although I found it wasn't the best fit for my project I wasn't sure how to implement it in a good way for my project, so I ended up separating files into .shared.tsx, .web.tsx, and .mobile.tsx instead.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
