Is it possible to compile a Haskell module to JS using ghcjs
? I played with it, but it seems it requires main
function to be defined before it would generate JS. A toy code below:
module Test where
add :: Int -> Int -> Int
add x y = x + y
I can't figure out how to compile it to JS using ghcjs
. It will compile only .hi and .o files if main function is not defined. If I can figure out a way to compile it to JS (with appropriate Z-encoded
function name, and a corresponding JS object, like what we have in FFI
for C), then I could use more powerful Haskell libraries like unordered-containers
and vector
to implement some algorithm, compile to JS, and use in Elm
through JS FFI.
Fay
seems to be capable of doing it. I will like to investigate it for ghcjs
first.
You can use GHCJS to compile Haskell code to JavaScript. It is a fork of GHC and supports pretty much everything GHC supports including TemplateHaskell. Most libraries on hackage work just fine as long as they do not have dependencies on C libraries. The compile times are slow and the resulting .
GHCJS is a Haskell to JavaScript compiler that uses the GHC API.
I just asked the ghcjs author (luite) in IRC, and he said, "there is no good way yet to directly export a library."
He does seem very interested in supporting that capability, though, so I'm hoping, and optimistic that this will change in the near future. The place to watch this issue is the ticket requesting this feature on github.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With