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

dynamic import syntax to dynamically import a cljs/squint namespace. #598

Open
jaymanmdev opened this issue Dec 29, 2024 · 1 comment
Open

Comments

@jaymanmdev
Copy link

jaymanmdev commented Dec 29, 2024

Is your feature request related to a problem? Please describe.
I don't know if a solution for this already exists.
I understand that it is possible to use normal dynamic import syntax like so:

(js/import "./test.mjs")

however, it would be nice for Squint to resolve paths with the ability to provide a namespace just like top level requires do, as this would make things much nicer.

Describe the solution you'd like
I'd like to be able to do something like:

(js/import 'test)
;; or
(js/import (find-ns 'test))

I know that using symbols in Squint may be out of scope, as when compiled it attempts to use the cljs.core module, which comes from Cherry, I'm assuming? But possibly providing a built in way to do this within Squint could be beneficial?
(I'm still relatively new to Clojure so I'm not sure the right way to go about this.)

Edit: A possible idea is to have a universal way in Squint (a compile-time macro?) to turn a namespace into a javascript url path, that can be used by Squint developers, as well as internally as part of the normal :require syntax. Just a thought. :D

Describe alternatives you've considered
I've considered creating my own function that transforms a keyword into a namespace url. This could work like so:

(import-from-module :test.core)

which would translate to this at runtime:

(js/import "./test/core.mjs")

This would work as keywords are just compiled into strings at compile time in Squint.
However, this will likely have the runtime cost of not allowing bundlers like Vite to code split these modules properly (as the paths aren't defined properly at build time.)

Additional context
N/A

Thank you in advance Mr. Bork!

@borkdude
Copy link
Member

You can already write:

(require 'test)

without a namespace. Wouldn't that help?

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