ClojureScript

REPLとメインエントリポイント

このガイドでは、ClojureScript 1.10.238以降が必要であり、クイックスタートに精通していることを前提としています。.

cljs.main名前空間

cljs.main名前空間は、ClojureScriptプログラムとインタラクティブセッションをJavaのアプリケーションランチャーツールjava経由で起動できるようにする機能を提供します。

このガイドは、Clojure CLIの-mフラグをサポートする他のClojureビルドツールでも簡単に使用できます。

たとえば、cljを使用する場合

clj -M -m cljs.main ...

cljs.main --help

cljs.main/mainエントリポイントは、さまざまな引数とフラグを受け入れます。

  • オプションや引数がない場合は、インタラクティブなRead-Eval-Print Loopを実行します

  • initオプション

    • -co, --compile-opts edn ビルドを構成するためのオプション。EDN文字列、またはシステムに依存するパス区切りのEDNファイル/クラスパスリソースのリストを使用できます。オプションは左から右にマージされます。

    • -d, --output-dir path 使用する出力ディレクトリを設定します。指定された場合、そのディレクトリのcljsc_opts.ednがClojureScriptコンパイラオプションの設定に使用されます。

    • -re, --repl-env env 使用するREPL環境。組み込みでサポートされている値:nodebrowser。デフォルトはbrowserです。

    • -ro, --repl-opts edn repl-envを構成するためのオプション。EDN文字列、またはシステムに依存するパス区切りのEDNファイル/クラスパスリソースのリストを使用できます。オプションは左から右にマージされます。

    • -t, --target name JavaScriptターゲット。環境ブートストラップを構成し、デフォルトはbrowserです。サポートされている値:nodeまたはnodejswebworkernone

  • --mainおよび--replのみのinitオプション

    • -e, --eval string 文字列内の式を評価します。nil以外の値を表示します

    • -i, --init path ファイルまたはリソースをロードします

    • -v, --verbose bool trueの場合、ClojureScriptの詳細ログを有効にします

  • --compileのみのinitオプション

    • -O, --optimizations level 最適化レベルを設定します。--compileメインオプションでのみ有効です。有効な値は、nonewhitespacesimpleadvancedです

    • -o, --output-to file 出力コンパイルファイルを設定します

    • -w, --watch path 継続的にビルドします。--compileメインオプションでのみ有効です。監視するディレクトリのシステムに依存するパス区切りのリストを指定します。

  • メインオプション

    • - 標準入力からスクリプトを実行します

    • -c, --compile [ns] コンパイルを実行します。オプションの名前空間が指定された場合は、メインエントリポイントとして使用します。--replが続く場合は、コンパイルが完了した後にREPLを起動します。--serverが続く場合は、コンパイルが完了した後に現在のディレクトリをサーブするWebサーバーを起動します。

    • -h, --help, -? このヘルプメッセージを表示して終了します

    • -m, --main ns 引数付きの名前空間から-main関数を呼び出します

    • -r, --repl REPLを実行します

    • -s, --serve host:port 現在のディレクトリをサーブする単純なWebサーバーを起動します

    • path ファイルまたはリソースからスクリプトを実行します

  • --mainおよび--replの場合

    • cljs.user名前空間に入ります

    • メインオプションの後に出現するコマンドライン引数を含む文字列のシーケンスに*command-line-args*をバインドします

    • すべてのinitオプションを順番に実行します

    • リクエストされた場合、-main関数を呼び出すか、REPLまたはスクリプトを実行します

initオプションは、繰り返し使用することも、自由に混在させることもできますが、メインオプションの前に記述する必要があります。

--compileの場合、後で--replまたは--serveオプションを指定できます。

パスは、ファイルシステムでは絶対パスまたは相対パス、またはクラスパスに対して相対パスにすることができます。クラスパス相対パスには、@または@/のプレフィックスが付いています

同じ内容は、使用メッセージにも記載されています

Usage: java -cp cljs.jar cljs.main [init-opt*] [main-opt] [arg*]

With no options or args, runs an interactive Read-Eval-Print Loop

init options:
  -co, --compile-opts edn     Options to configure the build, can be an EDN
                              string or system-dependent path-separated list of
                              EDN files / classpath resources. Options will be
                              merged left to right.
   -d, --output-dir path      Set the output directory to use. If supplied,
                              cljsc_opts.edn in that directory will be used to
                              set ClojureScript compiler options
  -re, --repl-env env         The REPL environment to use. Built-in supported
                              values: node, browser. Defaults to browser
  -ro, --repl-opts edn        Options to configure the repl-env, can be an EDN
                              string or system-dependent path-separated list of
                              EDN files / classpath resources. Options will be
                              merged left to right.
   -t, --target name          The JavaScript target. Configures environment
                              bootstrap and defaults to browser. Supported
                              values: node or nodejs, webworker, none

init options only for --main and --repl:
   -e, --eval string          Evaluate expressions in string; print non-nil
                              values
   -i, --init path            Load a file or resource
   -v, --verbose bool         If true, will enable ClojureScript verbose logging

init options only for --compile:
   -O, --optimizations level  Set optimization level, only effective with --
                              compile main option. Valid values are: none,
                              whitespace, simple, advanced
   -o, --output-to file       Set the output compiled file
   -w, --watch paths          Continuously build, only effective with the --
                              compile main option. Specifies a system-dependent
                              path-separated list of directories to watch.

main options:
   -                          Run a script from standard input
   -c, --compile [ns]         Run a compile. If optional namespace specified,
                              use as the main entry point. If --repl follows,
                              will launch a REPL after the compile completes.
                              If --server follows, will start a web server that
                              serves the current directory after the compile
                              completes.
   -h, --help, -?             Print this help message and exit
   -m, --main ns              Call the -main function from a namespace with args
   -r, --repl                 Run a repl
   -s, --serve host:port      Start a simple web server to serve the current
                              directory
   path                       Run a script from a file or resource

For --main and --repl:

  - Enters the cljs.user namespace
  - Binds *command-line-args* to a seq of strings containing command line
    args that appear after any main option
  - Runs all init options in order
  - Calls a -main function or runs a repl or script if requested

The init options may be repeated and mixed freely, but must appear before
any main option.

In the case of --compile you may supply --repl or --serve options afterwards.

Paths may be absolute or relative in the filesystem or relative to
classpath. Classpath-relative paths have prefix of @ or @/

REPLの起動

ClojureScriptのreplを起動する最も簡単な方法は、同梱されているcljs.jarで次のコマンドラインを使用することです。

java -cp cljs.jar cljs.main

REPLプロンプトには、現在の名前空間の名前が表示されます。デフォルトはcljs.userです。

REPLを使用する場合、いくつかの特別な変数を使用できます

  • *1*2*3 - 評価された最後の3つの式の結果を保持します

  • *e - 最後の例外の結果を保持します。

スクリプトの起動

ClojureScriptコードでいっぱいのファイルをスクリプトとして実行するには、スクリプトへのパスをcljs.mainに引数として渡します

java -cp cljs.jar cljs.main /path/to/myscript.cljs

スクリプトへの引数の渡し方

スクリプトに引数を渡すには、cljs.mainを起動するときに、さらに引数として渡します

java -cp cljs.jar cljs.main /path/to/myscript.cljs arg1 arg2 arg3

引数は、変数*command-line-args*にバインドされた文字列のシーケンスとしてプログラムに提供されます

*command-line-args* => ("arg1" "arg2" "arg3")

ソースのコンパイル

ClojureScriptソースコードをコンパイルするには、-cオプションを使用してメインの名前空間をcljs.mainに渡します

java -cp src:cljs.jar cljs.main -c my-namespace.core

出力は、-dオプション(指定されていない場合はout)で指定されたディレクトリ、または-oオプションで指定されたファイルに書き込まれます。