forked from uditalias/babel-plugin-remove-imports
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
37 lines (32 loc) · 1.26 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
/*
* Babel plugin to remove unwanted `import` declarations when building
* packages with babel transforms.
*
* PLEASE USE WITH COUTION and check your RegExp expressions carefully.
*/
module.exports = function (regex) {
return function(babel) {
return new babel.Transformer('babel-plugin-remove-imports', {
ImportDeclaration: function (node, parent) {
//node.source.value contains the import declaration
var text = node.source.value;
//convert regex to Array of regexp if we have a single item
if(!(regex instanceof Array)) {
regex = [regex];
}
//iterate over all regexps to find a truthy one,
//when found, remove the `import` node from the code
for(var i = 0, len = regex.length; i < len; i++) {
if(isRegexExpressionTruthy(text, regex[i])) {
this.dangerouslyRemove();
break;
}
}
}
});
}
};
//check whether the text (import declaration) is matches the given regex
function isRegexExpressionTruthy(text, regex) {
return (regex instanceof RegExp) ? !!text.match(regex) : false;
}