Exporting UMD from ClojureScript library

348 views
Skip to first unread message

Shaun LeBron

unread,
Nov 22, 2015, 2:42:53 AM11/22/15
to ClojureScript
I'm trying to export a JavaScript API from my ClojureScript library using a UMD pattern, given the following export function:
https://github.com/shaunlebron/parinfer/blob/7131d10ee21a6674279577b76743f623d4b2c3c9/parinfer-lib/src/parinfer/api_js.cljs#L64-L70

After compiling in advanced mode, and running Node, `require("./parinfer.js")` just returns {}. Adding the `:target :nodejs` option doesn't help.

I'm thinking it has something to do with google closure renaming my `module` symbol despite having the externs to prevent it. Anyway to prevent this?

Thomas Heller

unread,
Nov 22, 2015, 4:20:49 AM11/22/15
to ClojureScript
Hey,

my advice would be to use a post-process step instead of trying to making it go through the closure compiler. Not only will that produce headaches but the generated CLJS->JS also does not match the expected structure for UMD exports.

You'd need to tag the functions you want to have available by name ala

(defn ^:export js-indent-mode []...)

and then refer to them on the exports. Note that you need to refer to the munged names (eg. _ instead of -).

I do not know how to do this in cljsbuild but could show you an example in shadow-build if interested. Anyways all you'd need is to pipe the compiled output into the below script (at the marker). No externs needed for this case.

(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof module === 'object' && module.exports) {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory();
} else {
// Browser globals (root is window)
root.returnExports = factory();
}
}(this, function () {

// CLJS-COMPILED-OUTPUT-HERE

return {indentMode: parinfer.api_js.js_indent_mode,
indentModeChange: parinfer.api_js.js_indent_mode_change,
parenMode: parinfer.api_js.js_paren_mode};
}));


HTH,
/thomas

Shaun LeBron

unread,
Nov 23, 2015, 12:06:03 AM11/23/15
to ClojureScript
Thanks, Thomas! will post result here when completed.

Shaun LeBron

unread,
Nov 24, 2015, 3:29:22 PM11/24/15
to ClojureScript
Everything working here using some new shadow-build features Thomas made!
https://github.com/shaunlebron/parinfer/blob/52dcdaa140c60fd31a70f1ea2fe1e1359e7d4889/lib/dev/build.clj#L10-L13
Reply all
Reply to author
Forward
0 new messages