Are there configuration options for ES6 module resolution?


#1

For my set up, my source files are written in ES6 module format with imports / exports and what nots. When I build, I use BabelJS to transform my source into SystemJS modules, which I configure to resolve modules much like I would configure RequireJS; using a “baseURL”, “paths” and “map” to help resolve my modules consistently.

This leaves me in a pickle when trying to resolve my modules when editing with Tern. If I set up my module dependencies to be relative paths, Tern appears to work. However, I seldom use relative paths to resolve modules, and instead resolve my modules using their full path from a “baseURL”.

Are there additional configuration options for the “modules” and “es_modules” plugins like there is for the “requirejs” plugin? Or does the requirejs plugin integrate with the modules plugin?


#2

This doesn’t exist yet, but the good news is that Tern’s module resolution mechanism is pluggable, and you could write a plugin that provides this functionality. You can look at plugin/node_resolve.js for an example of a resolver plugin.


#3

“pluggable” !== “readable” :smile:

When a module starts with:

(function(mod) {
  if (typeof exports == "object" && typeof module == "object") // CommonJS
    return mod(require("../lib/infer"), require("../lib/tern"), require("../lib/signal"), require)
  if (typeof define == "function" && define.amd) // AMD
    return define(["../lib/infer", "../lib/tern", "../lib/signal"], mod)
  mod(tern, tern, tern.signal)
})(function(infer, tern, signal, require) {
  "use strict"

I’m like … yea… Clean Code Handbook … (or maybe I’m just spoiled by the ES6 cleanness).

I’ve looked at the plugin/node_resolve.js plugin.

Isn’t it completely different constructed meaning that one would have to rewrite the es6_module/module plugins in the process?

Shouldn’t it be simplier to just adjust the existing methods to resolve the paths like you do in the requirejs plugin:

  function resolveName(name, data) {

  ...

  if (opts.paths) {
        var known = opts.paths[name];
        if (known) return flattenPath(base + known + ".js");
        var dir = name.match(/^([^\/]+)(\/.*)$/);
        if (dir) {
          var known = opts.paths[dir[1]];
          if (known) return flattenPath(base + known + dir[2] + ".js");
        }

… ???

Cheers :slight_smile:


#4

Same use case here, I wonder if there’s an plugin in the wild that supports this that someone created?

I would write one myself but I don’t even know where to start using plugins/node_resolve as an example, it seems to me like it should be possible to just add support for a basePath configuration that can be passed to plugins/es_resolve? Then the base modules plugin would probably need to be updated to support a basePath config since my understanding is that es_resolve uses modules as a base? That would look the cleanest solution to me.