Seamless calls of Haskell code from JavaScript modules
Go to file
Pedro Tacla Yamada 836ab33414 Update README.md
2016-06-20 14:24:25 -03:00
example-react-serverside-rendering Get hello world to working state 2016-06-18 12:38:10 -03:00
examples Update examples 2016-06-19 22:28:42 -03:00
ghcjs-commonjs Add way to build & run examples in one command 2016-06-19 22:07:21 -03:00
ghcjs-loader Update the codebase 2016-06-18 16:09:42 -03:00
ghcjs-register Patches 2016-06-16 20:12:17 -03:00
ghcjs-require Workspace update 2016-06-18 15:19:55 -03:00
old-examples Move examples 2016-06-16 22:43:24 -03:00
README Update examples 2016-06-19 22:28:42 -03:00
.gitignore Add working webpack example 2016-06-18 16:06:12 -03:00
LICENSE Update the LICENSE 2016-06-18 15:20:28 -03:00
Makefile Update examples 2016-06-19 22:28:42 -03:00
README.md Update README.md 2016-06-20 14:24:25 -03:00

ghcjs-commonjs

ghcjs-commonjs is a work-in-progress collection of little hacks to make GHCJS generated JavaScript and CommonJS integration less troublesome.

This hasn't been tested in production yet, so try the examples and report any issues that you have

We provide the following tools:

  • ghcjs-require - Calling Haskell from Node.js and CommonJS
  • ghcjs-commonjs - Exposing Haskell to be called from CommonJS
  • ghcjs-register - require('./MyHaskellModule.hs')
  • ghcjs-loader - A webpack loader for GHCJS

ghcjs-require

Utilities for loading GHCJS .jsexes from CommonJS land.

Callling Haskell from JavaScript

This is main.js:

const ghcjsRequire = require('ghcjs-require');
const Main = ghcjsRequire(module, './Main.jsexe');
// ^^ This is a function that boots the Haskell RTS
Main(({wrapped}) => { // <- This callback is executed after the RTS is loaded
  wrapped.someFunction().then(() => console.log('someFunction is over'));
  // ^^ This function was generated, it'll call Haskell code asynchronously and
  //    return a promise to the result (the result needs to be a JavaScript
  //    value)
});

This is Main.hs:

import Control.Concurrent (threadDelay)
import qualified GHCJS.CommonJS as CJS (exportMain, pack)
someFunction = do
    putStrLn "Waiting for a second"
    threadDelay (1000 * 1000)
    putStrLn "Done!"
main = CJS.exportMain [ "someFunction" `CJS.exports` someFunction
                      ]

ghcjs-register

On the likes of coffee-script/register or babel-register:

require('ghcjs-register');
const Main = require('./Main.hs');

ghcjs-loader

This is a webpack loader for GHCJS. See the examples.

License

All code under this repository is licensed under the MIT license.