ES6 and AMD require paths are raw, you can make them mean whatever you want, but by default mean baseUrl+path
. In npm the paths are relative to the file calling require
if the path starts with a directory separater, if there is no separator it looks in node_modules
. For example:
// looks in node_modules
// we'll call this a "vendor require"
var handlebars = require('handlebars');
// looks relative to this file
// we'll call this a "relative require"
var local = require('./handlebars');
There is no such convention in AMD or ES6. Given this ambiguity we will need to be more intelligent with the umd transpilation.
Perhaps we can check the dependencies in package.json
to distinguish between local and vendor requires.
Given this package.json:
{
"dependencies": {
"rsvp": "*"
}
}
The following module code...
import { EventTarget } from 'rsvp/events'
import format from 'time/format'
becomes:
var EventTarget = require('rsvp/events').EventTarget;
var format = require('./time/format');
(amd/global umd code omitted for clarity.)
Because node modules require relative, the transpiler needs to be aware of the current file's path.
Given a file nested two directories from root with the same code:
import { EventTarget } from 'rsvp/events'
import format from 'time/format'
The output becomes:
var EventTarget = require('rsvp/events').EventTarget;
var format = require('../../time/format');
Vendor requires for global modules will not work. All vendor requires must map to an entry in package.json's dependencies
or devDependencies
.
I think if you want something truly universal, you do what @thomasboyt suggested, and use relative paths, reserving absolute paths only for packages. (Basically, follow Node style, since it's the lowest common denominator.)
Besides, I sincerely doubt anyone's going to use the default module loader in the browser. That is, nobody is going to lay out their app in the format
Some relevant discussion here: http://esdiscuss.org/topic/modulenaminganddeclarations#content-100