title: How to import modules in Swift's JavaScriptCore?
body: <p>I'm trying to use Swift's JavaScriptCore framework to take advantage of an existing JavaScript library that uses ES6 modules. Specifically, <a href="https://github.com/scp93ch/morse-pro" rel="noreferrer">morse-pro</a> by Stephen C Phillips. I've added the files to an Xcode playground, then used this code to load the library and run it in the JavaScript context:</p>
  
  <pre><code>import JavaScriptCore
  var jsContext = JSContext()
  // set up exception handler for javascript errors
  jsContext?.exceptionHandler = { context, exception in
      if let exc = exception {
          print("JS Exception:", exc.toString())
      }
  }
  // read the javascript files in and evaluate
  if let jsSourcePath = Bundle.main.path(forResource: "morse-pro-master/src/morse-pro-message", ofType: "js") {
      do {
          let jsSourceContents = try String(contentsOfFile: jsSourcePath)
          jsContext?.evaluateScript(jsSourceContents)
      } catch {
          print(error.localizedDescription)
      }
  }
  </code></pre>
  
  <p>This approach works fine with simple "Hello world" sort of tests, but it chokes on the morse-pro library with this JavaScript error:</p>
  
  <blockquote>
    <p>SyntaxError: Unexpected token '*'. import call expects
    exactly one argument.</p>
  </blockquote>
  
  <p>The error appears to be caused by this line in morse-pro-message.js:</p>
  
  <pre><code>import * as Morse from './morse-pro';
  </code></pre>
  
  <p>which I believe is trying to import all the morse-pro files as a module.</p>
  
  <p>I'm not familiar with ES6 modules, but the library appears to be working for others in normal JavaScript contexts. Is there something wrong with the way I'm loading the library in Swift? Or are modules a feature that JavaScriptCore doesn't support? (The <a href="https://developer.apple.com/documentation/javascriptcore" rel="noreferrer">documentation</a> just says it supports "JavaScript" and doesn't get any more specific.)</p>
  
  <p>I would appreciate any suggestions that point me in the direction of getting this library running in a JavaScriptCore VM.</p>