ClojureScript

REPL 和主要入口點

本指南需要 ClojureScript 1.10.238 或更高版本,並假設您熟悉快速入門.

cljs.main 命名空間

cljs.main 命名空間提供了函數,允許通過 Java 的應用程式啟動工具 java 啟動 ClojureScript 程式和互動式會話。

本指南也易於與任何其他支援 Clojure CLI -m 標誌的 Clojure 建置工具一起使用。

例如,使用 clj

clj -M -m cljs.main ...

cljs.main --help

cljs.main/main 入口點接受各種引數和標誌。

  • 若沒有選項或引數,則執行互動式讀取-求值-列印迴圈

  • 初始化選項

    • -co, --compile-opts edn 配置建置的選項,可以是 EDN 字串或系統相關的以路徑分隔的 EDN 檔案/類別路徑資源清單。選項將由左至右合併。

    • -d, --output-dir 路徑 設定要使用的輸出目錄。如果提供此選項,則該目錄中的 cljsc_opts.edn 將用於設定 ClojureScript 編譯器選項

    • -re, --repl-env 環境 要使用的 REPL 環境。內建支援的值:nodebrowser。預設值為 browser

    • -ro, --repl-opts edn 配置 repl-env 的選項,可以是 EDN 字串或系統相關的以路徑分隔的 EDN 檔案/類別路徑資源清單。選項將由左至右合併。

    • -t, --target 名稱 JavaScript 目標。設定環境引導,預設值為 browser。支援的值:nodenodejswebworkernone

  • 僅用於 --main--repl 的初始化選項

    • -e, --eval 字串 求值字串中的表達式;列印非 nil

    • -i, --init 路徑 載入檔案或資源

    • -v, --verbose 布林值 如果為 true,將啟用 ClojureScript 詳細記錄

  • 僅用於 --compile 的初始化選項

    • -O, --optimizations 層級 設定最佳化層級,僅在 --compile 主要選項中有效。有效值為:nonewhitespacesimpleadvanced

    • -o, --output-to 檔案 設定輸出的編譯檔案

    • -w, --watch 路徑 持續建置,僅在 --compile 主要選項中有效。指定要監看的系統相關的以路徑分隔的目錄清單。

  • 主要選項

    • - 從標準輸入執行腳本

    • -c, --compile [命名空間] 執行編譯。如果指定可選的命名空間,則將其用作主要入口點。如果後面接著 --repl,則在編譯完成後啟動 REPL。如果後面接著 --server,則在編譯完成後啟動一個服務目前目錄的 Web 伺服器。

    • -h, --help, -? 列印此說明訊息並退出

    • -m, --main 命名空間 使用引數從命名空間呼叫 -main 函數

    • -r, --repl 執行 repl

    • -s, --serve 主機:埠號 啟動一個簡單的 Web 伺服器以服務目前目錄

    • 路徑 從檔案或資源執行腳本

  • 對於 --main--repl

    • 進入 cljs.user 命名空間

    • *command-line-args* 繫結到包含在任何主要選項之後出現的命令列引數的字串序列

    • 按順序執行所有初始化選項

    • 呼叫 -main 函數,或根據請求執行 repl 或腳本

初始化選項可以重複並自由混合,但必須出現在任何主要選項之前。

--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 - 保存最後三個被求值的表達式的結果

  • *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 選項指定的檔案。