-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
36 lines (34 loc) · 1.03 KB
/
index.js
File metadata and controls
36 lines (34 loc) · 1.03 KB
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
function replaceImportPathPlugin (path, t, opts) {
const { src, dest } = opts;
if (!src || !dest) {
console.error('src & dest should be provided in babel-plugin-replace-import-path');
return;
}
const node = path.node;
if (!node || !src) {
return;
}
if (typeof src === 'string') {
if (node.source.value === src) {
node.source.value = dest;
}
} else { // src is a regex
node.source.value = node.source.value.replace(src, dest);
}
}
export default function replaceImportPath ({types: t}) {
return {
name: 'replace-import-path',
visitor: {
ImportDeclaration (path, { opts = {} }) {
if (Object.prototype.toString.call(opts) === '[object Array]') {
opts.forEach(opt => {
replaceImportPathPlugin(path, t, opt);
});
} else {
replaceImportPathPlugin(path, t, opts);
}
}
}
};
};