Support ns :require for JS libs, allow strings along with symbols

Description

Requiring JS code from CLJS requires a lot of manual JS interop and can be simplified by allowing Strings in the ns form.

Instead of using (js/require "react") or relying on a provided global js/React it should be possible to write

It is somewhat at odds with the current :foreign-libs and CLJSJS ecosystem since the intent is to directly consume NPM packages instead of relying on re-packaged NPM packages. node.js is able to directly use these requires but other runtimes (eg. browsers) require an additional build step that provides the NPM packages. :foreign-libs could probably be enhanced to provide "react" instead of cljsjs.react and then js/React.

I don't know what it would take to bring this into CLJS but I think it would be a useful feature. I have a working implementation in the shadow-cljs project [1] but unfortunately that is not easily portable due to significant differences in the compilation process. It currently only works for requiring packages (ie. "react") but not for relative requires (ie. "./foo") but :foreign-libs has the same limitation.

[1] https://github.com/thheller/shadow-cljs

Environment

None

Attachments

1

Activity

Show:

David Nolen July 5, 2017 at 7:00 PM

Thomas, like I said we should have updated the ticket. I also suggested string based require and it just didn't have anything to do with all this other stuff that you are suggesting in this issue. Supporting Node.js style paths as requires is just never, ever going to happen. Whatever you had in mind is not what I had in mind

Thomas Heller July 5, 2017 at 6:09 PM

Not what I had in mind when I suggested this but if that is the case there is nothing left to say for me.

António Monteiro July 5, 2017 at 6:03 PM

String requires in `:npm-deps` is only about supporting cases like "react-dom/server". Nothing else.

Thomas Heller July 5, 2017 at 6:01 PM

Consistency for one? How annoying is this from a users perspective that you can only use it for the browser but not when building node.js projects where you'd use many more string requires on average? If the answer is truly "Just use js/require" it would probably be much better to remove this feature altogether as :npm-deps already provided a solution for this otherwise. :npm-deps was just as rushed though.

António Monteiro July 5, 2017 at 5:43 PM

From my perspective, `:mpm-deps` was always about the browser. If you're compiling for Node.js just use `js/require`, no?

Am I missing some huge benefit here?

Completed

Details

Assignee

Reporter

Approval

Accepted

Patch

Code and Test

Fix versions

Affects versions

Priority

Created May 29, 2017 at 7:29 PM
Updated July 5, 2017 at 7:00 PM
Resolved July 5, 2017 at 7:00 PM