Skip to content

Instantly share code, notes, and snippets.

@also
Last active December 26, 2015 17:48
Show Gist options
  • Save also/7189192 to your computer and use it in GitHub Desktop.
Save also/7189192 to your computer and use it in GitHub Desktop.

This lives in the npm module oog (https://npmjs.org/package/oog).

Running the ClojureScript Hello World example in node.js without creating a goog global.

$ cljsc hello.cljs > hello.js
$ coffee test.coffee
Hello node.js
vm = require 'vm'
fs = require 'fs'
path = require 'path'
module.exports = (dir) ->
base_dir = path.resolve dir
context = vm.createContext()
load = (filename) ->
script = fs.readFileSync filename, 'utf8'
vm.runInContext script, context, filename
load path.resolve base_dir, 'base.js'
load path.resolve base_dir, 'deps.js'
{goog} = context
goog_require = goog.require
goog.require = (name) ->
return if goog.isProvided_ name
filename = goog.dependencies_.nameToPath[name]
load path.resolve base_dir, filename if filename?
goog_require name
load: load
goog: goog
global: context
require: (name) ->
goog.require name
goog.getObjectByName name
(ns hello)
(defn ^:export greet [n]
(str "Hello " n))
init_goog = require('./goog.coffee')
shim = init_goog './out/goog'
shim.load 'hello.js'
hello = shim.require 'hello'
console.log hello.greet 'node.js'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment