如何从具有多级路径的页面加载 ClojureScript 输出?
How to load ClojureScript output from a page with multi-level path?
我正在使用 Boot 开发单页 Web 应用程序。它最初是从 Holy Grail example.
开始的
我的 Compojure 路由定义如下:
(defroutes routes
; sente
(GET "/chsk" req ((:ring-ajax-get-or-ws-handshake (:sente system)) req))
(POST "/chsk" req ((:ring-ajax-post (:sente system)) req))
; everything else
(GET "/*" [] (-> (resource-response "/index.html")
(content-type "text/html")))
; vestige of old times
(route/not-found "Not Found"))
注意 "/*"
路由中的星号:我希望所有请求都到 return index.html
,ClojureScript 部分处理实际路由。
我是 运行 开发模式下的应用程序,这是来自 build.boot
的片段:
(deftask dev
"Run a restartable system in the Repl"
[]
(comp
(environ :env {:http-port "3000"})
(watch :verbose true)
(system :sys #'dev-system :auto true :files ["handler.clj"])
(reload :on-jsload 'myapp.core/init!)
(cljs :source-map true)
(repl :server true :init-ns 'myapp.user)))
它适用于单级路径,例如 localhost:3000/test
。但是,对于具有多级路径的页面,例如localhost:3000/foo/bar
.
index.html
中加载 JavaScript 的部分如下所示:
<body>
<div id="container"></div>
<script type="text/javascript" src="/main.js"></script>
</body>
最初是 src="main.js"
,但我添加了前导斜线,以便它从多级页面中找到 main.js
。如果没有斜杠,浏览器可能会在目录 [somewhere-in-cljs-output]/foo/main.js
中查找文件,假设页面为 localhost:3000/foo/bar
.
现在,main.js
由 ClojureScript 编译器生成,这里是:
var CLOSURE_UNCOMPILED_DEFINES = null;
if(typeof goog == "undefined") document.write('<script src="main.out/goog/base.js"></script>');
document.write('<script src="main.out/cljs_deps.js"></script>');
document.write('<script>if (typeof goog != "undefined") { goog.require("boot.cljs.main26293"); } else { console.warn("ClojureScript could not load :main, did you forget to specify :asset-path?"); };</script>');
当它在页面 localhost:3000/foo/bar
上运行时,我看到了警告。
显然问题与原始 main.js
参考相同,因为所需 JavaScript 文件的路径是相对的,而不是绝对的。我如何让它们成为绝对的?
我想我必须更改 build.boot
中的 cljs
任务调用,但不确定如何更改,因为我对 Boot 工作方式的心理模型非常不稳定。
感谢任何帮助。
稍作修改导致 build.boot
的 dev
任务发生以下变化:
(cljs :source-map true
:compiler-options {:asset-path "/main.out"})
我正在使用 Boot 开发单页 Web 应用程序。它最初是从 Holy Grail example.
开始的我的 Compojure 路由定义如下:
(defroutes routes
; sente
(GET "/chsk" req ((:ring-ajax-get-or-ws-handshake (:sente system)) req))
(POST "/chsk" req ((:ring-ajax-post (:sente system)) req))
; everything else
(GET "/*" [] (-> (resource-response "/index.html")
(content-type "text/html")))
; vestige of old times
(route/not-found "Not Found"))
注意 "/*"
路由中的星号:我希望所有请求都到 return index.html
,ClojureScript 部分处理实际路由。
我是 运行 开发模式下的应用程序,这是来自 build.boot
的片段:
(deftask dev
"Run a restartable system in the Repl"
[]
(comp
(environ :env {:http-port "3000"})
(watch :verbose true)
(system :sys #'dev-system :auto true :files ["handler.clj"])
(reload :on-jsload 'myapp.core/init!)
(cljs :source-map true)
(repl :server true :init-ns 'myapp.user)))
它适用于单级路径,例如 localhost:3000/test
。但是,对于具有多级路径的页面,例如localhost:3000/foo/bar
.
index.html
中加载 JavaScript 的部分如下所示:
<body>
<div id="container"></div>
<script type="text/javascript" src="/main.js"></script>
</body>
最初是 src="main.js"
,但我添加了前导斜线,以便它从多级页面中找到 main.js
。如果没有斜杠,浏览器可能会在目录 [somewhere-in-cljs-output]/foo/main.js
中查找文件,假设页面为 localhost:3000/foo/bar
.
现在,main.js
由 ClojureScript 编译器生成,这里是:
var CLOSURE_UNCOMPILED_DEFINES = null;
if(typeof goog == "undefined") document.write('<script src="main.out/goog/base.js"></script>');
document.write('<script src="main.out/cljs_deps.js"></script>');
document.write('<script>if (typeof goog != "undefined") { goog.require("boot.cljs.main26293"); } else { console.warn("ClojureScript could not load :main, did you forget to specify :asset-path?"); };</script>');
当它在页面 localhost:3000/foo/bar
上运行时,我看到了警告。
显然问题与原始 main.js
参考相同,因为所需 JavaScript 文件的路径是相对的,而不是绝对的。我如何让它们成为绝对的?
我想我必须更改 build.boot
中的 cljs
任务调用,但不确定如何更改,因为我对 Boot 工作方式的心理模型非常不稳定。
感谢任何帮助。
稍作修改导致 build.boot
的 dev
任务发生以下变化:
(cljs :source-map true
:compiler-options {:asset-path "/main.out"})