Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

JavaScript Library Preprocessing

Shaun Mahood edited this pageMar 18, 2017 ·2 revisions

DEPRECATION NOTICE: Please do not edit this wiki. Instead submit pull requests tohttps://github.com/clojure/clojurescript-site

The up to date version of this page can be found athttps://clojurescript.org/reference/javascript-library-preprocessing

ClojureScript allows you to add a custom transformation step for JavaScript libraries. This means that you are able to include JavaScript libraries in your project which are written in a dialect or make use of a JavaScript syntax extension. In order to effect the desired code transformation adefmethod which satisfiescljs.closure/js-transforms must be provided to the:preprocess option of the foreign library. For example, you can transform a JavaScript library using JSX into regular React function calls as follows:

(require '[cljs.build.api:as b]         '[clojure.java.io:as io])(refer 'cljs.closure:only '[js-transforms])(import 'javax.script.ScriptEngineManager)(defmethodjs-transforms:jsx [ijs opts]  (let [engine (doto (.getEngineByName (ScriptEngineManager.)"nashorn")                 (.eval (io/reader (io/file"babel.min.js")))                 (.put"input" (:source ijs)))]    (assoc ijs:source      (.eval engine (str"Babel.transform(input, {presets: ['react']}).code")))))(b/build"src"  {:main 'my-project.core:output-to"out/my_project.js":output-dir"out":foreign-libs [{:file"libs/example.js":provides ["my.example"]:preprocess:jsx}]})

The example above uses Babel to transform the JavaScript code by loading a minified version ofbabel-standalone using Nashorn. Thejs-transforms method gets and returns an object which satisfies theIJavaScript protocol. The object can be a plain map or a record with keys like:url,:provides,:requires and:source. The second argument, which is passed tojs-transforms, is a map with the compiler options.The JavaScript preprocessing happens beforemodule conversion, however, both steps are independent of each other and can be used in disjunction.

Clone this wiki locally

[8]ページ先頭

©2009-2025 Movatter.jp