-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
59 lines (53 loc) · 1.51 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
const identifierVisitor = {
Identifier(path) {
if (path.node.name === this.name) {
this.exp.remove();
}
}
}
const callVisitor = {
CallExpression(path) {
const args = path.node.arguments || [];
if (args.length === 1) {
const ext = extensions(args[0].value);
if (this.exts.indexOf(ext) > -1) {
path.traverse(identifierVisitor, this);
}
}
}
}
const extensions = (path) => {
const m = /.*(\.\w+)$/ig.exec(path);
if (m) {
return m[1].toLocaleLowerCase();
}
return "";
};
module.exports = function ({ types: t }) {
const EXTS = [".css", ".less", ".scss"];
return {
visitor: {
CallExpression(path) {
const args = path.node.arguments || [];
if (args.length === 1) {
const ext = extensions(args[0].value);
const exts = this.extensions || EXTS;
if (exts.indexOf(ext) > -1) {
path.traverse(identifierVisitor, {
name: "require",
exp: path
});
}
}
},
VariableDeclarator(path) {
const exts = this.extensions || EXTS;
path.traverse(callVisitor, {
exts,
name: "require",
exp: path
});
}
}
};
}