需要命名空间的项目组织和 cljsbuild 配置

Project organization and cljsbuild config to require namespace

如何组织我的项目结构并将 cljsbuild 配置为需要我自己的命名空间?例如在我的 project/src-cljs 文件夹中我有:

└── project
    ├── file1
    │   └── file1.cljs
    ├── file2
    │   └── file2.cljs
    └─── required
        └── required.cljs

我希望 file1.cljs(命名空间为 file1.file1)和 file2.cljs(命名空间为 file2.file2)要求 required.cljs(命名空间为 required.required).

我的 :cljsbuild 看起来像:

:cljsbuild {:builds
            [{:source-paths ["src-cljs/project/file1"]
              :compiler {:output-to "resources/public/js/file1.js"}}
             {:source-paths ["src-cljs/project/file2"]
              :compiler {:output-to "resources/public/js/file2.js"}}]}

当我 (:require [required.required :as required]) 并编译时我得到异常:

Caused by: clojure.lang.ExceptionInfo: No such namespace: required.required, could not locate required/required.cljs, required/required.cljc, or Closure namespace "required.required" at line 1 src-cljs/project/file1/file1.cljs

您通常不需要为每个名称空间使用单独的 js 输出文件和 cljsbuild 配置文件。您需要一个包含所有名称空间的 cljsbuild 配置文件。类似于:

:cljsbuild {:builds
            [{:source-paths ["src-cljs/project"]
              :compiler {:output-to "resources/public/js/project.js"}}]}

不仅如此:您可能希望将 ["src-cljs"] 作为 :source-paths,然后将您的名称空间命名为 project.ns1.sub-ns1。但是你可以在没有 project ns 前缀的情况下做到这一点。

您可以在来自 lein-cljsbuild

simple example project 中找到这个简单布局的示例

查看 cljsbuild 自述文件,您似乎走的是 "Multiple build configurations". In practice this is mostly used to have separate build profiles for your main code and the tests working against that code. That's shown in the advanced example 的道路。