-
Notifications
You must be signed in to change notification settings - Fork 389
Expand file tree
/
Copy pathpre-loader.js
More file actions
142 lines (134 loc) · 4.57 KB
/
pre-loader.js
File metadata and controls
142 lines (134 loc) · 4.57 KB
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
const babylon = require('@babel/parser')
const traverse = require('@babel/traverse').default
const t = require('@babel/types')
const generate = require('@babel/generator').default
const parseRequest = require('../utils/parse-request')
const isEmptyObject = require('../utils/is-empty-object')
const mergeVisitors = require('../utils/merge-visitors')
const parseQuery = require('loader-utils').parseQuery
module.exports = function (content) {
this.cacheable()
const mpx = this.getMpx()
const module = this._module
const mode = mpx.mode
const wxsModule = parseQuery(this.resourceQuery || '?').wxsModule
// 处理内联wxs
if (wxsModule) {
const wxsContentMap = mpx.wxsContentMap
const rawResourcePath = parseRequest(this.resource).rawResourcePath
content = wxsContentMap[`${rawResourcePath}~${wxsModule}`] || content
}
const visitor = {}
if (module.wxs) {
if (mode === 'ali') {
const insertNodes = babylon.parse(
'var __mpx_args__ = [];\n' +
'for (var i = 0; i < arguments.length; i++) {\n' +
' __mpx_args__[i] = arguments[i];\n' +
'}'
).program.body
mergeVisitors(visitor, {
Identifier (path) {
if (path.node.name === 'arguments') {
path.node.name = '__mpx_args__'
const targetPath = path.getFunctionParent().get('body')
if (!targetPath.inserted) {
const results = targetPath.unshiftContainer('body', insertNodes) || []
targetPath.inserted = true
results.forEach((item) => {
item.shouldStopTraverse = true
})
}
}
},
ForStatement (path) {
if (path.shouldStopTraverse) {
path.stop()
}
},
// 处理vant-aliapp中export var bem = bem;这种不被acorn支持的2b语法
ExportNamedDeclaration (path) {
if (
path.node.declaration &&
path.node.declaration.declarations &&
path.node.declaration.declarations.length === 1 &&
path.node.declaration.declarations[0].id.name === path.node.declaration.declarations[0].init.name
) {
const name = path.node.declaration.declarations[0].id.name
path.replaceWith(t.exportNamedDeclaration(undefined, [t.exportSpecifier(t.identifier(name), t.identifier(name))]))
}
}
})
}
if (mode !== 'wx') {
mergeVisitors(visitor, {
CallExpression (path) {
const callee = path.node.callee
if (t.isIdentifier(callee) && callee.name === 'getRegExp') {
const argPath = path.get('arguments')[0]
if (argPath.isStringLiteral()) {
argPath.replaceWith(t.stringLiteral(argPath.node.extra.raw.slice(1, -1)))
}
}
}
})
}
}
if (mode === 'dd') {
mergeVisitors(visitor, {
MemberExpression (path) {
const property = path.node.property
if (
(property.name === 'constructor' || property.value === 'constructor') &&
!(t.isMemberExpression(path.parent) && path.parentKey === 'object')
) {
path.replaceWith(t.logicalExpression('||', t.memberExpression(path.node, t.identifier('name')), path.node))
path.skip()
}
}
})
}
if (!module.wxs) {
mergeVisitors(visitor, {
MemberExpression (path) {
if (!t.isMemberExpression(path.node)) {
return
}
const property = path.node.property
if (
property && (property.name === 'constructor' || property.value === 'constructor') &&
!(t.isMemberExpression(path.parent) && path.parentKey === 'object')
) {
path.replaceWith(t.memberExpression(path.node, t.identifier('name')))
path.skip()
}
},
CallExpression (path) {
const callee = path.node.callee
const args = path.node.arguments
const transMap = {
getDate: 'Date',
getRegExp: 'RegExp'
}
if (t.isIdentifier(callee) && transMap[callee.name]) {
if (callee.name === 'getRegExp') {
const arg = args[0]
if (t.isStringLiteral(arg)) {
args[0] = t.stringLiteral(arg.extra.raw.slice(1, -1))
}
}
path.replaceWith(t.newExpression(t.identifier(transMap[callee.name]), args))
}
}
})
}
if (!isEmptyObject(visitor)) {
const ast = babylon.parse(content, {
sourceType: 'module'
})
traverse(ast, visitor)
return generate(ast).code
} else {
return content
}
}