mirror of
https://github.com/beijaflor-io/ghcjs-commonjs.git
synced 2024-09-11 16:25:25 +03:00
Seamless calls of Haskell code from JavaScript modules
example-react-serverside-rendering | ||
examples | ||
ghcjs-commonjs | ||
ghcjs-loader | ||
ghcjs-register | ||
ghcjs-require | ||
old-examples | ||
.gitignore | ||
README.md |
ghcjs-commonjs
ghcjs-commonjs is a work-in-progress collection of little hacks to make GHCJS and CommonJS integration less troublesome.
By that we mean:
- Easy implementation of calling Haskell code from JavaScript code
- Easy implementation of calling JavaScript code from Haskell
The first point is where the most friction is, since the GHCJS FFI is pretty stellar.
We provide the following tools:
ghcjs-require
- Calling Haskell from Node.js and CommonJSghcjs-register
- require('./MyHaskellModule.hs')ghcjs-loader
- A webpack loader for GHCJS
ghcjs-require
Utilities for loading GHCJS .jsexe
s from CommonJS land.
Callling Haskell from JavaScript
This is main.js
:
const ghcjsRequire = require('ghcjs-require');
const Main = ghcjsRequire('./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.jsexe
:
import Control.Concurrency (threadDelay)
import GHCJS.CommonJS (defaultMain, export)
someFunction = do
putStrLn "Waiting for a second"
threadDelay (1000 * 1000)
putStrLn "Done!"
main =
exportMain [("someFunction", 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.