Uploaded image for project: 'ClojureScript'
  1. CLJS-2061

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

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major
    • Resolution: Completed
    • Affects versions: 1.9.562
    • Fix versions: 1.9.854
    • Labels:
      None
    • Approval:
      Accepted
    • Patch:
      Code and Test

      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

      (ns demo.foo
        (:require ["react" :as react :refer (createElement)])
        (:import ["react-native" Text View]))
      

      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

        Attachments

          Activity

            People

            • Assignee:
              dnolen David Nolen
              Reporter:
              thheller Thomas Heller
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: