Skip to content

Instantly share code, notes, and snippets.

@tshm
Created May 6, 2016 15:40
Show Gist options
  • Save tshm/1ea30b12ed183e3d5d1f77ffdc6da8a4 to your computer and use it in GitHub Desktop.
Save tshm/1ea30b12ed183e3d5d1f77ffdc6da8a4 to your computer and use it in GitHub Desktop.
allow running Elm compiled code from nodejs
/** allow to use Elm from nodejs...
*/
const vm = require('vm')
const fs = require('fs')
function log( o ) {
console.log( o )
}
/** loads Elm compiled javascript
* and returns Elm object
*/
function loadElm( path ) {
log('exports called.')
const data = fs.readFileSync( path )
const context = { console, setInterval, setTimeout, setImmediate }
vm.runInNewContext( data, context, path )
return context.Elm
}
/** main
*/
const Elm = loadElm('./elm.js')
const app = Elm.worker( Elm.Main, {})
app.ports.time.subscribe(log)
log( Elm )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment