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

Add support for ES6 default imports/exports

    Details

    • Type: Improvement
    • Status: Open
    • Priority: Minor
    • Resolution: Unresolved
    • Affects versions: None
    • Fix versions: None
    • Labels:

      Description

      ES6 has special syntax for using "default" imports and there is currently no equivalent for CLJS when using imported ES6 code.

      import * as name from "module-name";
      (:require ["module-name" :as name])
      
      import { export } from "module-name";
      (:require ["module-name" :refer (export)])
      
      import { export as alias } from "module-name";
      (:require ["module-name" :refer (export) :rename {export alias}])
      
      import defaultExport from "module-name";
      import defaultExport, { export [ , [...] ] } from "module-name";
      import defaultExport, * as name from "module-name";
      ;; no easy access to defaultExport
      

      I'm proposing to add a :default to the ns :require.

      (:require ["module-name" :as mod :default foo])
      

      This makes it much more convenient to use rewritten ES6 code from CLJS. If "module-name" has a default export you currently have to write mod/default everywhere since they is no easy way to alias the default.

      (:require ["material-ui/RaisedButton" :as RaisedButton])
      ;; :as is incorrect and the user must now use
      RaisedButton/default
      
      (:require ["material-ui/RaisedButton" :default RaisedButton])
      ;; would allow direct use of
      RaisedButton
      

      Internally the Closure compiler (and ES6 code rewritten by babel) will rewrite default exports to a .default property, so :default really is just a convenient way to access it.

      The long version that already works is

      (:require ["material-ui/RaisedButton" :refer (default) :rename {default RaisedButton}])
      

      When ES6 becomes more widespread we should have convenient way to correctly refer to "default" exports.

      [1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import

        Attachments

          Activity

            People

            • Assignee:
              deraen Juho Teperi
              Reporter:
              thheller Thomas Heller
            • Votes:
              7 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated: