Project organization and cljsbuild config to requi

2019-09-16 03:33发布

问题:

How do I organize my project structure and configure cljsbuild to require my own namespace? For example in my project/src-cljs folder I have:

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

I'd like file1.cljs(namespaced as file1.file1) and file2.cljs(namespaced as file2.file2) to require required.cljs(namespaced as required.required).

My :cljsbuild looks like:

: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"}}]}

When I (:require [required.required :as required]) and compile I get the exception:

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

回答1:

You don't usually want a separate js output file and cljsbuild profile for each namespace. You want one single cljsbuild profile including all of your namespaces. Something like:

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

Not only that: you might want to have ["src-cljs"] as :source-paths and then name your namespaces like project.ns1.sub-ns1. But you can do it without the project ns prefix just fine.

You can find an example of this simple layout in the simple example project from lein-cljsbuild

Looking to the cljsbuild README, it seems like you were taking the path of "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.