/
compileJs.js
139 lines (124 loc) · 3.95 KB
/
compileJs.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
const path = require('path')
const fs = require('fs-extra')
const {
customBabelHandle,
behavourHandle,
replaceGlobalhandleFn,
getApiNameHandleFn,
commentBlock,
requireModuleFn,
ifProcessHandleFn,
prettierCode,
renamehandleFn,
wxConfigHandle,
antmoveActionHandle,
} = require('@antmove/utils')
const Config = require('../../config')
const wrapApisInfo = require('../../config/apiInfo/index').descObject
global.shortApiInfo = []
const apiWhiteList = ['__target__']
const customComponentPrefix = Config.library.customComponentPrefix
module.exports = function(fileInfo, ctx, originCode, apis) {
originCode = renamehandleFn(originCode)
originCode = customBabelHandle(originCode, ctx)
// 缺失api
getApiNameHandleFn(originCode, 'wx', (_api) => {
if (!apiWhiteList.includes(_api) && !global.shortApiInfo.includes(_api) && !wrapApisInfo[_api]) {
global.shortApiInfo.push(_api)
}
})
originCode = behavourHandle(originCode)
originCode = wxConfigHandle(originCode)
if (!Config.component2 && fileInfo.parent && fileInfo.parent.is) {
originCode = processComponentIs(originCode, fileInfo.parent.is)
}
originCode = ifProcessHandleFn(originCode, {
entry: 'wx',
dist: 'alipay',
code: 'wx.__target__',
})
let isMatchPlatformApi = '' // originCode.match(/\bwx\.(\w+)/g);
if (/wx(\.\w+)?/.test(originCode)) {
isMatchPlatformApi = true
}
originCode = replaceGlobalhandleFn(originCode, 'global', 'my.global')
Config.compile.wrapApis = Object.assign(Config.compile.wrapApis, apis)
originCode = commentBlock(originCode)
originCode = requireModuleFn(originCode, ctx)
/**
* 判断是否为 App()/Page()/Component()
* */
const componentWrapFnPath = `${customComponentPrefix}/component/componentClass.js`
const sep = ctx.$options.isNpmComponent ? '' : '../'
let apiPath = `${customComponentPrefix}/api/index.js`
let _componentPath = componentWrapFnPath
/**
* absolute to relative
*/
_componentPath = path
.relative(
path.join(fileInfo.dist, '../'),
path.join(fileInfo.output, _componentPath),
)
.replace(/\\/g, '/')
apiPath = path
.relative(
path.join(fileInfo.dist, sep),
path.join(fileInfo.output, apiPath),
)
.replace(/\\/g, '/')
if (_componentPath[0] !== '.') {
_componentPath = `./${_componentPath}`
}
if (apiPath[0] !== '.') {
apiPath = `./${apiPath}`
}
let insertCode = ''
if (fileInfo.filename === 'app.js' && fileInfo.deep === 0) {
insertCode += `import '${_componentPath}';\nmy.global={};\n`
// btn styleV2
const appContent = fs.readJSONSync(path.join(fileInfo.entry, './app.json'))
if (appContent.style) {
insertCode += `my.styleV2 = ${appContent.style === 'v2'}\n`
}
}
if (isMatchPlatformApi || (fileInfo.parent && fileInfo.parent.tplInfo)) {
let type = 'my'
if (Config.aliAppType === 'dingding') {
type = 'dd'
}
insertCode += `const _my = require('${apiPath}')(${type});
const wx = _my;
`
}
// Todo:
// if (fileInfo.parent && fileInfo.parent.tplInfo) {
// fileInfo.parent.tplInfo.button &&
// fileInfo.parent.tplInfo.button
// .forEach(function (info) {
// // todos
// // if (info.type === 'button' && info.scope)
// // originCode = processFnBodyHandleFn(originCode, info);
// });
// }
originCode = insertCode + originCode
originCode = prettierCode(originCode)
if (fileInfo.parent && fileInfo.parent.hasAntmoveAction) {
originCode = antmoveActionHandle(originCode, fileInfo)
originCode = originCode.replace(/"antmoveActionHandler";/g, '//执行时动态赋值,请勿删除')
}
fs.outputFileSync(fileInfo.dist, originCode)
}
function processComponentIs(code, isPath = '') {
if (isPath) {
code = `
my.setStorageSync({
key: 'activeComponent',
data: {
is: '${isPath}'
}
})\n
${code}`
}
return code
}