-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
194 lines (188 loc) · 6.1 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
'use strict'
const babylon = require('babylon')
const parse = require('esx/lib/parse')
const { marker } = require('esx/lib/symbols')
const components = new Proxy({}, {
has(t, p) { return p[0].toUpperCase() === p[0] },
get(t, p) {
const o = {[p]: () =>{}}
return o[p]
}
})
function converter ({ pragma }) {
const rx = RegExp(`^${pragma.replace(/\./, '\\.')}\\(|^"`)
const convert = (strings, expressions) => {
const state = parse(components, strings, expressions)
const { tree } = state
var i = tree.length
const map = {}
while (i--) {
const [tag, props, childMap, meta] = tree[i]
const children = new Array(childMap.length)
const { dynAttrs, dynChildren, spread, spreadIndices } = meta
const keys = Object.keys(props)
for (var c in childMap) {
if (typeof childMap[c] === 'number') {
children[c] = map[childMap[c]]
} else {
children[c] = childMap[c] || null
}
}
if (dynAttrs) {
for (var p in dynAttrs) {
const overridden = spread && spreadIndices.filter((n => {
return dynAttrs[p] < n
})).some((n) => {
return p in expressions[n] && spread[n].before.indexOf(p) > -1
})
if (overridden) continue
if (props[p] !== marker) continue // this means later static property, should override
props[p] = expressions[dynAttrs[p]]
}
}
if (dynChildren) {
for (var n in dynChildren) {
children[n] = expressions[dynChildren[n]]
}
}
var serializedProps = 'null'
if (keys.length === 0 && spreadIndices.length === 1) {
serializedProps = source(expressions[spreadIndices[0]])
} else if (keys.length > 0 && spreadIndices.length === 0) {
serializedProps = serialize(props)
} else if (keys.length > 0 && spreadIndices.length > 0) {
serializedProps = '{'
for (const ix of spreadIndices) {
const {before, after} = spread[ix]
if (before.length > 0) serializedProps += kv(pick(props, before)) + ','
serializedProps += '...' + serialize(expressions[ix]) + ','
if (after.length > 0) serializedProps += kv(pick(props, after)) + ','
}
serializedProps = serializedProps.slice(0, -1) + '}'
}
const reactChildren = children.length === 0 ? (props.children || null) : (children.length === 1 ? children[0] : children)
if (reactChildren === null) {
map[i] = `${pragma}(${tag.name || `'${tag}'`}, ${serializedProps})`
} else {
map[i] = `${pragma}(${tag.name || `'${tag}'`}, ${serializedProps}, ${serialize(reactChildren)})`
}
}
return map[0]
}
function pick (from, keys) {
var filt = {}
var i = keys.length
while (i--) {
var key = keys[i]
if (key in from) {
filt[key] = from[key]
}
}
return filt
}
function kv (o) {
var str = ''
for (const k in o) {
try {
// passing through Function checks it the key
// is legal or needs to be quoted
str += Function(k, `return ${k}`)(k) + ':' + serialize(o[k]) + ','
} catch (e) {
str += JSON.stringify(k) + ':' + serialize(o[k]) + ','
}
}
return str.slice(0, -1)
}
function serialize (o) {
if (Array.isArray(o)) return '[' + o.map((o) => serialize(o)).join(', ') + ']'
if (typeof o === 'string') return rx.test(o) ? o : JSON.stringify(o)
if (typeof o === 'object') {
if ('start' in o) return source(o)
return '{' + kv(o) + '}'
}
}
function source (node) {
const { start, end } = node
return convert.code.slice(start, end)
}
return convert
}
function required (mod, path) {
const { node } = path
const { name } = node.callee
if (name !== 'require') return false
const [ arg ] = node.arguments
return (arg.value === mod)
}
function transform(_, options = {}) {
const {
framework = 'React',
include = 'import'
} = options
const pragma = framework === 'React' ? 'React.createElement' : false
if (pragma === false) {
throw Error('React is currently the only supported framework')
}
if (include !== 'import' && include !== 'require') {
throw Error('Include option must be either import or require')
}
const convert = converter({ pragma })
const load = include === 'import' ?
`import React from 'react'` :
`const React = require('react')`
var reactIncluded = false
return {
name: 'esx',
visitor: {
Program: {
exit ({node}) {
if (reactIncluded) return
const [ inject ] = babylon.parse(load, {
allowImportExportEverywhere: true
}).program.body
const { body } = node
body.unshift(inject)
}
},
CallExpression (path) {
if (required('react', path) && path.parentPath.node.id) {
if (path.parentPath.node.id.name === 'React') {
reactIncluded = true
}
}
if (required('esx', path)) {
path.parentPath.parentPath.remove()
}
},
ImportDeclaration (path) {
path.get('specifiers')
.filter(({node}) => node.type === 'ImportDefaultSpecifier')
.forEach(({parent}) => {
const { source } = parent
const { local } = parent.specifiers
.find(({type}) => type === 'ImportDefaultSpecifier')
if (local.name === 'React' && source.value == 'react') {
reactIncluded = true
}
})
},
MemberExpression (path) {
const { node } = path
const { object, property } = node
if (property.name === 'register' && object.name === 'esx') {
path.parentPath.remove()
}
},
TaggedTemplateExpression (path, { file }) {
const { quasi } = path.node
const { quasis, expressions } = quasi
const strings = quasis.map(({value}) => value.cooked)
const { code } = file
convert.code = code
const converted = convert(strings, expressions)
path.replaceWithSourceString(converted)
}
}
}
}
module.exports = transform