/
index.ts
195 lines (190 loc) · 6.03 KB
/
index.ts
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
import traverse, { Binding, NodePath } from 'babel-traverse'
import generate from 'babel-generator'
import { Transformer } from './class'
import { prettyPrint } from 'html'
import { setting } from './utils'
import * as t from 'babel-types'
import { DEFAULT_Component_SET, INTERNAL_SAFE_GET, TARO_PACKAGE_NAME, ASYNC_PACKAGE_NAME, REDUX_PACKAGE_NAME, INTERNAL_DYNAMIC, IMAGE_COMPONENTS } from './constant'
import { transform as parse } from 'babel-core'
import { transform as babel7Transform } from '@babel/core'
export interface Options {
isRoot: boolean,
isApp: boolean,
path: string,
code: string,
isTyped: boolean,
isNormal: boolean
}
export interface Result {
template: string
components: {
name: string,
path: string
}[]
}
interface TransformResult extends Result {
code: string,
ast: t.File
}
export default function transform<T> (options: Options): TransformResult {
const code = options.isTyped
? babel7Transform(options.code, {
parserOpts: {
sourceType: 'module',
plugins: [
'typescript',
'classProperties',
'jsx',
'trailingFunctionCommas',
'asyncFunctions',
'exponentiationOperator',
'asyncGenerators',
'objectRestSpread',
'decorators'
] as any[]
}
}).code
: options.code
setting.sourceCode = code
// babel-traverse 无法生成 Hub
// 导致 Path#getSource|buildCodeFrameError 都无法直接使用
// 原因大概是 babylon.parse 没有生成 File 实例导致 scope 和 path 原型上都没有 `file`
// 将来升级到 babel@7 可以直接用 parse 而不是 transform
const ast = parse(code, {
parserOpts: {
sourceType: 'module',
plugins: [
'typescript',
'classProperties',
'jsx',
'flow',
'flowComment',
'trailingFunctionCommas',
'asyncFunctions',
'exponentiationOperator',
'asyncGenerators',
'objectRestSpread',
'decorators'
] as any[]
}
}).ast as t.File
if (options.isNormal) {
return { ast } as any
}
// transformFromAst(ast, code)
let result
const componentSourceMap = new Map<string, string[]>()
const imageSource = new Set<string>()
let mainClass!: NodePath<t.ClassDeclaration>
let storeName!: string
let renderMethod!: NodePath<t.ClassMethod>
traverse(ast, {
ClassDeclaration (path) {
mainClass = path
},
ClassMethod (path) {
if (t.isIdentifier(path.node.key) && path.node.key.name === 'render') {
renderMethod = path
}
},
AwaitExpression (path) {
const isAsyncImported = ast.program.body.some(statement => {
return t.isImportDeclaration(statement) && statement.source.value === ASYNC_PACKAGE_NAME
})
if (!isAsyncImported) {
ast.program.body.unshift(
t.importDeclaration([], t.stringLiteral(ASYNC_PACKAGE_NAME))
)
}
},
JSXOpeningElement (path) {
const { name } = path.node.name as t.JSXIdentifier
if (name === 'Provider') {
const modules = path.scope.getAllBindings('module')
const providerBinding = Object.values(modules).some((m: Binding) => m.identifier.name === 'Provider')
if (providerBinding) {
path.node.name = t.jSXIdentifier('View')
const store = path.node.attributes.find(attr => attr.name.name === 'store')
if (store && t.isJSXExpressionContainer(store.value) && t.isIdentifier(store.value.expression)) {
storeName = store.value.expression.name
}
path.node.attributes = []
}
}
if (IMAGE_COMPONENTS.has(name)) {
for (const attr of path.node.attributes) {
if (
t.isIdentifier(attr) &&
attr.name.name === 'src'
) {
if (t.isStringLiteral(attr.value)) {
imageSource.add(attr.value.value)
} else if (t.isJSXExpressionContainer(attr.value)) {
if (t.isStringLiteral(attr.value.expression)) {
imageSource.add(attr.value.expression.value)
}
}
}
}
}
},
ImportDeclaration (path) {
const source = path.node.source.value
const names: string[] = []
if (source === TARO_PACKAGE_NAME) {
path.node.specifiers.push(
t.importSpecifier(t.identifier(INTERNAL_SAFE_GET), t.identifier(INTERNAL_SAFE_GET)),
t.importSpecifier(t.identifier(INTERNAL_DYNAMIC), t.identifier(INTERNAL_DYNAMIC))
)
}
if (
source === REDUX_PACKAGE_NAME
) {
path.node.specifiers.forEach((s, index, specs) => {
if (s.local.name === 'Provider') {
specs.splice(index, 1)
specs.push(
t.importSpecifier(t.identifier('setStore'), t.identifier('setStore'))
)
}
})
}
path.traverse({
ImportDefaultSpecifier (path) {
const name = path.node.local.name
DEFAULT_Component_SET.has(name) || names.push(name)
},
ImportSpecifier (path) {
const name = path.node.local.name
DEFAULT_Component_SET.has(name) || names.push(name)
}
})
componentSourceMap.set(source, names)
}
})
const storeBinding = mainClass.scope.getBinding(storeName)
if (storeBinding) {
const statementPath = storeBinding.path.getStatementParent()
if (statementPath) {
ast.program.body.forEach((node, index, body) => {
if (node === statementPath.node) {
body.splice(index + 1, 0, t.expressionStatement(
t.callExpression(t.identifier('setStore'), [
t.identifier(storeName)
])
))
}
})
}
}
if (options.isApp) {
renderMethod.remove()
return { ast } as TransformResult
}
result = new Transformer(mainClass, options.isRoot, componentSourceMap, options.path).result
result.code = generate(ast).code
result.ast = ast
result.template = prettyPrint(result.template)
result.imageSrcs = Array.from(imageSource)
return result
}