I'm building a small haste project where I want to use Elasticsearch. However, bloodhound which seems like the library to go for for elasticsearch in haskell depends indirectly on template-haskell - which isn't supported by haste. Now, I don't need to call elastic from the client, so I don't need bloodhound in haste, but I need to be able to call it from within the same code base as haste is built to use the same code for server and client side. I guess I somehow could have separate client and server side implementations but I really like the haste way.
How can I have calls to dependencies that only exist on the server side in haste?
Preprocessor can be used for this purpose. Haste defines __HASTE__
macro so it should be enough to wrap your code in conditional statement:
{-# LANGUAGE CPP #-}
main = do
#ifdef __HASTE__
print "haste!"
#endif
#ifndef __HASTE__
print "not haste!"
#endif
print "everybody"
Don’t forget to enable C preprocessor extension using {-# LANGUAGE CPP #-}
pragma.
You can also achieve similar effect in your ‘.cabal’ file:
Build-Depends:
bytestring >= 0.9.2.1
if flag(haste-inst)
Build-Depends:
base == 4.6.0.1,
array == 0.4.0.1
else
Build-Depends:
base,
array,
random,
websockets >= 0.8
(source https://github.com/valderman/haste-compiler/blob/0.4/libraries/haste-lib/haste-lib.cabal#L63)
Note that the haste-inst
flag has been renamed to haste-cabal
in the latest development version of Haste.