-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
59 lines (52 loc) · 1.87 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
'use strict';
var path = require('path');
var resolve = require('./lib/resolve');
function getConfig(moduleName) {
try {
return require(resolve(path.join(moduleName, 'package.json')));
} catch (e) {
return {};
}
}
function getFullPath(originalPath) {
var fullPath = originalPath;
if (path.dirname(originalPath) === '.') {
// There are no subfolders, so node will try to use file specified by the
// "main" key in the package.json. We need the es6 version though, so
// let's lookup the "jsnext:main" key instead, if it's present.
var config = getConfig(originalPath);
if (config['jsnext:main']) {
fullPath = path.join(originalPath, config['jsnext:main']);
}
}
return getModulePath(fullPath);
}
function getModulePath(name) {
try {
return resolve(name);
} catch (e) {
// If a module wasn't found with this name, just return the original path.
console.warn('Module with name "' + name + '" could not be found by node."', e.message);
return name;
}
}
function renameAlias(originalPath, filename) {
var result = originalPath;
if (originalPath[0] !== '.' && originalPath[0] !== '/' && originalPath.indexOf(':') === -1) {
var fullPath = getFullPath(originalPath);
result = path.join(path.dirname(fullPath), path.basename(fullPath, '.js'));
// babel uses 'unknown' as a special value for filename when the transformed
// source can't be traced to a file (e.g., transformed string)
// https://github.com/babel/babel/blob/d2e7e6a/packages/babel-core/src/transformation/file/options/config.js
if (filename && filename !== 'unknown') {
result = path.relative(path.dirname(filename), result);
}
else {
console.warn('Warning: missing source path leads to wrong / absolute import path.');
}
}
return result.replace(/\\/g, '/');
}
module.exports = {
resolveModuleSource: renameAlias
};