2012年4月21日土曜日

ClojureScriptの導入

ハードル高いと言われてしまったので。

Leiningenの導入


まずはbuild toolの導入です。
https://github.com/technomancy/leiningen
からスクリプトをダウンロードしましょう。
  • Linux
    • Installationの"Download the script"
  • Windows
    •  Installationの"the batch file"
 ダウンロードしたスクリプトはleinと名前を付けて、PATHの通ったところまたはダウンロードした場所にPATHを通しましょう。

ターミナルを立ち上げて

lein self-install

が実行出来ればleiningenの導入は完了です。

Projectの作成


ターミナルで

lein new cljstest

と打ちましょう。
cljstestというプロジェクトが作られます。

作られたプロジェクトのディレクトリには、project.cljが存在します。
これを以下のように書き換えて下さい。

(defproject cljstest "1.0.0-SNAPSHOT"
:dependencies [[org.clojure/clojure "1.4.0"]]
:plugins [[lein-cljsbuild "0.1.8"]]
:cljsbuild {
:builds [{
:source-path "src"
:compiler {
:output-to "main.js"
:optimizations :whitespace
:pretty-print true}}]})
view raw project.clj hosted with ❤ by GitHub

詳しい設定の仕方は
https://github.com/emezeske/lein-cljsbuild

編集ができたら

lein deps
lein cljsbuild auto

を実行して下さい。

core.cljsの編集


cljstest/src/cljstestに存在するcore.cljをcore.cljsにリネームし、以下のように編集します。

(ns cljstest.core)
(.alert js/window "Hello")
view raw core.clj hosted with ❤ by GitHub

ファイルを保存すると自動でコンパイルされ、 cljstestにmain.jsが作成されます。
以下のようなhtmlファイルをcljstestに作り、実行してみましょう。

<html>
<head>
<title>cljstest</title>
</head>
<body>
<script type="text/javascript" src="main.js"></script>
</body>
</html>
view raw test.html hosted with ❤ by GitHub

アラートが出れば完成です。

0 件のコメント:

コメントを投稿