Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I mark methods created with `reify` with ^:export, so that the Closure compiler doesn't rename them?

When creating JavaScript objects with reify, how can I mark the methods with ^:export so that the Google Closure compiler doesn't rename them in advanced mode?

For example:

(reify
   Object
   (foo [this] ...)
   (bar [this] ...))

I've tried

(reify
   Object
   (^:export foo [this] ...)
   (^:export bar [this] ...))

but this doesn't seem to help, and the names still get changed with advanced optimizations.

If there isn't a way to do this, how can I construct a JavaScript object with methods, other than creating a plain js-obj and using set! to set functions to properties (where I'm not sure how to prevent advanced optimizations from breaking things either)?

like image 517
Jakub Arnold Avatar asked May 01 '15 21:05

Jakub Arnold


1 Answers

You have to provide ^:export on your protocol methods as you will call them in JS, not methods from your reified object directly.

(ns example.core)

(defprotocol MyProtocol
  (^:export foo [this])

(defn ^:export create []
  (reify
    MyProtocol
    (foo [this] "bar")))

Then you can use it from JS:

var a = example.core.create();
var b = example.core.foo(a);
// b = "bar"

I tried it with the current cljs.jar and it emitted optimized JS with original foo name.

like image 174
Piotrek Bzdyl Avatar answered Sep 26 '22 02:09

Piotrek Bzdyl