Search code examples
haskellghcjs

Compiling Haskell module to Javascript using ghcjs


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.


Solution

  • 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.