boot-clj / boot-cljs

Boot task to compile ClojureScript programs.
Eclipse Public License 1.0
176 stars 40 forks source link

Unclear how to use new :preprocess in :foreign-libs #151

Closed skynet-gh closed 7 years ago

skynet-gh commented 7 years ago

I'm trying to follow the guide to the new ClojureScript features for JavaScript modules and am running into an issue adding preprocessors to boot-cljs. The guide uses lein, but I've tried to adapt it in this project.

The guide uses similar code to the clojurescript wiki to add preprocessors (in my build.boot):

(require '[clojure.java.io :as io]
         '[cljs.build.api :as b]
         '[cljs.closure :as closure])
(import 'javax.script.ScriptEngineManager)

(defmethod closure/js-transforms :jsx [ijs opts]
  (let [code (str (gensym))
        engine (doto (.getEngineByName (ScriptEngineManager.) "nashorn")
                 (.eval (io/reader (io/file "babel.min.js"))))]
    (.put engine code (:source ijs))
    (assoc ijs :source
      (.eval engine (str "Babel.transform(" code
                         ", {presets: ['react', 'es2016']}).code")))))

But, I guess because boot-cljs runs the compiler in a different pod, the preprocessor isn't picked up and I get this message in the console, and can't compile the jsx files:

WARNING: Unsupported preprocess value :jsx for foreign library src/js/hello.js. null

I know this feature is very new, but I'm interested in using it if it's available in boot, otherwise I might look into adding a way to pass it in. Thanks!

Deraen commented 7 years ago

This is currently quite hard to support, as there is no way to require the namespace which provides the js-transforms method in Boot-cljs pod. Code from build.boot is not available in Boot-cljs pods either.

I have proposed convention to use namespaced keywords for :preprocess values, so that namespace part can be used by tooling to require the namespace (https://github.com/clojure/clojurescript-site/pull/54). But this is not yet supported.

skynet-gh commented 7 years ago

Thanks for the explanation and your attention to this!

Deraen commented 7 years ago

Code is now merged to master and I have pushed a new SNAPSHOT. Here is a example using Cljsjs babel package and running example code during pod init: https://github.com/Deraen/saapas/commit/a4f261e8d5363933c7c065e81610fcccf4267078

https://github.com/boot-clj/boot-cljs/blob/master/docs/cljs.edn.md#compiler-pod-init

Deraen commented 7 years ago

I reverted the changes as this will be implemented upstream https://dev.clojure.org/jira/browse/CLJS-2143 and implementation here could have been incompatible.