-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
183 lines (161 loc) · 4.84 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
'use-strict'
const visited = Symbol('visited')
const imported = Symbol('imported')
module.exports = function dvaCoreHmrPlugin({
types: t,
template
}) {
function getImportRequirePath(identifierName, scope) {
if (scope.hasBinding(identifierName)) {
const binding = scope.getBinding(identifierName)
if (binding) {
const parent = binding.path.parent
if (t.isImportDeclaration(parent)) { // import xxx from 'xxx' or import { xxx } from 'xxx'
return parent.source.value
} else if (t.isVariableDeclaration(parent)) {
const declarator = findDeclarator(parent.declarations, identifierName)
if (declarator) {
if (isRequire(declarator.init)) { // const xxx = require('xxx')
return getArguments0(declarator.init)
} else if (isRequireMember(declarator.init)) { // const xxx = require('xxx').xxx
return getArguments0(declarator.init.object)
}
}
}
}
}
return null
}
function isModelCall(node, appNames = ['app']) {
if (!t.isMemberExpression(node)) return false
const {
object,
property
} = node
return (
(t.isIdentifier(property) && property.name === 'model') &&
(t.isIdentifier(object) && appNames.indexOf(object.name) >= 0)
)
}
function isRequire(node) {
return t.isCallExpression(node) &&
t.isIdentifier(node.callee) &&
node.callee.name === 'require'
}
function isRequireMember(node) {
if (!t.isMemberExpression(node)) return false
const {
object,
property
} = node
return isRequire(object) &&
t.isIdentifier(property) &&
property.name
}
function findDeclarator(declarations, identifier) {
// eslint-disable-next-line
for (const d of declarations) {
if (t.isIdentifier(d.id) && d.id.name === identifier) {
return d
}
}
return null
}
function getArguments0(node) {
if (t.isLiteral(node.arguments[0])) {
return node.arguments[0].value
}
return null
}
function getRequirePath(node, scope) {
switch (node.type) {
// app.model(require('xxxx))
case 'CallExpression':
{
const path = getArguments0(node)
if (path) {
return path
}
break
}
case 'Identifier':
{
const path = getImportRequirePath(node.name, scope)
if (path) {
return path
}
break
}
// app.model(require('xxxx).xxxxx)
case 'MemberExpression':
{
if (isRequireMember(node)) {
const path = getArguments0(node.object)
if (path) {
return path
}
}
break
}
default:
break
}
return null
}
const hmrTemplate = template(`
if (module.hot) {
module.hot.accept('MODELPATH', () => {
try {
let newModel = require('MODELPATH');
if (newModel.default) newModel = newModel.default;
APPNAME.replaceModel(newModel);
} catch(e) {
console.error(e);
}
});
}`)
return {
name: 'dva-core-hmr',
visitor: {
CallExpression(path) {
if (path[visited]) return
path[visited] = true
const { callee, arguments: args } = path.node
const isDva =
this.file.opts.filename &&
(
this.file.opts.filename.indexOf('/node_modules/_dva-core@') >= 0 || // tnpm
this.file.opts.filename.indexOf('/node_modules/_dva@') >= 0 ||
this.file.opts.filename.indexOf('/node_modules/dva-core/') >= 0 || // npm
this.file.opts.filename.indexOf('/node_modules/dva/') >= 0
)
// don't do anything within dva itself
if (!isDva && isModelCall(callee, this.opts.appNames)) {
const modelPath = getRequirePath(args[0], path.scope)
// if modelPath can be found directly
if (modelPath) {
if (!this.opts.appImport || !this.opts.appImportName) {
const hmrExpression = hmrTemplate({
MODELPATH: t.stringLiteral(modelPath),
APPNAME: callee.object
})
path.parentPath.insertAfter(hmrExpression)
} else {
const programPath = path.findParent(p => p.isProgram())
if (!programPath[imported]) {
const importer = template(this.opts.appImport)
programPath.node.body.push(importer({}))
programPath[imported] = true
}
const hmrExpression = hmrTemplate({
MODELPATH: t.stringLiteral(modelPath),
APPNAME: t.identifier(this.opts.appImportName)
})
programPath.node.body.push(hmrExpression)
}
}
}
}
}
}
}