tshm
5/6/2016 - 3:40 PM

allow running Elm compiled code from nodejs

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 )