-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
158 lines (128 loc) · 4.49 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const jsxSyntax = require('babel-plugin-syntax-jsx')
const REFERENCE = '__CSSM__'
const SOURCE_ATTR_NAME = 'styleName'
const TARGET_ATTR_NAME = 'className'
const getPathChecker = (state) => {
if (state.opts.pathToStyles) {
return new RegExp(state.opts.pathToStyles)
}
return /^\.\/styles\.css$/
}
const isCssModuleImport = (node, state) => {
const pathChecker = getPathChecker(state)
return node.specifiers.length === 0 && pathChecker.test(node.source.value)
}
const setState = (state, key, value) => state.file.set(key, value)
const getState = (state, key) => state.file.get(key)
const setClasses = (state, value) => setState(state, 'classes', value)
const getClasses = state => getState(state, 'classes')
const hasImportDecl = (path, state) => (
getState(state, 'hasCssModuleImport') || path.scope.hasBinding(REFERENCE)
)
module.exports = ({ types: t }) => {
const isSourceAttribute = node =>
t.isJSXIdentifier(node.name, { name: SOURCE_ATTR_NAME })
const isTargetAttribute = node =>
t.isJSXIdentifier(node.name, { name: TARGET_ATTR_NAME })
const buildAttribute = (name, value) => t.JSXAttribute(
t.JSXIdentifier(name),
t.isStringLiteral(value) ? value : t.JSXExpressionContainer(value),
)
return {
inherits: jsxSyntax,
name: 'transform-jsx-css-modules',
visitor: {
ImportDeclaration(path, state) {
if (isCssModuleImport(path.node, state)) {
const nextImport = t.importDeclaration(
[t.importDefaultSpecifier(t.identifier(REFERENCE))],
t.stringLiteral(path.node.source.value),
)
setState(state, 'hasCssModuleImport', true)
path.replaceWith(nextImport)
}
},
JSXOpeningElement: {
enter(path, state) {
if (!hasImportDecl(path, state)) return
// Reset to empty array for each new attribute
setClasses(state, [])
},
exit(path, state) {
if (!hasImportDecl(path, state)) return
const classes = getState(state, 'classes')
// TRANSFORMATIONS
if (classes.length > 0) {
path.node.attributes = path.node.attributes
.filter(attribute => !isSourceAttribute(attribute))
.filter(attribute => !isTargetAttribute(attribute))
}
if (classes.length === 1) {
path.node.attributes.push(buildAttribute(
TARGET_ATTR_NAME,
classes[0],
))
}
if (classes.length > 1) {
if (classes.every(className => t.isStringLiteral(className))) {
path.node.attributes.push(buildAttribute(
TARGET_ATTR_NAME,
t.stringLiteral(
classes
.map(className => className.value)
.join(' '),
),
))
} else {
path.node.attributes.push(buildAttribute(
TARGET_ATTR_NAME,
t.callExpression(
t.memberExpression(
t.arrayExpression(classes),
t.identifier('join'),
),
[t.stringLiteral(' ')],
),
))
}
}
},
},
JSXAttribute(path, state) {
if (!hasImportDecl(path, state)) return
let classes = getClasses(state)
if (isTargetAttribute(path.node)) {
if (t.isJSXExpressionContainer(path.node.value)) {
classes.push(path.node.value.expression)
}
if (t.isStringLiteral(path.node.value)) {
const globalClasses = path.node.value.value
.split(' ')
.map(className => t.stringLiteral(className))
classes = classes.concat(globalClasses)
}
}
if (isSourceAttribute(path.node)) {
if (t.isJSXExpressionContainer(path.node.value)) {
classes.push(t.memberExpression(
t.identifier(REFERENCE),
path.node.value.expression,
true,
))
}
if (t.isStringLiteral(path.node.value)) {
const scopedClasses = path.node.value.value
.split(' ')
.map(className => t.memberExpression(
t.identifier(REFERENCE),
t.stringLiteral(className),
true,
))
classes = classes.concat(scopedClasses)
}
}
setClasses(state, classes)
},
},
}
}