我正在尝试确定是否可以在 Node.js 上使用 Om 来生成 HTML。我已经能够让ClojureScript 在 Node.js 上运行,并且显然React 可以在 Node.js 上运行。我按照基本指南创建了一个 Leiningen 项目
lein new figwheel om-tut -- --om
修改project.clj文件启动如下
(defproject om-tut "0.1.0-SNAPSHOT"
:description "FIXME: write this!"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.6.0"]
[org.clojure/clojurescript "0.0-2850"]
[figwheel "0.2.5-SNAPSHOT"]
[org.clojure/core.async "0.1.346.0-17112a-alpha"]
[sablono "0.3.4"]
[org.omcljs/om "0.8.8"]]
:plugins [[lein-cljsbuild "1.0.4"]
[lein-figwheel "0.2.5-SNAPSHOT"]
[lein-npm "0.4.0"]]
:source-paths ["src"]
:clean-targets ^{:protect false} ["resources/public/js/compiled" "out/server" "out/server/om_tut.js"]
:cljsbuild {
:builds [{:id "dev"
:source-paths ["src" "dev_src"]
:compiler {:output-to "resources/public/js/compiled/om_tut.js"
:output-dir "resources/public/js/compiled/out"
:optimizations :none
:main om-tut.dev
:asset-path "js/compiled/out"
:source-map true
:source-map-timestamp true
:cache-analysis true }}
{:id "min"
:source-paths ["src"]
:compiler {:output-to "resources/public/js/compiled/om_tut.js"
:main om-tut.core
:optimizations :advanced
:pretty-print false}}
{:id "server"
:source-paths ["src"]
:compiler {
:main pow.core
:output-to "out/server/om_tut.js"
:output-dir "out/server"
:optimizations :simple
:target :nodejs
:cache-analysis true
:source-map "out/server/om_tut.js.map"}}]}
并将 core.cljs 更改为
(ns ^:figwheel-always om-tut.core
(:require[om.core :as om :include-macros true]
[om.dom :as dom :include-macros true]))
(enable-console-print!)
(println "Edits to this text should show up in your developer console.")
;; define your app data so that it doesn't get over-written on reload
(defonce app-state (atom {:text "Hello world!"}))
(defn render [data owner]
(reify om/IRender
(render [_]
(dom/h1 nil (:text data)))))
(om/root
render
app-state
{:target (. js/document (getElementById "app"))})
(defn -main []
om.dom/render-to-str (render app-state nil))
(set! *main-cli-fn* -main)
项目使用以下命令编译成功
lein cljsbuild once server
但是当我尝试从 Windows cmd shell 运行生成输出时
node out\server\om_tut.js
我明白了
...\om-tut\out\server\om_tut.js:40237
om.dom.input = om.dom.wrap_form_element.call(null, React.DOM.input, "input");
^
ReferenceError: React is not defined
at Object.<anonymous> (...\om-tut\out\server\
om_tut.js:40237:52)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
我认为未定义的 React 对象应该来自 ...\om-tut\out\react.inc.js 文件,该文件存在并且应该在生成的 om_tut.js 文件的第 37532 行加载
cljs.core.load_file("react.inc.js");
关于可能出了什么问题的任何想法?