-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
index.js
242 lines (227 loc) Β· 7.6 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// @flow
import { createEmotionMacro } from './emotion-macro'
import { createStyledMacro } from './styled-macro'
import cssMacro, { transformCssCallExpression } from './css-macro'
import { addNamed } from '@babel/helper-module-imports'
import nodePath from 'path'
import { getSourceMap, getStyledOptions } from './utils'
let webStyledMacro = createStyledMacro({
importPath: '@emotion/styled-base',
originalImportPath: '@emotion/styled',
isWeb: true
})
let nativeStyledMacro = createStyledMacro({
importPath: '@emotion/native',
originalImportPath: '@emotion/native',
isWeb: false
})
let primitivesStyledMacro = createStyledMacro({
importPath: '@emotion/primitives',
originalImportPath: '@emotion/primitives',
isWeb: false
})
export const macros = {
createEmotionMacro,
css: cssMacro,
createStyledMacro
}
export type BabelPath = any
export type EmotionBabelPluginPass = any
let emotionCoreMacroThatsNotARealMacro = ({ references, state, babel }) => {
Object.keys(references).forEach(refKey => {
if (refKey === 'css') {
references[refKey].forEach(path => {
transformCssCallExpression({ babel, state, path: path.parentPath })
})
}
})
}
emotionCoreMacroThatsNotARealMacro.keepImport = true
function getAbsolutePath(instancePath: string, rootPath: string) {
if (instancePath.charAt(0) === '.') {
let absoluteInstancePath = nodePath.resolve(rootPath, instancePath)
return absoluteInstancePath
}
return false
}
function getInstancePathToCompare(instancePath: string, rootPath: string) {
let absolutePath = getAbsolutePath(instancePath, rootPath)
if (absolutePath === false) {
return instancePath
}
return absolutePath
}
export default function(babel: *) {
let t = babel.types
return {
name: 'emotion',
inherits: require('babel-plugin-syntax-jsx'),
visitor: {
ImportDeclaration(path: *, state: *) {
const hasFilepath =
path.hub.file.opts.filename &&
path.hub.file.opts.filename !== 'unknown'
let dirname = hasFilepath
? nodePath.dirname(path.hub.file.opts.filename)
: ''
if (
!state.pluginMacros[path.node.source.value] &&
state.emotionInstancePaths.indexOf(
getInstancePathToCompare(path.node.source.value, dirname)
) !== -1
) {
state.pluginMacros[path.node.source.value] = createEmotionMacro(
path.node.source.value
)
}
let pluginMacros = state.pluginMacros
// most of this is from https://github.com/kentcdodds/babel-plugin-macros/blob/master/src/index.js
if (pluginMacros[path.node.source.value] === undefined) {
return
}
if (t.isImportNamespaceSpecifier(path.node.specifiers[0])) {
return
}
const imports = path.node.specifiers.map(s => ({
localName: s.local.name,
importedName:
s.type === 'ImportDefaultSpecifier' ? 'default' : s.imported.name
}))
let shouldExit = false
let hasReferences = false
const referencePathsByImportName = imports.reduce(
(byName, { importedName, localName }) => {
let binding = path.scope.getBinding(localName)
if (!binding) {
shouldExit = true
return byName
}
byName[importedName] = binding.referencePaths
hasReferences =
hasReferences || Boolean(byName[importedName].length)
return byName
},
{}
)
if (!hasReferences || shouldExit) {
return
}
/**
* Other plugins that run before babel-plugin-macros might use path.replace, where a path is
* put into its own replacement. Apparently babel does not update the scope after such
* an operation. As a remedy, the whole scope is traversed again with an empty "Identifier"
* visitor - this makes the problem go away.
*
* See: https://github.com/kentcdodds/import-all.macro/issues/7
*/
state.file.scope.path.traverse({
Identifier() {}
})
pluginMacros[path.node.source.value]({
references: referencePathsByImportName,
state,
babel,
isBabelMacrosCall: true,
isEmotionCall: true
})
if (!pluginMacros[path.node.source.value].keepImport) {
path.remove()
}
},
Program(path: *, state: *) {
state.emotionInstancePaths = (state.opts.instances || []).map(
instancePath => getInstancePathToCompare(instancePath, process.cwd())
)
state.pluginMacros = {
'@emotion/css': cssMacro,
'@emotion/styled': webStyledMacro,
'@emotion/core': emotionCoreMacroThatsNotARealMacro,
'@emotion/primitives': primitivesStyledMacro,
'@emotion/native': nativeStyledMacro,
emotion: createEmotionMacro('emotion')
}
if (state.opts.cssPropOptimization === undefined) {
for (const node of path.node.body) {
if (
t.isImportDeclaration(node) &&
node.source.value === '@emotion/core' &&
node.specifiers.some(
x => t.isImportSpecifier(x) && x.imported.name === 'jsx'
)
) {
state.transformCssProp = true
break
}
}
} else {
state.transformCssProp = state.opts.cssPropOptimization
}
if (state.opts.sourceMap === false) {
state.emotionSourceMap = false
} else {
state.emotionSourceMap = true
}
},
JSXAttribute(path: *, state: *) {
if (path.node.name.name !== 'css' || !state.transformCssProp) {
return
}
if (
t.isJSXExpressionContainer(path.node.value) &&
(t.isObjectExpression(path.node.value.expression) ||
t.isArrayExpression(path.node.value.expression))
) {
let expressionPath = path.get('value.expression')
let sourceMap =
state.emotionSourceMap && path.node.loc !== undefined
? getSourceMap(path.node.loc.start, state)
: ''
expressionPath.replaceWith(
t.callExpression(
// the name of this identifier doesn't really matter at all
// it'll never appear in generated code
t.identifier('___shouldNeverAppearCSS'),
[path.node.value.expression]
)
)
transformCssCallExpression({
babel,
state,
path: expressionPath,
sourceMap
})
if (t.isCallExpression(expressionPath)) {
if (!state.cssIdentifier) {
state.cssIdentifier = addNamed(path, 'css', '@emotion/core', {
nameHint: 'css'
})
}
expressionPath
.get('callee')
.replaceWith(t.cloneDeep(state.cssIdentifier))
}
}
},
CallExpression: {
exit(path: BabelPath, state: EmotionBabelPluginPass) {
try {
if (
path.node.callee &&
path.node.callee.property &&
path.node.callee.property.name === 'withComponent'
) {
switch (path.node.arguments.length) {
case 1:
case 2: {
path.node.arguments[1] = getStyledOptions(t, path, state)
}
}
}
} catch (e) {
throw path.buildCodeFrameError(e)
}
}
}
}
}
}