-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
126 lines (109 loc) · 3.43 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
var nodePath = require("path");
function getExt(node) {
return nodePath.extname(node.source.value).replace(/^\./, "");
}
module.exports = function(babel) {
var styleName = null;
var style = null;
var specifier = null;
var randomSpecifier = null;
var t = babel.types;
return {
post() {
randomSpecifier = null;
},
visitor: {
ImportDeclaration: function importResolver(path, state) {
var extensions =
state.opts != null &&
Array.isArray(state.opts.extensions) &&
state.opts.extensions;
if (!extensions) {
throw new Error(
"You have not specified any extensions in the plugin options."
);
}
var node = path.node;
var anonymousImports = path.container.filter(n => {
return (
t.isImportDeclaration(n) &&
n.specifiers.length === 0 &&
extensions.indexOf(getExt(n)) > -1
);
});
if (anonymousImports.length > 1) {
throw new Error(
"Cannot use anonymous style name with more than one stylesheet import."
);
}
if (extensions.indexOf(getExt(node)) === -1) {
return;
}
specifier = node.specifiers[0];
randomSpecifier = t.ImportDefaultSpecifier(
path.scope.generateUidIdentifier()
);
node.specifiers = [specifier || randomSpecifier];
},
JSXOpeningElement: {
exit(path, state) {
if (
styleName === null ||
randomSpecifier === null ||
!t.isStringLiteral(styleName.node.value)
) {
return;
}
var classNames = styleName.node.value.value
.split(" ")
.filter(v => v.trim() !== "");
var expressions = classNames
.map(c => {
var parts = c.split(".");
var hasParts = parts[0] !== undefined && parts[1] !== undefined;
if (specifier && !hasParts) {
return;
}
var obj = hasParts ? parts[0] : randomSpecifier.local.name;
var prop = hasParts ? parts[1] : c;
var hasHyphen = /\w+-\w+/.test(prop) === true;
return t.memberExpression(
t.identifier(obj),
hasHyphen ? t.stringLiteral(prop) : t.identifier(prop),
hasHyphen
);
})
.filter(e => e !== undefined);
var hasStyleNameAndStyle =
styleName &&
style &&
styleName.parentPath.node === style.parentPath.node;
if (hasStyleNameAndStyle) {
style.node.value = t.arrayExpression(
expressions.concat([style.node.value.expression])
);
styleName.remove();
} else {
if (classNames.length > 1) {
styleName.node.value = t.arrayExpression(expressions);
} else {
styleName.node.value = expressions[0];
}
styleName.node.name.name = "style";
}
style = null;
styleName = null;
specifier = null;
}
},
JSXAttribute: function JSXAttribute(path, state) {
var name = path.node.name.name;
if (name === "styleName") {
styleName = path;
} else if (name === "style") {
style = path;
}
}
}
};
};