Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

require-lazy macro unnecessarily prescriptive about namespace/module structure #247

Open
alexdao3 opened this issue Feb 2, 2022 · 1 comment

Comments

@alexdao3
Copy link

alexdao3 commented Feb 2, 2022

Hi there!

I attempted to use the rum/require-lazy macro for code-splitting modules but ran into some unexpected behavior with the module-loading logic. I have the following (simplified) build config and namespace structure:

;; shadow-cljs.edn
{:builds {:client {:target     :browser
                   :module-loader true
                   :modules    {:client {:entries [my-app.client]}
                                :room {:entries [my-app.components.room]
                                       :depends-on #{:client}}}}}}

;; my-app.some-other-page
(require-lazy '[my-app.components.room :refer [Room])

Expected behavior

When I load the some-other-page component, I expect the module loader to load the :room module specified in my build config

Actual behavior

However, the code emitted by the require-lazy macro attempts to load a nonexistent :components module (see screenshot), which creates a runtime exception because I don't have a :components module.

Other notes

The source of this issue is at

module (->> (str lib)
(re-find #"\.([a-z0-9-]+)")
second
keyword)]
, where the regex grabs components from [my-app.components.room] and ignores room. If I were specifying modules, I would assume that we grab the last token from a required namespace e.g. room from [my-app.components.room]. If it was intentional to resolve to components with your regex, could you share your reasoning?

Otherwise if you agree that extracting room makes more sense, I wonder if we could simplify the regex to make things a bit more explicit. I'm happy to create a PR

(-> (str module)
    (clojure.string/split #"\.")
    last
    keyword)

Screenshot of error loading components module:
image

@roman01la
Copy link
Collaborator

Original author of the macro here. This is indeed an undocummented and obscure behaviour. Instead the macro should resolve to actual module name, similar to how shadow's loadable macro is doing that https://github.com/thheller/shadow-cljs/blob/master/src/main/shadow/lazy.clj#L22

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants