There are two ways to approach this:
Option 1: Import both and use a conditional statement
Combine the code for projectA and projectB into one file with a conditional statement.
import initProjectA
import initProjectB
if (condition) {
initProjectA()
} else {
initProjectB()
}
When compiled, it will look like this:
function initProjectA() {
}
function initProjectB() {
}
if (condition) {
//
}
Option 2: Use dynamic imports
With dynamic imports, three separate files will be created: projectA, projectB, and conditional.
import(module).then(importedModule => {
importedModule.default() // assuming you did `export default`
})
The conditional file will only load and execute one of them based on the condition.
- Check which project to download
- Download the file and execute it
To use dynamic imports, you may need to adjust some configurations. Search for "webpack dynamic imports" for more information.