Created
May 6, 2016 15:40
-
-
Save tshm/1ea30b12ed183e3d5d1f77ffdc6da8a4 to your computer and use it in GitHub Desktop.
allow running Elm compiled code from nodejs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** 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