You cannot import conditionally, but you can do the opposite: export something conditionally. It depends on your use case, so this may not be for you.
You can do:
api.js
import mockAPI from './mockAPI' import realAPI from './realAPI' const exportedAPI = shouldUseMock ? mockAPI : realAPI export default exportedAPI
apiConsumer.js
import API from './api' ...
I use this to mock analytics libraries such as mixpanel, etc., because currently I cannot have multiple builds or our interface. Not the most elegant, but it works. I just have a few ifs here and there depending on the environment, because in the case of mixpanel it needs initialization.
Kev Apr 24 '17 at 15:22 2017-04-24 15:22
source share