-
Notifications
You must be signed in to change notification settings - Fork 26
/
index.js
234 lines (176 loc) · 4.93 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
const wxmlToAxml = require('./lib/wxml/index')
const wxmlInclude = require('./lib/wxml/include')
const wxssToAcss = require('./lib/wxss/index')
const jsToAli = require('./lib/js/index')
const parseJson = require('./lib/json/index')
const wxsToJs = require('./lib/wxs/index')
const svgToPng = require('./lib/svg/index')
const fs = require('file-system')
const path = require('path')
// 获取文件格式
function getFileExt (path) {
let match = path.match(/\.(\w+)$/)
return match ? match[1] : ''
}
function getDest (src) {
let paths = src.split(path.sep)
if (!paths[paths.length - 1]) {
paths.pop()
}
paths[paths.length - 1] = paths[paths.length - 1] + '_alipay'
return paths.join(path.sep)
}
function getFilePath (filepath) {
return path.join(__dirname, filepath)
}
function jsToAliHelp (relative, code) {
let destPath = path.relative(relative, 'my.shim.js').replace('../', '')
// 追加polyfill
code = `var _myShim = require('${destPath}');
${code}
`
code = jsToAli(code, {
relative
})
return code
}
function printWarn (warn) {
let msg = '-----打包警告信息:-----\n'
for (let key in warn) {
let result = warn[key]
if (result.length) {
msg += `文件目录${key}:\n`
result.forEach((item, index) => {
msg += `${index + 1}. ${item}\n`
})
}
}
if (msg.length > 20) {
msg += `wxml校验文档: https://github.com/douzi8/wxToAlipay/tree/master/lib/wxml`
console.warn(msg)
}
}
// 复制polyfill到根目录
function copyPolyFill (shimPath, dest) {
console.log('复制polyfill到根目录')
const babel = require("babel-core")
fs.copyFileSync(shimPath, path.join(dest, 'my.shim.js'), {
process: function(contents) {
// 第一次批量处理
contents = babel.transform(contents, {
presets: [
['es2015', { modules: false }]
],
comments: false,
}).code
let es = fs.readFileSync(getFilePath('lib/js/es.polyfill.js'), { encoding: 'utf8' })
return `${es}
${contents.trim()}`;
}
});
// 复制Reflect
fs.copyFileSync(getFilePath('lib/js/es.reflect.js'), path.join(dest, 'es.reflect.js'))
}
function fixedWxmlInclude (dest) {
let appJson = fs.readFileSync(path.join(dest, 'app.json'), { encoding: 'utf8' })
appJson = JSON.parse(appJson)
let pagePath = []
appJson.pages.forEach(item => {
let wxmlpath = path.join(dest, item + '.axml')
pagePath.push(wxmlpath)
let code = fs.readFileSync(wxmlpath, { encoding: 'utf8' })
fs.writeFileSync(wxmlpath, wxmlInclude(code, dest, wxmlpath), { encoding: 'utf8' })
})
// 删掉非页面和非模块的模板
fs.recurseSync(dest, ['**/*.axml'], function(filepath) {
if (pagePath.includes(filepath)) return
let jsonPath = filepath.replace(/\.axml$/, '.json')
if (fs.existsSync(jsonPath)) {
let code = fs.readFileSync(jsonPath, { encoding: 'utf8' })
code = JSON.parse(code)
if (code.component) return
}
fs.unlinkSync(filepath)
});
}
function wxToalipay ({
src,
dest,
filter,
svgToImage,
shimPath,
callback
}) {
if (!src) {
throw '必须配置微信小程序源码目录'
}
if (!dest) {
dest = getDest(src)
}
if (!Array.isArray(filter)) {
filter = []
}
filter = [
`**/*.{js,wxss,wxml, wxs,json, png, jpg, svg}`,
'!project.config.json',
'!node_modules/**/*',
].concat(filter)
if (shimPath) {
filter.push(`!${shimPath}`)
}
// 删除dest
if (fs.existsSync(dest)) {
fs.rmdirSync(dest)
}
// 警告信息
let warn = {}
fs.copySync(src, dest, {
noProcess: '**/*.{jpg, png, svg}',
filter,
process (contents, filepath, relative) {
console.log(`正在打包 ${relative}`)
let ext = getFileExt(relative)
let destFilepath
warn[relative] = []
switch (ext) {
case 'wxss':
destFilepath = path.join(dest, relative.replace(/\.wxss$/, '.acss'));
contents = wxssToAcss(contents)
break
case 'js':
contents = jsToAliHelp(relative, contents)
break
case 'wxml':
destFilepath = path.join(dest, relative.replace(/\.wxml$/, '.axml'));
contents = wxmlToAxml(contents, {
warn: warn[relative],
filepath,
root: dest
})
break
case 'json':
contents = parseJson(contents, relative === 'app.json')
break
case 'wxs':
destFilepath = path.join(dest, relative.replace(/\.wxs$/, '.sjs'))
contents = wxsToJs(contents)
break
}
if (callback) {
contents = callback(contents, relative)
}
if (destFilepath) {
return {
filepath: destFilepath,
contents
}
}
return contents
}
});
copyPolyFill(getFilePath('lib/js/shim.js'), dest)
// 解决include标签bug
fixedWxmlInclude(dest)
printWarn(warn)
}
module.exports = wxToalipay