-
Notifications
You must be signed in to change notification settings - Fork 76
/
index.js
132 lines (118 loc) · 3.19 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
import { hasRootPathPrefixInString, transformRelativeToRootPath } from './helper';
import CallExpressionTester from './call-expression-tester';
const replacePrefix = (path, opts = [], sourceFile) => {
const paths = 'paths' in opts ? opts.paths : opts;
const options = [].concat(paths);
for (let i = 0; i < options.length; i++) {
let rootPathSuffix = '';
let rootPathPrefix = '';
const option = options[i];
if (option.rootPathSuffix && typeof option.rootPathSuffix === 'string') {
rootPathSuffix = option.rootPathSuffix;
}
if (option.rootPathPrefix && typeof option.rootPathPrefix === 'string') {
rootPathPrefix = option.rootPathPrefix;
} else {
rootPathPrefix = '~';
}
if (hasRootPathPrefixInString(path, rootPathPrefix)) {
return transformRelativeToRootPath(
path,
rootPathSuffix,
rootPathPrefix,
sourceFile,
option.root || undefined,
);
}
}
return path;
};
const replacePrefixStrOrQuasi = (node, opts, sourceFile) => {
// String literal
if (typeof node === 'string') {
return replacePrefix(node, opts, sourceFile);
}
// Template literal quasi
else if (node && typeof node.cooked === 'string') {
return {
...node,
cooked: replacePrefix(node.cooked, opts, sourceFile),
raw: replacePrefix(node.raw, opts, sourceFile),
};
} else {
return node;
}
};
/**
* Recursively traverses binary expressions to find the first `StringLiteral` if any.
* @param {Object} t Babel types
* @param {Node} arg a Babel node
* @return {StringLiteral?}
*/
const traverseExpression = (t, arg) => {
if (t.isTemplateLiteral(arg)) {
return arg.quasis[0];
}
if (t.isStringLiteral(arg)) {
return arg;
}
if (t.isBinaryExpression(arg)) {
return traverseExpression(t, arg.left);
}
return null;
};
export default ({ types: t }) => {
let callExpressionTester;
const visitor = {
CallExpression(path, state) {
if (!callExpressionTester.test(path)) {
return;
}
const args = path.node.arguments;
if (!args.length) {
return;
}
const firstArg = traverseExpression(t, args[0]);
if (firstArg) {
firstArg.value = replacePrefixStrOrQuasi(
firstArg.value,
state.opts,
state.file.opts.filename,
);
}
},
ImportDeclaration(path, state) {
path.node.source.value = replacePrefix(
path.node.source.value,
state.opts,
state.file.opts.filename,
);
},
ExportNamedDeclaration(path, state) {
if (path.node.source) {
path.node.source.value = replacePrefix(
path.node.source.value,
state.opts,
state.file.opts.filename,
);
}
},
ExportAllDeclaration(path, state) {
if (path.node.source) {
path.node.source.value = replacePrefix(
path.node.source.value,
state.opts,
state.file.opts.filename,
);
}
},
};
return {
visitor: {
Program(path, state) {
callExpressionTester = new CallExpressionTester(t, state.opts);
path.traverse(visitor, state);
},
},
};
};