-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
161 lines (141 loc) · 5.02 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
// https://github.com/CJY0208/babel-plugin-tester 开发
const crypto = require('crypto')
const jsxHelpers = require('jsx-ast-utils')
const {
get,
getKey2Id,
isFunction,
callExpressionVisitor
} = require('./helpers')
module.exports = function({ types: t, template, env: getEnv }) {
// 7.x https://github.com/babel/babel/blob/master/babel.config.js#L4
// 6.x https://github.com/babel/babel/blob/6.x/packages/babel-core/src/transformation/file/options/build-config-chain.js#L165
// 尝试从 env 函数或 process 中获取当前 babel 环境
const env = isFunction(getEnv)
? getEnv()
: process.env.BABEL_ENV || process.env.NODE_ENV || 'development'
const jSXAttribute = (t.jSXAttribute || t.jsxAttribute).bind(t)
const jSXIdentifier = (t.jSXIdentifier || t.jsxIdentifier).bind(t)
const jSXExpressionContainer = (
t.jSXExpressionContainer || t.jsxExpressionContainer
).bind(t)
function getElementVisitor(filehashIdentifier) {
const KATypeCountMap = new Map()
// 对每种 NodeType 做编号处理
const key2Id = getKey2Id()
function genUUID(node) {
try {
const typeId = key2Id(get(node, 'name.name'))
const count = KATypeCountMap.get(typeId) || 0
const kaValue = count + 1
KATypeCountMap.set(typeId, kaValue)
const nodeId = `${typeId}${kaValue.toString(32)}`
const isArrayElement = node.__isArrayElement
const rawStart = isArrayElement ? 'iAr' : ''
return jSXExpressionContainer(
t.templateLiteral(
[
t.templateElement({ raw: rawStart, cooked: rawStart }),
t.templateElement({ raw: nodeId, cooked: nodeId }, true)
],
[filehashIdentifier]
)
)
} catch (error) {
return t.stringLiteral(`error`)
}
}
return {
JSXOpeningElement: {
enter(path) {
const { node } = path
// 排除 Fragment
// TODO: 考虑 Fragment 重命名情况
if (jsxHelpers.elementType(node).includes('Fragment')) {
return
}
const hasKey = jsxHelpers.hasProp(node.attributes, 'key')
const keyAttr = jsxHelpers.getProp(node.attributes, 'key')
const keyAttrValue = get(keyAttr, 'value.value')
// 排除 key 为以下的项,保证 SSR 时两端结果一致
if (
hasKey &&
['keep-alive-placeholder', 'keeper-container'].includes(
keyAttrValue
)
) {
return
}
const isArrayElement = node.__isArrayElement
// 不允许自定义 _ka 属性
// DONE: 使用 key 属性替换,需考虑不覆盖 array 结构中的 key 属性,array 结构中保持 _ka 属性
// 可参考:https://github.com/yannickcr/eslint-plugin-react/blob/master/lib/rules/jsx-key.js
const attributes = node.attributes.filter(attr => {
try {
return (
attr.type !== 'JSXAttribute' ||
jsxHelpers.propName(attr) !== '_ka'
)
} catch (error) {
return true
}
})
const uuidName =
env !== 'production' || isArrayElement || hasKey ? '_ka' : 'key'
node.attributes = [
...attributes,
jSXAttribute(jSXIdentifier(uuidName), genUUID(node))
]
}
}
}
}
return {
visitor: {
Program: {
enter(path, { cwd, filename, file: { opts = {} } = {} }) {
const md5 = crypto.createHash('md5')
const filepath =
filename && filename.replace && cwd
? filename.replace(cwd, '')
: opts.sourceFileName
md5.update(filepath)
const hash = md5.digest('base64').slice(0, 4)
const filehashIdentifier = path.scope.generateUidIdentifier(
'filehash'
)
let filehashTemplate
try {
filehashTemplate = template(`const %%filehash%% = %%hashString%%;`)(
{
filehash: filehashIdentifier,
hashString: t.stringLiteral(hash)
}
)
} catch (error) {
filehashTemplate = template(
`const ${filehashIdentifier.name} = '${hash}';`
)()
}
const imports = path.node.body.filter(node =>
t.isImportDeclaration(node)
)
if (imports.length > 0) {
const insertPlace = imports[imports.length - 1]
const insertPlacePath = path.get(
`body.${path.node.body.indexOf(insertPlace)}`
)
insertPlacePath.insertAfter(filehashTemplate)
} else {
const insertPlacePath = path.get(`body.0`)
if (insertPlacePath) {
insertPlacePath.insertBefore(filehashTemplate)
}
}
path.traverse(callExpressionVisitor)
path.traverse(getElementVisitor(filehashIdentifier))
}
}
}
}
}