-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
155 lines (127 loc) · 4.27 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
'use strict';
const path = require('path');
const whatwgUrl = require('whatwg-url');
const resolve = require('resolve');
const pathIsInside = require('path-is-inside');
const minimatch = require('minimatch');
const isPathSpecifier = str => /^\.{0,2}[/\\]/.test(str);
const pathToURL = str => {
/* istanbul ignore next */
if (path.sep === path.win32.sep) {
str = str.replace(/\\/g, '/');
}
return str;
};
function basedirResolve(importPath, sourceFileName, pluginOptions) {
const {alwaysRootImport, neverRootImport} = {
alwaysRootImport: [],
neverRootImport: [],
...pluginOptions
};
const sourceDirName = path.dirname(sourceFileName);
if (isPathSpecifier(importPath)) {
/* Not a bare import. */
return sourceDirName;
}
if (!Array.isArray(alwaysRootImport) || alwaysRootImport.length === 0) {
return sourceDirName;
}
const importPackage = importPath.split('/', importPath[0] === '@' ? 2 : 1).join('/');
if (alwaysRootImport.some(v => minimatch(importPackage, v)) &&
!neverRootImport.some(v => minimatch(importPackage, v))) {
return pluginOptions.rootBaseDir || process.cwd();
}
return sourceDirName;
}
function absResolve(importPath, sourceFileName, pluginOptions = {}) {
if (whatwgUrl.parseURL(importPath) !== null) {
return importPath;
}
return resolve.sync(importPath, {
basedir: basedirResolve(importPath, sourceFileName, pluginOptions),
extensions: pluginOptions.extensions || ['.mjs', '.js', 'json'],
moduleDirectory: pluginOptions.resolveDirectories || 'node_modules',
preserveSymlinks: pluginOptions.preserveSymlinks !== false,
packageFilter(packageJson) {
packageJson.main = packageJson.module ||
packageJson['jsnext:main'] || packageJson.main;
return packageJson;
}
});
}
function tryResolve(babelPath, importPath, sourceFileName, pluginOptions) {
if (whatwgUrl.parseURL(importPath) !== null) {
return importPath;
}
if ([].concat(pluginOptions.ignorePrefixes).some(ignore => importPath.startsWith(ignore))) {
return importPath;
}
try {
const importPathAbs = absResolve(importPath, sourceFileName, pluginOptions);
const nodeModules = path.resolve(pluginOptions.rootBaseDir || process.cwd(), 'node_modules');
const isNodeModule = pathIsInside(importPathAbs, nodeModules);
const fromNodeModule = pathIsInside(path.resolve(sourceFileName), nodeModules);
let importPathRel = path.relative(path.dirname(sourceFileName), importPathAbs);
const sep = pluginOptions.fsPath === true ? path.sep : path.posix.sep;
const {modulesDir} = pluginOptions;
if (modulesDir && isNodeModule && !fromNodeModule) {
if (modulesDir.includes('://')) {
return modulesDir + (modulesDir.endsWith('/') ? '' : '/') + pathToURL(path.relative(nodeModules, importPathAbs));
}
importPathRel = path.join(
modulesDir,
path.relative(nodeModules, importPathAbs));
}
if (pluginOptions.fsPath !== true) {
importPathRel = pathToURL(importPathRel);
}
if (!isPathSpecifier(importPathRel) && !path.isAbsolute(importPathRel)) {
importPathRel = '.' + sep + importPathRel;
}
return importPathRel;
} catch (_) {
if (pluginOptions.failOnUnresolved) {
throw babelPath.buildCodeFrameError(`Could not resolve '${importPath}'.`);
} else {
console.error(`Could not resolve '${importPath}' in file '${sourceFileName}'.`);
return importPath;
}
}
}
function getVisitor(t, opts) {
const visitor = {
CallExpression(path, {file}) {
if (path.node.callee.type !== 'Import') {
return;
}
const [source] = path.get('arguments');
if (source.type !== 'StringLiteral') {
/* Should never happen */
return;
}
source.replaceWith(t.stringLiteral(tryResolve(path, source.node.value, file.opts.parserOpts.sourceFileName, opts)));
},
'ImportDeclaration|ExportNamedDeclaration|ExportAllDeclaration'(path, {file}) {
const source = path.get('source');
// An export without a 'from' clause
if (source.node === null) {
return;
}
source.replaceWith(t.stringLiteral(tryResolve(path, source.node.value, file.opts.parserOpts.sourceFileName, opts)));
}
};
if (!opts.processAtProgramExit) {
return visitor;
}
return {
Program: {
exit(path, state) {
path.traverse(visitor, state);
}
}
};
}
module.exports = ({types: t}, opts) => ({
visitor: getVisitor(t, opts)
});
module.exports.resolve = absResolve;